MOA 12.03
Real Time Analytics for Data Streams
StringUtils.java
Go to the documentation of this file.
00001 /*
00002  *    StringUtils.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby ([email protected])
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.core;
00021 
00022 import java.text.DecimalFormat;
00023 
00030 public class StringUtils {
00031 
00032     public static final String newline = System.getProperty("line.separator");
00033 
00034     public static String doubleToString(double value, int fractionDigits) {
00035         return doubleToString(value, 0, fractionDigits);
00036     }
00037 
00038     public static String doubleToString(double value, int minFractionDigits,
00039             int maxFractionDigits) {
00040         DecimalFormat numberFormat = new DecimalFormat();
00041         numberFormat.setMinimumFractionDigits(minFractionDigits);
00042         numberFormat.setMaximumFractionDigits(maxFractionDigits);
00043         return numberFormat.format(value);
00044     }
00045 
00046     public static void appendNewline(StringBuilder out) {
00047         out.append(newline);
00048     }
00049 
00050     public static void appendIndent(StringBuilder out, int indent) {
00051         for (int i = 0; i < indent; i++) {
00052             out.append(' ');
00053         }
00054     }
00055 
00056     public static void appendIndented(StringBuilder out, int indent, String s) {
00057         appendIndent(out, indent);
00058         out.append(s);
00059     }
00060 
00061     public static void appendNewlineIndented(StringBuilder out, int indent,
00062             String s) {
00063         appendNewline(out);
00064         appendIndented(out, indent, s);
00065     }
00066 
00067     public static String secondsToDHMSString(double seconds) {
00068         if (seconds < 60) {
00069             return doubleToString(seconds, 2, 2) + 's';
00070         }
00071         long secs = (int) (seconds);
00072         long mins = secs / 60;
00073         long hours = mins / 60;
00074         long days = hours / 24;
00075         secs %= 60;
00076         mins %= 60;
00077         hours %= 24;
00078         StringBuilder result = new StringBuilder();
00079         if (days > 0) {
00080             result.append(days);
00081             result.append('d');
00082         }
00083         if ((hours > 0) || (days > 0)) {
00084             result.append(hours);
00085             result.append('h');
00086         }
00087         if ((hours > 0) || (days > 0) || (mins > 0)) {
00088             result.append(mins);
00089             result.append('m');
00090         }
00091         result.append(secs);
00092         result.append('s');
00093         return result.toString();
00094     }
00095 }
 All Classes Namespaces Files Functions Variables Enumerations