lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markrmil...@apache.org
Subject svn commit: r779575 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/document/DateTools.java
Date Thu, 28 May 2009 12:01:36 GMT
Author: markrmiller
Date: Thu May 28 12:01:35 2009
New Revision: 779575

URL: http://svn.apache.org/viewvc?rev=779575&view=rev
Log:
LUCENE-1653: Avoid creating a Calendar in every call to DateTools#dateToString, DateTools#timeToString
and DateTools#round.

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/document/DateTools.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=779575&r1=779574&r2=779575&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Thu May 28 12:01:35 2009
@@ -366,6 +366,10 @@
 
  8. LUCENE-1596: MultiTermDocs speedup when set with
     MultiTermDocs.seek(MultiTermEnum) (yonik)
+    
+ 9. LUCENE-1653: Avoid creating a Calendar in every call to 
+    DateTools#dateToString, DateTools#timeToString and
+    DateTools#round.  (Shai Erera via Mark Miller)
 
 Documentation
 

Modified: lucene/java/trunk/src/java/org/apache/lucene/document/DateTools.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/document/DateTools.java?rev=779575&r1=779574&r2=779575&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/document/DateTools.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/document/DateTools.java Thu May 28 12:01:35
2009
@@ -60,6 +60,8 @@
     MILLISECOND_FORMAT.setTimeZone(GMT);
   }
 
+  private static final Calendar calInstance = Calendar.getInstance(GMT);
+  
   // cannot create, the class has static methods only
   private DateTools() {}
 
@@ -72,7 +74,7 @@
    * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,
    *  depeding on <code>resolution</code>; using GMT as timezone 
    */
-  public static String dateToString(Date date, Resolution resolution) {
+  public static synchronized String dateToString(Date date, Resolution resolution) {
     return timeToString(date.getTime(), resolution);
   }
 
@@ -85,44 +87,27 @@
    * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,
    *  depeding on <code>resolution</code>; using GMT as timezone
    */
-  public static String timeToString(long time, Resolution resolution) {
-    Calendar cal = Calendar.getInstance(GMT);
-
-    cal.setTimeInMillis(round(time, resolution));
-
-    String result;
+  public static synchronized String timeToString(long time, Resolution resolution) {
+    calInstance.setTimeInMillis(round(time, resolution));
+    Date date = calInstance.getTime();
+    
     if (resolution == Resolution.YEAR) {
-      synchronized (YEAR_FORMAT) {
-        result = YEAR_FORMAT.format(cal.getTime());
-      }
+      return YEAR_FORMAT.format(date);
     } else if (resolution == Resolution.MONTH) {
-      synchronized (MONTH_FORMAT) {
-        result = MONTH_FORMAT.format(cal.getTime());
-      }
+      return MONTH_FORMAT.format(date);
     } else if (resolution == Resolution.DAY) {
-      synchronized (DAY_FORMAT) {
-        result = DAY_FORMAT.format(cal.getTime());
-      }
+      return DAY_FORMAT.format(date);
     } else if (resolution == Resolution.HOUR) {
-      synchronized (HOUR_FORMAT) {
-        result = HOUR_FORMAT.format(cal.getTime());
-      }
+      return HOUR_FORMAT.format(date);
     } else if (resolution == Resolution.MINUTE) {
-      synchronized (MINUTE_FORMAT) {
-        result = MINUTE_FORMAT.format(cal.getTime());
-      }
+      return MINUTE_FORMAT.format(date);
     } else if (resolution == Resolution.SECOND) {
-      synchronized (SECOND_FORMAT) {
-        result = SECOND_FORMAT.format(cal.getTime());
-      }
+      return SECOND_FORMAT.format(date);
     } else if (resolution == Resolution.MILLISECOND) {
-      synchronized (MILLISECOND_FORMAT) {
-        result = MILLISECOND_FORMAT.format(cal.getTime());
-      }
-    } else {
-      throw new IllegalArgumentException("unknown resolution " + resolution);
+      return MILLISECOND_FORMAT.format(date);
     }
-    return result;
+    
+    throw new IllegalArgumentException("unknown resolution " + resolution);
   }
   
   /**
@@ -135,7 +120,7 @@
    * @throws ParseException if <code>dateString</code> is not in the 
    *  expected format 
    */
-  public static long stringToTime(String dateString) throws ParseException {
+  public static synchronized long stringToTime(String dateString) throws ParseException {
     return stringToDate(dateString).getTime();
   }
 
@@ -149,40 +134,23 @@
    * @throws ParseException if <code>dateString</code> is not in the 
    *  expected format 
    */
-  public static Date stringToDate(String dateString) throws ParseException {
-    Date date;
+  public static synchronized Date stringToDate(String dateString) throws ParseException {
     if (dateString.length() == 4) {
-      synchronized (YEAR_FORMAT) {
-        date = YEAR_FORMAT.parse(dateString);
-      }
+      return YEAR_FORMAT.parse(dateString);
     } else if (dateString.length() == 6) {
-      synchronized (MONTH_FORMAT) {
-        date = MONTH_FORMAT.parse(dateString);
-      }
+      return MONTH_FORMAT.parse(dateString);
     } else if (dateString.length() == 8) {
-      synchronized (DAY_FORMAT) {
-        date = DAY_FORMAT.parse(dateString);
-      }
+      return DAY_FORMAT.parse(dateString);
     } else if (dateString.length() == 10) {
-      synchronized (HOUR_FORMAT) {
-        date = HOUR_FORMAT.parse(dateString);
-      }
+      return HOUR_FORMAT.parse(dateString);
     } else if (dateString.length() == 12) {
-      synchronized (MINUTE_FORMAT) {
-        date = MINUTE_FORMAT.parse(dateString);
-      }
+      return MINUTE_FORMAT.parse(dateString);
     } else if (dateString.length() == 14) {
-      synchronized (SECOND_FORMAT) {
-        date = SECOND_FORMAT.parse(dateString);
-      }
+      return SECOND_FORMAT.parse(dateString);
     } else if (dateString.length() == 17) {
-      synchronized (MILLISECOND_FORMAT) {
-        date = MILLISECOND_FORMAT.parse(dateString);
-      }
-    } else {
-      throw new ParseException("Input is not valid date string: " + dateString, 0);
+      return MILLISECOND_FORMAT.parse(dateString);
     }
-    return date;
+    throw new ParseException("Input is not valid date string: " + dateString, 0);
   }
   
   /**
@@ -194,7 +162,7 @@
    * @return the date with all values more precise than <code>resolution</code>
    *  set to 0 or 1
    */
-  public static Date round(Date date, Resolution resolution) {
+  public static synchronized Date round(Date date, Resolution resolution) {
     return new Date(round(date.getTime(), resolution));
   }
   
@@ -208,47 +176,42 @@
    * @return the date with all values more precise than <code>resolution</code>
    *  set to 0 or 1, expressed as milliseconds since January 1, 1970, 00:00:00 GMT
    */
-  public static long round(long time, Resolution resolution) {
-    Calendar cal = Calendar.getInstance(GMT);
-
-    // protected in JDK's prior to 1.4
-    //cal.setTimeInMillis(time);
-    
-    cal.setTime(new Date(time));
+  public static synchronized long round(long time, Resolution resolution) {
+    calInstance.setTimeInMillis(time);
     
     if (resolution == Resolution.YEAR) {
-      cal.set(Calendar.MONTH, 0);
-      cal.set(Calendar.DAY_OF_MONTH, 1);
-      cal.set(Calendar.HOUR_OF_DAY, 0);
-      cal.set(Calendar.MINUTE, 0);
-      cal.set(Calendar.SECOND, 0);
-      cal.set(Calendar.MILLISECOND, 0);
+      calInstance.set(Calendar.MONTH, 0);
+      calInstance.set(Calendar.DAY_OF_MONTH, 1);
+      calInstance.set(Calendar.HOUR_OF_DAY, 0);
+      calInstance.set(Calendar.MINUTE, 0);
+      calInstance.set(Calendar.SECOND, 0);
+      calInstance.set(Calendar.MILLISECOND, 0);
     } else if (resolution == Resolution.MONTH) {
-      cal.set(Calendar.DAY_OF_MONTH, 1);
-      cal.set(Calendar.HOUR_OF_DAY, 0);
-      cal.set(Calendar.MINUTE, 0);
-      cal.set(Calendar.SECOND, 0);
-      cal.set(Calendar.MILLISECOND, 0);
+      calInstance.set(Calendar.DAY_OF_MONTH, 1);
+      calInstance.set(Calendar.HOUR_OF_DAY, 0);
+      calInstance.set(Calendar.MINUTE, 0);
+      calInstance.set(Calendar.SECOND, 0);
+      calInstance.set(Calendar.MILLISECOND, 0);
     } else if (resolution == Resolution.DAY) {
-      cal.set(Calendar.HOUR_OF_DAY, 0);
-      cal.set(Calendar.MINUTE, 0);
-      cal.set(Calendar.SECOND, 0);
-      cal.set(Calendar.MILLISECOND, 0);
+      calInstance.set(Calendar.HOUR_OF_DAY, 0);
+      calInstance.set(Calendar.MINUTE, 0);
+      calInstance.set(Calendar.SECOND, 0);
+      calInstance.set(Calendar.MILLISECOND, 0);
     } else if (resolution == Resolution.HOUR) {
-      cal.set(Calendar.MINUTE, 0);
-      cal.set(Calendar.SECOND, 0);
-      cal.set(Calendar.MILLISECOND, 0);
+      calInstance.set(Calendar.MINUTE, 0);
+      calInstance.set(Calendar.SECOND, 0);
+      calInstance.set(Calendar.MILLISECOND, 0);
     } else if (resolution == Resolution.MINUTE) {
-      cal.set(Calendar.SECOND, 0);
-      cal.set(Calendar.MILLISECOND, 0);
+      calInstance.set(Calendar.SECOND, 0);
+      calInstance.set(Calendar.MILLISECOND, 0);
     } else if (resolution == Resolution.SECOND) {
-      cal.set(Calendar.MILLISECOND, 0);
+      calInstance.set(Calendar.MILLISECOND, 0);
     } else if (resolution == Resolution.MILLISECOND) {
       // don't cut off anything
     } else {
       throw new IllegalArgumentException("unknown resolution " + resolution);
     }
-    return cal.getTime().getTime();
+    return calInstance.getTimeInMillis();
   }
 
   /** Specifies the time granularity. */



Mime
View raw message