From 7261fd8d764068e19348ea16024ba694246abb22 Mon Sep 17 00:00:00 2001 From: Jonathan Hsieh Date: Sat, 17 May 2014 01:32:22 +0000 Subject: [PATCH] HBASE-6990 ADDENDUM git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1595409 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop/hbase/util/PrettyPrinter.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java new file mode 100644 index 00000000000..702bda6d9e1 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java @@ -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(); + } + +}