1+ /*
2+ * Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com .mapcode ;
18+
19+ import javax .annotation .Nonnull ;
20+
21+ import static com .mapcode .CheckArgs .checkNonnull ;
22+
23+ /**
24+ * This enum defines all alphabets supported for mapcodes. Mapcodes can be safely converted between
25+ * alphabets and fed to the mapcode decoder in the regular ASCII Roman alphabet or any other.
26+ */
27+ public enum Alphabet {
28+ ROMAN (0 ),
29+ GREEK (1 ),
30+ CYRILLIC (2 ),
31+ HEBREW (3 ),
32+ HINDI (4 ),
33+ MALAY (5 ),
34+ GEORGIAN (6 ),
35+ KATAKANA (7 ),
36+ THAI (8 ),
37+ LAO (9 ),
38+ ARMENIAN (10 ),
39+ BENGALI (11 ),
40+ GURMUKHI (12 ),
41+ TIBETAN (13 );
42+
43+ /**
44+ * The numeric code is synonym for the alphanumeric code. It can be used in the decoder
45+ * to define a territory as well.
46+ */
47+ private final int code ;
48+
49+ private Alphabet (final int code ) {
50+ this .code = code ;
51+ }
52+
53+ public int getCode () {
54+ return code ;
55+ }
56+
57+ /**
58+ * Get an alphabet from a numeric code.
59+ *
60+ * @param code Numeric code.
61+ * @return Alphabet.
62+ * @throws UnknownAlphabetException Thrown if code out of range.
63+ */
64+ @ Nonnull
65+ public static Alphabet fromCode (final int code ) throws UnknownAlphabetException {
66+ if ((code >= 0 ) && (code < Alphabet .values ().length )) {
67+ return Alphabet .values ()[code ];
68+ }
69+ throw new UnknownAlphabetException (code );
70+ }
71+
72+ /**
73+ * Return alphabet from a string, which can be a numeric or alpha code.
74+ *
75+ * @param numericOrAlpha Alphabet. May be a numeric or alphanumeric code.
76+ * @return Alphabet.
77+ * @throws UnknownAlphabetException Thrown if incorrect numeric or alphanumeric code.
78+ */
79+ @ Nonnull
80+ public static Alphabet fromString (@ Nonnull final String numericOrAlpha ) throws UnknownAlphabetException {
81+ checkNonnull ("name" , numericOrAlpha );
82+ final String trimmed = numericOrAlpha .trim ().toUpperCase ();
83+ try {
84+ return fromCode (Integer .valueOf (numericOrAlpha ));
85+ } catch (final IllegalArgumentException ignored ) {
86+ // Ignore. Re-try as alpha code.
87+ }
88+ try {
89+ return valueOf (trimmed );
90+ } catch (final IllegalArgumentException ignored ) {
91+ throw new UnknownAlphabetException (trimmed );
92+ }
93+ }
94+
95+ /**
96+ * Static checking of the static data structures.
97+ */
98+ static {
99+ int i = 0 ;
100+ for (final Alphabet alphabet : Alphabet .values ()) {
101+ if (Alphabet .values ()[i ].code != i ) {
102+ throw new ExceptionInInitializerError ("Incorrect alphabet code: " + alphabet + ".code should be " + i );
103+ }
104+ ++i ;
105+ }
106+ }
107+ }
0 commit comments