[tor-commits] [metrics-lib/master] Avoid static initializer blocks.

karsten at torproject.org karsten at torproject.org
Mon Jan 23 18:43:20 UTC 2012


commit cbda9efad1f5a514fcd62378013431b20b2f9fd8
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Mon Jan 23 18:26:36 2012 +0100

    Avoid static initializer blocks.
    
    Commit 1bf8a7f introduced a problem with parsing perfectly valid date
    strings.  Changing the SimpleDateFormat class member to a local variable
    and avoiding its initialization in a static initializer block solved the
    problem.  It's unclear what exactly the problem was, possibly related to
    class loader weirdness.  Using a local variable fixed it.  Wasted enough
    time on this problem.
---
 .../descriptor/impl/BridgeNetworkStatusImpl.java   |   11 ++++-------
 .../torproject/descriptor/impl/ParseHelper.java    |   10 ++++------
 2 files changed, 8 insertions(+), 13 deletions(-)

diff --git a/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java b/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java
index a240e1c..79ee8ce 100644
--- a/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java
+++ b/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java
@@ -18,13 +18,6 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl
     this.setPublishedMillisFromFileName(fileName);
   }
 
-  private static SimpleDateFormat fileNameFormat = new SimpleDateFormat(
-      "yyyyMMdd-HHmmss");
-  static {
-    fileNameFormat.setLenient(false);
-    fileNameFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
-  }
-
   private void setPublishedMillisFromFileName(String fileName)
       throws DescriptorParseException {
     if (fileName.length() == 
@@ -33,6 +26,10 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl
       String publishedString = fileName.substring(0,
           "yyyyMMdd-HHmmss".length());
       try {
+        SimpleDateFormat fileNameFormat = new SimpleDateFormat(
+            "yyyyMMdd-HHmmss");
+        fileNameFormat.setLenient(false);
+        fileNameFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
         this.publishedMillis = fileNameFormat.parse(publishedString).
             getTime();
       } catch (ParseException e) {
diff --git a/src/org/torproject/descriptor/impl/ParseHelper.java b/src/org/torproject/descriptor/impl/ParseHelper.java
index 8314e23..facd4ea 100644
--- a/src/org/torproject/descriptor/impl/ParseHelper.java
+++ b/src/org/torproject/descriptor/impl/ParseHelper.java
@@ -105,12 +105,6 @@ public class ParseHelper {
     return exitPattern;
   }
 
-  private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat(
-      "yyyy-MM-dd HH:mm:ss");
-  static {
-    dateTimeFormat.setLenient(false);
-    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
-  }
   public static long parseTimestampAtIndex(String line, String[] parts,
       int dateIndex, int timeIndex) throws DescriptorParseException {
     if (dateIndex >= parts.length || timeIndex >= parts.length) {
@@ -119,6 +113,10 @@ public class ParseHelper {
     }
     long result = -1L;
     try {
+      SimpleDateFormat dateTimeFormat = new SimpleDateFormat(
+          "yyyy-MM-dd HH:mm:ss");
+      dateTimeFormat.setLenient(false);
+      dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
       result = dateTimeFormat.parse(
           parts[dateIndex] + " " + parts[timeIndex]).getTime();
     } catch (ParseException e) {





More information about the tor-commits mailing list