LUCENE-1653: Avoid creating a Calendar in every call to DateTools#dateToString, DateTools#timeToString and DateTools#round.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@779575 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2009-05-28 12:01:35 +00:00
parent f953c8883e
commit 3c3b283e3c
2 changed files with 55 additions and 88 deletions

View File

@ -367,6 +367,10 @@ Optimizations
8. LUCENE-1596: MultiTermDocs speedup when set with 8. LUCENE-1596: MultiTermDocs speedup when set with
MultiTermDocs.seek(MultiTermEnum) (yonik) 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 Documentation
Build Build

View File

@ -60,6 +60,8 @@ public class DateTools {
MILLISECOND_FORMAT.setTimeZone(GMT); MILLISECOND_FORMAT.setTimeZone(GMT);
} }
private static final Calendar calInstance = Calendar.getInstance(GMT);
// cannot create, the class has static methods only // cannot create, the class has static methods only
private DateTools() {} private DateTools() {}
@ -72,7 +74,7 @@ public class DateTools {
* @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter, * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,
* depeding on <code>resolution</code>; using GMT as timezone * 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); return timeToString(date.getTime(), resolution);
} }
@ -85,45 +87,28 @@ public class DateTools {
* @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter, * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,
* depeding on <code>resolution</code>; using GMT as timezone * depeding on <code>resolution</code>; using GMT as timezone
*/ */
public static String timeToString(long time, Resolution resolution) { public static synchronized String timeToString(long time, Resolution resolution) {
Calendar cal = Calendar.getInstance(GMT); calInstance.setTimeInMillis(round(time, resolution));
Date date = calInstance.getTime();
cal.setTimeInMillis(round(time, resolution));
String result;
if (resolution == Resolution.YEAR) { if (resolution == Resolution.YEAR) {
synchronized (YEAR_FORMAT) { return YEAR_FORMAT.format(date);
result = YEAR_FORMAT.format(cal.getTime());
}
} else if (resolution == Resolution.MONTH) { } else if (resolution == Resolution.MONTH) {
synchronized (MONTH_FORMAT) { return MONTH_FORMAT.format(date);
result = MONTH_FORMAT.format(cal.getTime());
}
} else if (resolution == Resolution.DAY) { } else if (resolution == Resolution.DAY) {
synchronized (DAY_FORMAT) { return DAY_FORMAT.format(date);
result = DAY_FORMAT.format(cal.getTime());
}
} else if (resolution == Resolution.HOUR) { } else if (resolution == Resolution.HOUR) {
synchronized (HOUR_FORMAT) { return HOUR_FORMAT.format(date);
result = HOUR_FORMAT.format(cal.getTime());
}
} else if (resolution == Resolution.MINUTE) { } else if (resolution == Resolution.MINUTE) {
synchronized (MINUTE_FORMAT) { return MINUTE_FORMAT.format(date);
result = MINUTE_FORMAT.format(cal.getTime());
}
} else if (resolution == Resolution.SECOND) { } else if (resolution == Resolution.SECOND) {
synchronized (SECOND_FORMAT) { return SECOND_FORMAT.format(date);
result = SECOND_FORMAT.format(cal.getTime());
}
} else if (resolution == Resolution.MILLISECOND) { } else if (resolution == Resolution.MILLISECOND) {
synchronized (MILLISECOND_FORMAT) { return MILLISECOND_FORMAT.format(date);
result = MILLISECOND_FORMAT.format(cal.getTime());
} }
} else {
throw new IllegalArgumentException("unknown resolution " + resolution); throw new IllegalArgumentException("unknown resolution " + resolution);
} }
return result;
}
/** /**
* Converts a string produced by <code>timeToString</code> or * Converts a string produced by <code>timeToString</code> or
@ -135,7 +120,7 @@ public class DateTools {
* @throws ParseException if <code>dateString</code> is not in the * @throws ParseException if <code>dateString</code> is not in the
* expected format * expected format
*/ */
public static long stringToTime(String dateString) throws ParseException { public static synchronized long stringToTime(String dateString) throws ParseException {
return stringToDate(dateString).getTime(); return stringToDate(dateString).getTime();
} }
@ -149,41 +134,24 @@ public class DateTools {
* @throws ParseException if <code>dateString</code> is not in the * @throws ParseException if <code>dateString</code> is not in the
* expected format * expected format
*/ */
public static Date stringToDate(String dateString) throws ParseException { public static synchronized Date stringToDate(String dateString) throws ParseException {
Date date;
if (dateString.length() == 4) { if (dateString.length() == 4) {
synchronized (YEAR_FORMAT) { return YEAR_FORMAT.parse(dateString);
date = YEAR_FORMAT.parse(dateString);
}
} else if (dateString.length() == 6) { } else if (dateString.length() == 6) {
synchronized (MONTH_FORMAT) { return MONTH_FORMAT.parse(dateString);
date = MONTH_FORMAT.parse(dateString);
}
} else if (dateString.length() == 8) { } else if (dateString.length() == 8) {
synchronized (DAY_FORMAT) { return DAY_FORMAT.parse(dateString);
date = DAY_FORMAT.parse(dateString);
}
} else if (dateString.length() == 10) { } else if (dateString.length() == 10) {
synchronized (HOUR_FORMAT) { return HOUR_FORMAT.parse(dateString);
date = HOUR_FORMAT.parse(dateString);
}
} else if (dateString.length() == 12) { } else if (dateString.length() == 12) {
synchronized (MINUTE_FORMAT) { return MINUTE_FORMAT.parse(dateString);
date = MINUTE_FORMAT.parse(dateString);
}
} else if (dateString.length() == 14) { } else if (dateString.length() == 14) {
synchronized (SECOND_FORMAT) { return SECOND_FORMAT.parse(dateString);
date = SECOND_FORMAT.parse(dateString);
}
} else if (dateString.length() == 17) { } else if (dateString.length() == 17) {
synchronized (MILLISECOND_FORMAT) { return MILLISECOND_FORMAT.parse(dateString);
date = MILLISECOND_FORMAT.parse(dateString);
} }
} else {
throw new ParseException("Input is not valid date string: " + dateString, 0); throw new ParseException("Input is not valid date string: " + dateString, 0);
} }
return date;
}
/** /**
* Limit a date's resolution. For example, the date <code>2004-09-21 13:50:11</code> * Limit a date's resolution. For example, the date <code>2004-09-21 13:50:11</code>
@ -194,7 +162,7 @@ public class DateTools {
* @return the date with all values more precise than <code>resolution</code> * @return the date with all values more precise than <code>resolution</code>
* set to 0 or 1 * 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)); return new Date(round(date.getTime(), resolution));
} }
@ -208,47 +176,42 @@ public class DateTools {
* @return the date with all values more precise than <code>resolution</code> * @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 * set to 0 or 1, expressed as milliseconds since January 1, 1970, 00:00:00 GMT
*/ */
public static long round(long time, Resolution resolution) { public static synchronized long round(long time, Resolution resolution) {
Calendar cal = Calendar.getInstance(GMT); calInstance.setTimeInMillis(time);
// protected in JDK's prior to 1.4
//cal.setTimeInMillis(time);
cal.setTime(new Date(time));
if (resolution == Resolution.YEAR) { if (resolution == Resolution.YEAR) {
cal.set(Calendar.MONTH, 0); calInstance.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1); calInstance.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0); calInstance.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0); calInstance.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0); calInstance.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0); calInstance.set(Calendar.MILLISECOND, 0);
} else if (resolution == Resolution.MONTH) { } else if (resolution == Resolution.MONTH) {
cal.set(Calendar.DAY_OF_MONTH, 1); calInstance.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0); calInstance.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0); calInstance.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0); calInstance.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0); calInstance.set(Calendar.MILLISECOND, 0);
} else if (resolution == Resolution.DAY) { } else if (resolution == Resolution.DAY) {
cal.set(Calendar.HOUR_OF_DAY, 0); calInstance.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0); calInstance.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0); calInstance.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0); calInstance.set(Calendar.MILLISECOND, 0);
} else if (resolution == Resolution.HOUR) { } else if (resolution == Resolution.HOUR) {
cal.set(Calendar.MINUTE, 0); calInstance.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0); calInstance.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0); calInstance.set(Calendar.MILLISECOND, 0);
} else if (resolution == Resolution.MINUTE) { } else if (resolution == Resolution.MINUTE) {
cal.set(Calendar.SECOND, 0); calInstance.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0); calInstance.set(Calendar.MILLISECOND, 0);
} else if (resolution == Resolution.SECOND) { } else if (resolution == Resolution.SECOND) {
cal.set(Calendar.MILLISECOND, 0); calInstance.set(Calendar.MILLISECOND, 0);
} else if (resolution == Resolution.MILLISECOND) { } else if (resolution == Resolution.MILLISECOND) {
// don't cut off anything // don't cut off anything
} else { } else {
throw new IllegalArgumentException("unknown resolution " + resolution); throw new IllegalArgumentException("unknown resolution " + resolution);
} }
return cal.getTime().getTime(); return calInstance.getTimeInMillis();
} }
/** Specifies the time granularity. */ /** Specifies the time granularity. */