You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by jm...@apache.org on 2014/05/17 03:32:22 UTC

svn commit: r1595408 - /hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java

Author: jmhsieh
Date: Sat May 17 01:32:22 2014
New Revision: 1595408

URL: http://svn.apache.org/r1595408
Log:
HBASE-6990 ADDENDUM

Added:
    hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java

Added: hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java?rev=1595408&view=auto
==============================================================================
--- hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java (added)
+++ hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java Sat May 17 01:32:22 2014
@@ -0,0 +1,101 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.util;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.hbase.HConstants;
+
+@InterfaceAudience.Public
+@InterfaceStability.Evolving
+public class PrettyPrinter {
+
+  public enum Unit {
+    TIME_INTERVAL,
+    NONE
+  }
+
+  public static String format(final String value, final Unit unit) {
+    StringBuilder human = new StringBuilder();
+    switch (unit) {
+      case TIME_INTERVAL:
+        human.append(humanReadableTTL(Long.valueOf(value)));
+        break;
+      default:
+        human.append(value);
+    }
+    return human.toString();
+  }
+
+
+  private static String humanReadableTTL(final long interval){
+    StringBuilder sb = new StringBuilder();
+    int days, hours, minutes, seconds;
+
+    // edge cases first
+    if (interval == Integer.MAX_VALUE) {
+      sb.append("FOREVER");
+      return sb.toString();
+    }
+    if (interval < HConstants.MINUTE_IN_SECONDS) {
+      sb.append(interval);
+      sb.append(" SECOND").append(interval == 1 ? "" : "S");
+      return sb.toString();
+    }
+
+    days  =   (int) (interval / HConstants.DAY_IN_SECONDS);
+    hours =   (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
+    minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
+        - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
+    seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
+        - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);
+
+    sb.append(interval);
+    sb.append(" SECONDS (");
+
+    if (days > 0) {
+      sb.append(days);
+      sb.append(" DAY").append(days == 1 ? "" : "S");
+    }
+
+    if (hours > 0 ) {
+      sb.append(days > 0 ? " " : "");
+      sb.append(hours);
+      sb.append(" HOUR").append(hours == 1 ? "" : "S");
+    }
+
+    if (minutes > 0) {
+      sb.append(days + hours > 0 ? " " : "");
+      sb.append(minutes);
+      sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
+    }
+
+    if (seconds > 0) {
+      sb.append(days + hours + minutes > 0 ? " " : "");
+      sb.append(seconds);
+      sb.append(" SECOND").append(minutes == 1 ? "" : "S");
+    }
+
+    sb.append(")");
+
+    return sb.toString();
+  }
+
+}