Author: Jogy
Date: 2006-09-18 22:56:30 +0000 (Mon, 18 Sep 2006)
New Revision: 10487

Added:
   trunk/freenet/src/freenet/support/TimeUtil.java
Removed:
   trunk/freenet/src/freenet/support/ByteFormat.java
Modified:
   trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java
   trunk/freenet/src/test/PaddingSpeedTest.java
Log:
- refactor time formatting (4w13d, etc) into a separate class 
freenet.support.TimeUtil, just like size formatting already is (=SizeUtil)
- remove freenet.support.ByteFormat as it is only used by a test class and 
redundant to the (better) SizeUtil
- adapt DarknetConnectionsToadlet and the test-class accordingly

Modified: trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java       
2006-09-18 22:29:09 UTC (rev 10486)
+++ trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java       
2006-09-18 22:56:30 UTC (rev 10487)
@@ -50,6 +50,7 @@
 import freenet.support.MultiValueTable;
 import freenet.support.SimpleFieldSet;
 import freenet.support.SizeUtil;
+import freenet.support.TimeUtil;
 import freenet.support.io.Bucket;

 public class DarknetConnectionsToadlet extends Toadlet {
@@ -158,7 +159,7 @@
                if(peerNodeStatuses.length>0){

                        /* node status values */
-                       long nodeUptimeSeconds = ( now - node.startupTime ) / 
1000;
+                       long nodeUptimeSeconds = (now - node.startupTime) / 
1000;
                        int bwlimitDelayTime = (int) node.getBwlimitDelayTime();
                        int nodeAveragePingTime = (int) 
node.getNodeAveragePingTime();
                        int networkSizeEstimateSession = 
node.getNetworkSizeEstimate(-1);
@@ -170,7 +171,7 @@
                        double missRoutingDistance =  
node.missRoutingDistance.currentValue();
                        DecimalFormat fix1 = new DecimalFormat("##0.0%");
                        double backedoffPercent =  
node.backedoffPercent.currentValue();
-                       String nodeUptimeString = 
timeIntervalToString(nodeUptimeSeconds);
+                       String nodeUptimeString = 
TimeUtil.formatTime(nodeUptimeSeconds * 1000);  // *1000 to convert to 
milliseconds

                        // BEGIN OVERVIEW TABLE
                        HTMLNode overviewTable = contentNode.addChild("table", 
"class", "column");
@@ -802,48 +803,7 @@
                if (idle == -1) {
                        return " ";
                }
-               long idleSeconds = (now - idle) / 1000;
-               return timeIntervalToString( idleSeconds );
+               long idleMilliseconds = now - idle;
+               return TimeUtil.formatTime(idleMilliseconds);
        }
-       
-       private String timeIntervalToString(long timeInterval) {
-               StringBuffer sb = new StringBuffer(1024);
-               long l = timeInterval;
-               int termCount = 0;
-               int weeks = (int) l / (7*24*60*60);
-               if(weeks > 0) {
-                 sb.append(weeks + "w");
-                 termCount++;
-                 l = l - (weeks * (7*24*60*60));
-               }
-               int days = (int) l / (24*60*60);
-               if(days > 0) {
-                 sb.append(days + "d");
-                 termCount++;
-                 l = l - (days * (24*60*60));
-               }
-               if(termCount >= 2) {
-                 return sb.toString();
-               }
-               int hours = (int) l / (60*60);
-               if(hours > 0) {
-                 sb.append(hours + "h");
-                 termCount++;
-                 l = l - (hours * (60*60));
-               }
-               if(termCount >= 2) {
-                 return sb.toString();
-               }
-               int minutes = (int) l / 60;
-               if(minutes > 0) {
-                 sb.append(minutes + "m");
-                 termCount++;
-                 l = l - (minutes * 60);
-               }
-               if(termCount >= 2) {
-                 return sb.toString();
-               }
-               sb.append(l + "s");
-               return sb.toString();
-       }
 }

Deleted: trunk/freenet/src/freenet/support/ByteFormat.java
===================================================================
--- trunk/freenet/src/freenet/support/ByteFormat.java   2006-09-18 22:29:09 UTC 
(rev 10486)
+++ trunk/freenet/src/freenet/support/ByteFormat.java   2006-09-18 22:56:30 UTC 
(rev 10487)
@@ -1,45 +0,0 @@
-/*
-  ByteFormat.java / Freenet
-  Copyright (C) 2005-2006 The Free Network project
-  This program is free software; you can redistribute it and/or
-  modify it under the terms of the GNU General Public License as
-  published by the Free Software Foundation; either version 2 of
-  the License, or (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program; if not, write to the Free Software
-  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-*/
-
-package freenet.support;
-
-import java.text.NumberFormat;
-
-/**
- * Utility class with one function...
- * Refactored from DefaultInfolet's impl.
- */
-public class ByteFormat {
-       public static String format(long bytes, boolean html) {
-               NumberFormat nf = NumberFormat.getInstance();
-               String out;
-               if (bytes == 0)
-                       out = "None";
-               else if (bytes > (2L << 32))
-                   out = nf.format(bytes >> 30) + " GiB";
-               else if (bytes > (2 << 22))
-                   out = nf.format(bytes >> 20) + " MiB";
-               else if (bytes > (2 << 12))
-                   out = nf.format(bytes >> 10) + " KiB";
-               else
-                   out = nf.format(bytes) + " Bytes";
-               if(html)
-                   out = out.replaceAll(" ", "&nbsp;");
-               return out;
-       }
-}

Added: trunk/freenet/src/freenet/support/TimeUtil.java
===================================================================
--- trunk/freenet/src/freenet/support/TimeUtil.java     2006-09-18 22:29:09 UTC 
(rev 10486)
+++ trunk/freenet/src/freenet/support/TimeUtil.java     2006-09-18 22:56:30 UTC 
(rev 10487)
@@ -0,0 +1,75 @@
+/*
+  TimeUtil.java / Freenet
+  Copyright (C) 2005-2006 The Free Network project
+  This program is free software; you can redistribute it and/or
+  modify it under the terms of the GNU General Public License as
+  published by the Free Software Foundation; either version 2 of
+  the License, or (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+package freenet.support;
+
+/**
+ * Time formatting utility.
+ * Formats milliseconds into a week/day/hour/second string (without 
milliseconds).
+ */
+public class TimeUtil {
+       public static String formatTime(long timeInterval, int maxTerms) {
+               StringBuffer sb = new StringBuffer(64);
+               long l = timeInterval / 1000;  // ms -> s
+               int termCount = 0;
+               //
+               int weeks = (int)(l / (7*24*60*60));
+               if (weeks > 0) {
+                 sb.append(weeks + "w");
+                 termCount++;
+                 l = l - (weeks * (7*24*60*60));
+               }
+               //
+               int days = (int)(l / (24*60*60));
+               if (days > 0) {
+                 sb.append(days + "d");
+                 termCount++;
+                 l = l - (days * (24*60*60));
+               }
+               if(termCount >= maxTerms) {
+                 return sb.toString();
+               }
+               //
+               int hours = (int)(l / (60*60));
+               if (hours > 0) {
+                 sb.append(hours + "h");
+                 termCount++;
+                 l = l - (hours * (60*60));
+               }
+               if(termCount >= maxTerms) {
+                 return sb.toString();
+               }
+               //
+               int minutes = (int)(l / 60);
+               if (minutes > 0) {
+                 sb.append(minutes + "m");
+                 termCount++;
+                 l = l - (minutes * 60);
+               }
+               if(termCount >= maxTerms) {
+                 return sb.toString();
+               }
+               //
+               sb.append(l + "s");
+               return sb.toString();
+       }
+       
+       public static String formatTime(long timeInterval) {
+               return formatTime(timeInterval, 2);
+       }
+}

Modified: trunk/freenet/src/test/PaddingSpeedTest.java
===================================================================
--- trunk/freenet/src/test/PaddingSpeedTest.java        2006-09-18 22:29:09 UTC 
(rev 10486)
+++ trunk/freenet/src/test/PaddingSpeedTest.java        2006-09-18 22:56:30 UTC 
(rev 10487)
@@ -25,7 +25,7 @@

 import org.spaceroots.mantissa.random.MersenneTwister;

-import freenet.support.ByteFormat;
+import freenet.support.SizeUtil;

 /**
  * Test the speed of RNGs and hashes.
@@ -107,6 +107,6 @@
      */
     private static void printStats(String name, int bytesTotal, long interval) 
{
         double rate = bytesTotal / ((double)interval/1000);
-        System.out.println(name+": "+bytesTotal+" in "+interval+"ms = 
"+ByteFormat.format((long)rate,false)+"/s");
+        System.out.println(name+": "+bytesTotal+" in "+interval+"ms = 
"+SizeUtil.formatSize((long)rate,false)+"/s");
     }
 }


Reply via email to