From 5d5c20193e524a9e378da9102f50b196f6e7d5df Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Fri, 6 Sep 2024 14:21:22 -0700 Subject: [PATCH] Strictly conforming to the spec --- .../tools/tzdb/TzdbZoneRulesProvider.java | 48 ++++++++++--------- test/jdk/sun/util/calendar/zi/Month.java | 38 +++++++-------- test/jdk/sun/util/calendar/zi/RuleDay.java | 31 +++++------- 3 files changed, 54 insertions(+), 63 deletions(-) diff --git a/make/jdk/src/classes/build/tools/tzdb/TzdbZoneRulesProvider.java b/make/jdk/src/classes/build/tools/tzdb/TzdbZoneRulesProvider.java index 4c3e72fe9eac0..760397255b863 100644 --- a/make/jdk/src/classes/build/tools/tzdb/TzdbZoneRulesProvider.java +++ b/make/jdk/src/classes/build/tools/tzdb/TzdbZoneRulesProvider.java @@ -364,33 +364,35 @@ int parseYear(String year, int defaultYear) { } Month parseMonth(String mon) { - switch (mon) { - case "Jan", "January": return Month.JANUARY; - case "Feb", "February": return Month.FEBRUARY; - case "Mar", "March": return Month.MARCH; - case "Apr", "April": return Month.APRIL; - case "May": return Month.MAY; - case "Jun", "June": return Month.JUNE; - case "Jul", "July": return Month.JULY; - case "Aug", "August": return Month.AUGUST; - case "Sep", "September": return Month.SEPTEMBER; - case "Oct", "October": return Month.OCTOBER; - case "Nov", "November": return Month.NOVEMBER; - case "Dec", "December": return Month.DECEMBER; - } + int len = mon.length(); + + if (mon.regionMatches(true, 0, "January", 0, len)) return Month.JANUARY; + if (mon.regionMatches(true, 0, "February", 0, len)) return Month.FEBRUARY; + if (mon.regionMatches(true, 0, "March", 0, len)) return Month.MARCH; + if (mon.regionMatches(true, 0, "April", 0, len)) return Month.APRIL; + if (mon.regionMatches(true, 0, "May", 0, len)) return Month.MAY; + if (mon.regionMatches(true, 0, "June", 0, len)) return Month.JUNE; + if (mon.regionMatches(true, 0, "July", 0, len)) return Month.JULY; + if (mon.regionMatches(true, 0, "August", 0, len)) return Month.AUGUST; + if (mon.regionMatches(true, 0, "September", 0, len)) return Month.SEPTEMBER; + if (mon.regionMatches(true, 0, "October", 0, len)) return Month.OCTOBER; + if (mon.regionMatches(true, 0, "November", 0, len)) return Month.NOVEMBER; + if (mon.regionMatches(true, 0, "December", 0, len)) return Month.DECEMBER; + throw new IllegalArgumentException("Unknown month: " + mon); } DayOfWeek parseDayOfWeek(String dow) { - switch (dow) { - case "Mon", "Monday": return DayOfWeek.MONDAY; - case "Tue", "Tuesday": return DayOfWeek.TUESDAY; - case "Wed", "Wednesday": return DayOfWeek.WEDNESDAY; - case "Thu", "Thursday": return DayOfWeek.THURSDAY; - case "Fri", "Friday": return DayOfWeek.FRIDAY; - case "Sat", "Saturday": return DayOfWeek.SATURDAY; - case "Sun", "Sunday": return DayOfWeek.SUNDAY; - } + int len = dow.length(); + + if (dow.regionMatches(true, 0, "Monday", 0, len)) return DayOfWeek.MONDAY; + if (dow.regionMatches(true, 0, "Tuesday", 0, len)) return DayOfWeek.TUESDAY; + if (dow.regionMatches(true, 0, "Wednesday", 0, len)) return DayOfWeek.WEDNESDAY; + if (dow.regionMatches(true, 0, "Thursday", 0, len)) return DayOfWeek.THURSDAY; + if (dow.regionMatches(true, 0, "Friday", 0, len)) return DayOfWeek.FRIDAY; + if (dow.regionMatches(true, 0, "Saturday", 0, len)) return DayOfWeek.SATURDAY; + if (dow.regionMatches(true, 0, "Sunday", 0, len)) return DayOfWeek.SUNDAY; + throw new IllegalArgumentException("Unknown day-of-week: " + dow); } diff --git a/test/jdk/sun/util/calendar/zi/Month.java b/test/jdk/sun/util/calendar/zi/Month.java index 6e1573588b921..bab909f763787 100644 --- a/test/jdk/sun/util/calendar/zi/Month.java +++ b/test/jdk/sun/util/calendar/zi/Month.java @@ -21,11 +21,6 @@ * questions. */ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * Month enum handles month related manipulation. * @@ -47,15 +42,6 @@ enum Month { private final String abbr; - private static final Map abbreviations - = new HashMap(12); - - static { - for (Month m : Month.values()) { - abbreviations.put(m.abbr, m); - } - } - private Month(String abbr) { this.abbr = abbr; } @@ -70,14 +56,22 @@ int value() { * @return the Month value */ static Month parse(String name) { - if (name.length() > 3) { - name = name.substring(0, 3); - } - Month m = abbreviations.get(name); - if (m != null) { - return m; - } - return null; + int len = name.length(); + + if (name.regionMatches(true, 0, "January", 0, len)) return Month.JANUARY; + if (name.regionMatches(true, 0, "February", 0, len)) return Month.FEBRUARY; + if (name.regionMatches(true, 0, "March", 0, len)) return Month.MARCH; + if (name.regionMatches(true, 0, "April", 0, len)) return Month.APRIL; + if (name.regionMatches(true, 0, "May", 0, len)) return Month.MAY; + if (name.regionMatches(true, 0, "June", 0, len)) return Month.JUNE; + if (name.regionMatches(true, 0, "July", 0, len)) return Month.JULY; + if (name.regionMatches(true, 0, "August", 0, len)) return Month.AUGUST; + if (name.regionMatches(true, 0, "September", 0, len)) return Month.SEPTEMBER; + if (name.regionMatches(true, 0, "October", 0, len)) return Month.OCTOBER; + if (name.regionMatches(true, 0, "November", 0, len)) return Month.NOVEMBER; + if (name.regionMatches(true, 0, "December", 0, len)) return Month.DECEMBER; + + throw new IllegalArgumentException("Unknown month: " + name); } /** diff --git a/test/jdk/sun/util/calendar/zi/RuleDay.java b/test/jdk/sun/util/calendar/zi/RuleDay.java index 75d77976be1a8..9cd81c1e5246f 100644 --- a/test/jdk/sun/util/calendar/zi/RuleDay.java +++ b/test/jdk/sun/util/calendar/zi/RuleDay.java @@ -21,11 +21,6 @@ * questions. */ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * RuleDay class represents the value of the "ON" field. The day of * week values start from 1 following the {@link java.util.Calendar} @@ -34,13 +29,6 @@ * @since 1.4 */ class RuleDay { - private static final Map abbreviations = new HashMap(7); - static { - for (DayOfWeek day : DayOfWeek.values()) { - abbreviations.put(day.getAbbr(), day); - } - } - private String dayName = null; private DayOfWeek dow; private boolean lastOne = false; @@ -166,16 +154,23 @@ String getDayOfWeekForSimpleTimeZone() { return sign + toString(d); } - private static DayOfWeek getDOW(String abbr) { - if (abbr.length() > 3) { - abbr = abbr.substring(0, 3); - } - return abbreviations.get(abbr); + private static DayOfWeek getDOW(String name) { + int len = name.length(); + + if (name.regionMatches(true, 0, "Monday", 0, len)) return DayOfWeek.MONDAY; + if (name.regionMatches(true, 0, "Tuesday", 0, len)) return DayOfWeek.TUESDAY; + if (name.regionMatches(true, 0, "Wednesday", 0, len)) return DayOfWeek.WEDNESDAY; + if (name.regionMatches(true, 0, "Thursday", 0, len)) return DayOfWeek.THURSDAY; + if (name.regionMatches(true, 0, "Friday", 0, len)) return DayOfWeek.FRIDAY; + if (name.regionMatches(true, 0, "Saturday", 0, len)) return DayOfWeek.SATURDAY; + if (name.regionMatches(true, 0, "Sunday", 0, len)) return DayOfWeek.SUNDAY; + + throw new IllegalArgumentException("Unknown day-of-week: " + name); } /** * Converts the specified day of week value to the day-of-week - * name defined in {@link java.util.Calenda}. + * name defined in {@link java.util.Calendar}. * @param dow 1-based day of week value * @return the Calendar day of week name with "Calendar." prefix. * @throws IllegalArgumentException if the specified dow value is out of range.