CalendarType.java revision 12238:22f901cf304f
159415Sobrien/*
259243Sobrien * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
359243Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
459243Sobrien *
559243Sobrien * This code is free software; you can redistribute it and/or modify it
659243Sobrien * under the terms of the GNU General Public License version 2 only, as
759243Sobrien * published by the Free Software Foundation.  Oracle designates this
859243Sobrien * particular file as subject to the "Classpath" exception as provided
959243Sobrien * by Oracle in the LICENSE file that accompanied this code.
1059243Sobrien *
1159243Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1259243Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1359243Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1459243Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1559243Sobrien * accompanied this code).
1659243Sobrien *
1759243Sobrien * You should have received a copy of the GNU General Public License version
1859243Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1959243Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2059243Sobrien *
2159243Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2259243Sobrien * or visit www.oracle.com if you need additional information or have any
2359243Sobrien * questions.
2459243Sobrien */
2559243Sobrien
2659243Sobrienpackage build.tools.cldrconverter;
2759243Sobrien
2859243Sobrienimport java.util.Locale;
2959243Sobrien
3059243Sobrien/**
3159243Sobrien * Constants for the Calendars supported by JRE.
3259243Sobrien * Note that "GENERIC" calendar data will NOT be extracted to JDK's ResourceBundles.
3359243Sobrien */
3459243Sobrienenum CalendarType {
3559243Sobrien    GENERIC, GREGORIAN("gregory"), BUDDHIST, JAPANESE, ROC,
3659243Sobrien    ISLAMIC, ISLAMIC_CIVIL("islamic-civil"), ISLAMIC_UMALQURA("islamic-umalqura");
3759243Sobrien
3859243Sobrien    private static final int[][] ERA_DATA = {
3959415Sobrien        // start index, array length
4059243Sobrien        {0,   2},   // generic
4159243Sobrien        {0,   2},   // gregorian
4259243Sobrien        {0,   1},   // buddhist
4359243Sobrien        {232, 4},   // japanese (eras from Meiji)
4459243Sobrien        {0,   2},   // roc (Minguo)
4559243Sobrien        {0,   1},   // islamic (Hijrah)
4659243Sobrien        {0,   1},   // islamic-civil (same as islamic)
4759243Sobrien        {0,   1},   // islamic-umalqura
4859243Sobrien    };
4959243Sobrien
5059243Sobrien    private final String lname; // lowercase name
5159243Sobrien    private final String uname; // unicode key name (e.g., "gregory" for GREGORIAN)
5259243Sobrien
5359243Sobrien    private CalendarType() {
5459243Sobrien        this(null);
5559243Sobrien    }
5659243Sobrien
5759243Sobrien    private CalendarType(String uname) {
5859243Sobrien        String lname = name().toLowerCase(Locale.ROOT);
5959243Sobrien        if (lname.startsWith("islamic_")) {
6059243Sobrien            lname = lname.replace('_', '-');
6159243Sobrien        }
6259243Sobrien        this.lname = lname;
6359243Sobrien        this.uname = (uname != null) ? uname : lname;
6459243Sobrien    }
6559243Sobrien
6659243Sobrien    String lname() {
6759243Sobrien        return lname;
6859243Sobrien    }
6959243Sobrien
7059243Sobrien    String uname() {
7159243Sobrien        return uname;
7259243Sobrien    }
7359243Sobrien
7459243Sobrien    String keyElementName() {
7559243Sobrien        return (this == GREGORIAN) ? "" : lname + ".";
7659243Sobrien    }
7759243Sobrien
7859243Sobrien    int normalizeEraIndex(int index) {
7959243Sobrien        index -= ERA_DATA[ordinal()][0];
8059243Sobrien        if (index >= ERA_DATA[ordinal()][1]) {
8159243Sobrien            index = -1;
8259243Sobrien        }
8359243Sobrien        return index;
8459243Sobrien    }
8559243Sobrien
8659243Sobrien    int getEraLength(String name) {
8759243Sobrien        return ERA_DATA[ordinal()][1];
8859243Sobrien    }
8959243Sobrien
9059243Sobrien    static CalendarType forName(String name) {
9159243Sobrien        for (CalendarType type : values()) {
9259243Sobrien            if (type.lname.equals(name) || type.uname.equals(name)) {
9359243Sobrien                return type;
9459243Sobrien            }
9559243Sobrien        }
9659243Sobrien        return null;
9759243Sobrien    }
9859243Sobrien}
9959243Sobrien