Small style stuff
[zxing.git] / core / src / com / google / zxing / common / CharacterSetECI.java
1 /*
2  * Copyright 2008 ZXing authors
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.google.zxing.common;
18
19 import java.util.Hashtable;
20
21 /**
22  * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1
23  * of ISO 18004.
24  *
25  * @author Sean Owen
26  */
27 public final class CharacterSetECI extends ECI {
28
29   private static Hashtable VALUE_TO_ECI;
30   private static Hashtable NAME_TO_ECI;
31
32   private static void initialize() {
33     VALUE_TO_ECI = new Hashtable(29);
34     NAME_TO_ECI = new Hashtable(29);
35     // TODO figure out if these values are even right!
36     addCharacterSet(0, "Cp437");
37     addCharacterSet(1, new String[] {"ISO8859_1", "ISO-8859-1"});
38     addCharacterSet(2, "Cp437");
39     addCharacterSet(3, new String[] {"ISO8859_1", "ISO-8859-1"});
40     addCharacterSet(4, "ISO8859_2");
41     addCharacterSet(5, "ISO8859_3");
42     addCharacterSet(6, "ISO8859_4");
43     addCharacterSet(7, "ISO8859_5");
44     addCharacterSet(8, "ISO8859_6");
45     addCharacterSet(9, "ISO8859_7");
46     addCharacterSet(10, "ISO8859_8");
47     addCharacterSet(11, "ISO8859_9");
48     addCharacterSet(12, "ISO8859_10");
49     addCharacterSet(13, "ISO8859_11");
50     addCharacterSet(15, "ISO8859_13");
51     addCharacterSet(16, "ISO8859_14");
52     addCharacterSet(17, "ISO8859_15");
53     addCharacterSet(18, "ISO8859_16");
54     addCharacterSet(20, new String[] {"SJIS", "Shift_JIS"});
55   }
56
57   private final String encodingName;
58
59   private CharacterSetECI(int value, String encodingName) {
60     super(value);
61     this.encodingName = encodingName;
62   }
63
64   public String getEncodingName() {
65     return encodingName;
66   }
67
68   private static void addCharacterSet(int value, String encodingName) {
69     CharacterSetECI eci = new CharacterSetECI(value, encodingName);
70     VALUE_TO_ECI.put(new Integer(value), eci); // can't use valueOf
71     NAME_TO_ECI.put(encodingName, eci);
72   }
73
74   private static void addCharacterSet(int value, String[] encodingNames) {
75     CharacterSetECI eci = new CharacterSetECI(value, encodingNames[0]);
76     VALUE_TO_ECI.put(new Integer(value), eci); // can't use valueOf
77     for (int i = 0; i < encodingNames.length; i++) {
78       NAME_TO_ECI.put(encodingNames[i], eci);
79     }
80   }
81
82   /**
83    * @param value character set ECI value
84    * @return {@link CharacterSetECI} representing ECI of given value, or null if it is legal but
85    *   unsupported
86    * @throws IllegalArgumentException if ECI value is invalid
87    */
88   public static CharacterSetECI getCharacterSetECIByValue(int value) {
89     if (VALUE_TO_ECI == null) {
90       initialize();
91     }
92     if (value < 0 || value >= 900) {
93       throw new IllegalArgumentException("Bad ECI value: " + value);
94     }
95     return (CharacterSetECI) VALUE_TO_ECI.get(new Integer(value));
96   }
97
98   /**
99    * @param name character set ECI encoding name
100    * @return {@link CharacterSetECI} representing ECI for character encoding, or null if it is legal
101    *   but unsupported
102    */
103   public static CharacterSetECI getCharacterSetECIByName(String name) {
104     if (NAME_TO_ECI == null) {
105       initialize();
106     }
107     return (CharacterSetECI) NAME_TO_ECI.get(name);
108   }
109
110 }