1 /*
2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package build.tools.cldrconverter;
27 
28 import java.util.Locale;
29 
30 /**
31  * Constants for the Calendars supported by JRE.
32  * Note that "GENERIC" calendar data will NOT be extracted to JDK's ResourceBundles.
33  */
34 enum CalendarType {
35     GENERIC, GREGORIAN("gregory"), BUDDHIST, JAPANESE, ROC,
36     ISLAMIC, ISLAMIC_CIVIL("islamic-civil"), ISLAMIC_UMALQURA("islamic-umalqura");
37 
38     private static final int[][] ERA_DATA = {
39         // start index, array length
40         {0,   2},   // generic
41         {0,   2},   // gregorian
42         {0,   1},   // buddhist
43         {232, 5},   // japanese (eras from Meiji)
44         {0,   2},   // roc (Minguo)
45         {0,   1},   // islamic (Hijrah)
46         {0,   1},   // islamic-civil (same as islamic)
47         {0,   1},   // islamic-umalqura
48     };
49 
50     private final String lname; // lowercase name
51     private final String uname; // unicode key name (e.g., "gregory" for GREGORIAN)
52 
CalendarType()53     private CalendarType() {
54         this(null);
55     }
56 
CalendarType(String uname)57     private CalendarType(String uname) {
58         String lname = name().toLowerCase(Locale.ROOT);
59         if (lname.startsWith("islamic_")) {
60             lname = lname.replace('_', '-');
61         }
62         this.lname = lname;
63         this.uname = (uname != null) ? uname : lname;
64     }
65 
lname()66     String lname() {
67         return lname;
68     }
69 
uname()70     String uname() {
71         return uname;
72     }
73 
keyElementName()74     String keyElementName() {
75         return (this == GREGORIAN) ? "" : lname + ".";
76     }
77 
normalizeEraIndex(int index)78     int normalizeEraIndex(int index) {
79         index -= ERA_DATA[ordinal()][0];
80         if (index >= ERA_DATA[ordinal()][1]) {
81             index = -1;
82         }
83         return index;
84     }
85 
getEraLength(String name)86     int getEraLength(String name) {
87         return ERA_DATA[ordinal()][1];
88     }
89 
forName(String name)90     static CalendarType forName(String name) {
91         for (CalendarType type : values()) {
92             if (type.lname.equals(name) || type.uname.equals(name)) {
93                 return type;
94             }
95         }
96         return null;
97     }
98 }
99