src/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java

Print this page
rev 6495 : imported patch 8007038

@@ -23,10 +23,11 @@
  * questions.
  */
 package sun.util.locale.provider;
 
 import static java.util.Calendar.*;
+import static java.util.GregorianCalendar.*;
 import java.util.Comparator;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;

@@ -48,10 +49,13 @@
         this.langtags = langtags;
     }
 
     @Override
     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
+        if (!valueRangeCheck(calendarType, field, value, style)) {
+            return null;
+        }
         String name = null;
         String key = getResourceKey(calendarType, field, style);
         if (key != null) {
             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
             if (strings != null && strings.length > 0) {

@@ -275,6 +279,32 @@
         case NARROW_FORMAT:
             return "Narrows";
         }
         return "Names";
     }
+
+    private boolean valueRangeCheck(String calType, int field, int value, int style) {
+        switch (field) {
+        case MONTH:
+            return value >= JANUARY && value <= UNDECIMBER;
+        case DAY_OF_WEEK:
+            return value >= SUNDAY && value <= SATURDAY;
+        case AM_PM:
+            return value == AM || value == PM;
+        case ERA:
+            switch (calType) {
+            case "gregory":
+                return value == BC || value == AD;
+            case "buddhist": // BC or B.E.
+            case "roc": // Before R.O.C. or R.O.C.
+            case "islamic": // Before AH or AH
+                return value == 0 || value == 1;
+            case "japanese":
+                return value >= 0 && value <= 4; // Seireki, Meiji, Taisho, Showa, or Heisei
+            default:
+                return false; // unknown calendar type
+            }
+        default:
+            return true; // no value range limitation
+        }
+    }
 }