From 787eb9996c0f9b32ae247ba072c048dceaa0a453 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Fri, 10 Apr 2015 12:52:32 -0700 Subject: Libcore: Refactor TimeZone for compile-time initialization Introduce a private inner class for custom-time-zone parsing. This moves the Pattern field which cannot be compile-time initialized. As a result, TimeZone, SimpleTimeZone & ZoneInfo can be initialized in the boot image. Bug: 19542228 Change-Id: I6fc8cb65975e14351a6bfc1ddfac175368efd1eb --- luni/src/main/java/java/util/TimeZone.java | 82 +++++++++++++++++------------- 1 file changed, 46 insertions(+), 36 deletions(-) (limited to 'luni') diff --git a/luni/src/main/java/java/util/TimeZone.java b/luni/src/main/java/java/util/TimeZone.java index 854a4a6..d7adbf2 100644 --- a/luni/src/main/java/java/util/TimeZone.java +++ b/luni/src/main/java/java/util/TimeZone.java @@ -68,7 +68,51 @@ import org.apache.harmony.luni.internal.util.TimezoneGetter; public abstract class TimeZone implements Serializable, Cloneable { private static final long serialVersionUID = 3581463369166924961L; - private static final Pattern CUSTOM_ZONE_ID_PATTERN = Pattern.compile("^GMT[-+](\\d{1,2})(:?(\\d\\d))?$"); + /** + * Helper class to parse a custom timezone. This is in a separate class as regular expressions + * cannot be compile-time initialized, so that static field is separated out from TimeZone + * proper. + */ + private final static class CustomTimeZoneParser { + private static final Pattern CUSTOM_ZONE_ID_PATTERN = + Pattern.compile("^GMT[-+](\\d{1,2})(:?(\\d\\d))?$"); + + private CustomTimeZoneParser() {} + + /** + * Returns a new SimpleTimeZone for an ID of the form "GMT[+|-]hh[[:]mm]", or null. + */ + private static TimeZone getCustomTimeZone(String id) { + Matcher m = CUSTOM_ZONE_ID_PATTERN.matcher(id); + if (!m.matches()) { + return null; + } + + int hour; + int minute = 0; + try { + hour = Integer.parseInt(m.group(1)); + if (m.group(3) != null) { + minute = Integer.parseInt(m.group(3)); + } + } catch (NumberFormatException impossible) { + throw new AssertionError(impossible); + } + + if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { + return null; + } + + char sign = id.charAt(3); + int raw = (hour * 3600000) + (minute * 60000); + if (sign == '-') { + raw = -raw; + } + + String cleanId = String.format("GMT%c%02d:%02d", sign, hour, minute); + return new SimpleTimeZone(raw, cleanId); + } + } /** * The short display name style, such as {@code PDT}. Requests for this @@ -368,7 +412,7 @@ public abstract class TimeZone implements Serializable, Cloneable { // Custom time zone? if (zone == null && id.length() > 3 && id.startsWith("GMT")) { - zone = getCustomTimeZone(id); + zone = CustomTimeZoneParser.getCustomTimeZone(id); } // We never return null; on failure we return the equivalent of "GMT". @@ -376,40 +420,6 @@ public abstract class TimeZone implements Serializable, Cloneable { } /** - * Returns a new SimpleTimeZone for an ID of the form "GMT[+|-]hh[[:]mm]", or null. - */ - private static TimeZone getCustomTimeZone(String id) { - Matcher m = CUSTOM_ZONE_ID_PATTERN.matcher(id); - if (!m.matches()) { - return null; - } - - int hour; - int minute = 0; - try { - hour = Integer.parseInt(m.group(1)); - if (m.group(3) != null) { - minute = Integer.parseInt(m.group(3)); - } - } catch (NumberFormatException impossible) { - throw new AssertionError(impossible); - } - - if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { - return null; - } - - char sign = id.charAt(3); - int raw = (hour * 3600000) + (minute * 60000); - if (sign == '-') { - raw = -raw; - } - - String cleanId = String.format("GMT%c%02d:%02d", sign, hour, minute); - return new SimpleTimeZone(raw, cleanId); - } - - /** * Returns true if {@code timeZone} has the same rules as this time zone. * *

The base implementation returns true if both time zones have the same -- cgit v1.1