1 /*
   2  * Copyright (c) 2012, 2013, 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  */
  33 enum CalendarType {
  34     GREGORIAN("gregory"), BUDDHIST, JAPANESE, ROC,
  35     ISLAMIC, ISLAMIC_CIVIL("islamicc"), ISLAMIC_UMALQURA("islamic-umalqura");
  36 
  37     private static final int[][] ERA_DATA = {
  38         // start index, array length
  39         {0,   2},   // gregorian
  40         {0,   1},   // buddhist
  41         {232, 5},   // japanese (eras from Meiji)
  42         {0,   2},   // roc (Minguo)
  43         {0,   1},   // islamic (Hijrah)
  44         {0,   1},   // islamicc (same as islamic)
  45         {0,   1},   // islamic-umalqura
  46     };
  47 
  48     private final String lname; // lowercase name
  49     private final String uname; // unicode key name (e.g., "gregory" for GREGORIAN)
  50 
  51     private CalendarType() {
  52         this(null);
  53     }
  54 
  55     private CalendarType(String uname) {
  56         String lname = name().toLowerCase(Locale.ROOT);
  57         if (lname.startsWith("islamic_")) {
  58             lname = lname.replace('_', '-');
  59         }
  60         this.lname = lname;
  61         this.uname = (uname != null) ? uname : lname;
  62     }
  63 
  64     String lname() {
  65         return lname;
  66     }
  67 
  68     String uname() {
  69         return uname;
  70     }
  71 
  72     String keyElementName() {
  73         return (this == GREGORIAN) ? "" : lname + ".";
  74     }
  75 
  76     int normalizeEraIndex(int index) {
  77         index -= ERA_DATA[ordinal()][0];
  78         if (index >= ERA_DATA[ordinal()][1]) {
  79             index = -1;
  80         }
  81         return index;
  82     }
  83 
  84     int getEraLength(String name) {
  85         return ERA_DATA[ordinal()][1];
  86     }
  87 
  88     static CalendarType forName(String name) {
  89         for (CalendarType type : values()) {
  90             if (type.lname.equals(name)) {
  91                 return type;
  92             }
  93         }
  94         return null;
  95     }
  96 }