OSDN Git Service

パッケージ変更。テスト整備。
[mikutoga/TogaGem.git] / src / main / java / jp / sfjp / mikutoga / corelib / I18nText.java
1 /*
2  * internationalization text
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.corelib;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Locale;
14 import java.util.Map;
15 import java.util.Set;
16
17 /**
18  * 多言語のバリアントを持つ文字列情報。
19  * 言語はISO639言語コードで区別される。
20  * <ul>
21  * <li>プライマリ:識別子にはこちらを使う。
22  * 基本はMMD発祥の地に敬意を表して日本語。
23  * <li>グローバル:基本は英語。UCS Basic-Latinオンリーの貧弱な言語環境でも
24  * 読める文字列が望ましい。
25  * <li>その他:必要に応じて好きな言語を。
26  * </ul>
27  */
28 public class I18nText implements CharSequence {
29
30     /** プライマリ言語のロケール。 */
31     public static final Locale LOCALE_PRIMARY = Locale.JAPANESE;
32     /** プライマリ言語のISO639言語コード。 */
33     public static final String CODE639_PRIMARY =
34             LOCALE_PRIMARY.getLanguage();
35
36     /** グローバル言語のロケール。 */
37     public static final Locale LOCALE_GLOBAL = Locale.ENGLISH;
38     /** グローバル言語のISO639言語コード。 */
39     public static final String CODE639_GLOBAL =
40             LOCALE_GLOBAL.getLanguage();
41
42     static{
43         assert "ja".equals(CODE639_PRIMARY);
44         assert "en".equals(CODE639_GLOBAL);
45     }
46
47
48     /**
49      * キーはISO639、値は多言語テキスト。
50      */
51     private final Map<String, String> nameMap =
52             new HashMap<String, String>();
53
54
55     /**
56      * コンストラクタ。
57      */
58     public I18nText(){
59         super();
60         return;
61     }
62
63     /**
64      * プライマリ文字列の登録。
65      * @param seq プライマリ文字列。nullの場合は削除動作
66      */
67     public void setPrimaryText(CharSequence seq){
68         setI18nText(CODE639_PRIMARY, seq);
69         return;
70     }
71
72     /**
73      * グローバル文字列の登録。
74      * @param seq グローバル文字列。nullの場合は削除動作
75      */
76     public void setGlobalText(CharSequence seq){
77         setI18nText(CODE639_GLOBAL, seq);
78         return;
79     }
80
81     /**
82      * 任意のロケールに関連付けられた文字列の登録。
83      * @param locale ロケール
84      * @param seq 文字列。nullの場合は削除動作
85      * @throws NullPointerException ロケール引数がnull
86      */
87     public void setI18nText(Locale locale, CharSequence seq)
88             throws NullPointerException{
89         String code639 = locale.getLanguage();
90         setI18nText(code639, seq);
91         return;
92     }
93
94     /**
95      * 任意の言語コードに関連付けられた文字列の登録。
96      * @param code639 ISO639言語コード
97      * @param seq 文字列。nullの場合は削除動作
98      * @throws NullPointerException 言語コードがnull
99      */
100     public void setI18nText(String code639, CharSequence seq)
101             throws NullPointerException{
102         if(code639 == null) throw new NullPointerException();
103
104         if(seq != null){
105             String text = seq.toString();
106             this.nameMap.put(code639, text);
107         }else{
108             this.nameMap.remove(code639);
109         }
110
111         return;
112     }
113
114     /**
115      * プライマリ文字列を返す。
116      * @return 文字列。見つからなければnullを返す。
117      */
118     public String getPrimaryText(){
119         String result = getI18nText(CODE639_PRIMARY);
120         return result;
121     }
122
123     /**
124      * グローバル文字列を返す。
125      * @return 文字列。見つからなければnullを返す。
126      */
127     public String getGlobalText(){
128         String result = getI18nText(CODE639_GLOBAL);
129         return result;
130     }
131
132     /**
133      * ロケールに応じた文字列を返す。
134      * @param locale ロケール
135      * @return 文字列。見つからなければnullを返す。
136      * @throws NullPointerException 引数がnull
137      */
138     public String getI18nText(Locale locale) throws NullPointerException{
139         String code639 = locale.getLanguage();
140         String result = getI18nText(code639);
141         return result;
142     }
143
144     /**
145      * 言語コードに応じた文字列を返す。
146      * @param code639 ISO639言語コード
147      * @return 文字列。見つからなければnullを返す。
148      * @throws NullPointerException 引数がnull
149      */
150     public String getI18nText(String code639) throws NullPointerException{
151         if(code639 == null) throw new NullPointerException();
152         String result = this.nameMap.get(code639);
153         return result;
154     }
155
156     /**
157      * プライマリ文字列を返す。
158      * <p>見つからなければグローバル文字列を返す。
159      * それでも見つからなければ長さ0の空文字列を返す。
160      * <p>※決してnullは返さない。
161      * @return 文字列
162      */
163     public String getText(){
164         String result;
165
166         result = getPrimaryText();
167
168         if(result == null){
169             result = getGlobalText();
170         }
171
172         if(result == null){
173             result = "";
174         }
175
176         return result;
177     }
178
179     /**
180      * 実行環境のデフォルトロケールに応じた文字列を返す。
181      * <p>見つからなければグローバル文字列、プライマリ文字列の順に返す。
182      * それでも見つからなければ適当な言語コードの文字列を返す。
183      * それでも見つからなければ長さ0の空文字列を返す。
184      * <p>デフォルトロケールの確認はその都度行われる。
185      * <p>※決してnullは返さない。
186      * @return 文字列
187      */
188     public String getLocalizedText(){
189         Locale locale = Locale.getDefault();
190         String langCode = locale.getLanguage();
191
192         String result;
193
194         result = this.nameMap.get(langCode);
195
196         if(result == null){
197             result = this.nameMap.get(CODE639_GLOBAL);
198         }
199
200         if(result == null){
201             result = this.nameMap.get(CODE639_PRIMARY);
202         }
203
204         if(result == null){
205             Set<String> langSet = this.nameMap.keySet();
206             for(String lang : langSet){
207                 result = this.nameMap.get(lang);
208                 if(result != null) break;
209             }
210         }
211
212         if(result == null){
213             result = "";
214         }
215
216         return result;
217     }
218
219     /**
220      * 全言語の文字列を削除する。
221      */
222     public void clearI18nText(){
223         this.nameMap.clear();
224         return;
225     }
226
227     /**
228      * 登録済みの全ISO639言語コードリストを返す。
229      * 優先度はプライマリ、グローバル、その他の順。
230      * @return 全ISO639言語コード
231      */
232     public List<String> lang639CodeList(){
233         Set<String> set = this.nameMap.keySet();
234         List<String> result = new ArrayList<String>(set.size());
235
236         for(String lang : set){
237             if(lang.equals(CODE639_PRIMARY)) result.add(lang);
238         }
239
240         for(String lang : set){
241             if(lang.equals(CODE639_GLOBAL)) result.add(lang);
242         }
243
244         for(String lang : set){
245             if(lang.equals(CODE639_PRIMARY)) continue;
246             if(lang.equals(CODE639_GLOBAL)) continue;
247             result.add(lang);
248         }
249
250         return result;
251     }
252
253     /**
254      * プライマリ文字列が登録されているか判定する。
255      * @return 登録されていればtrue
256      */
257     public boolean hasPrimaryText(){
258         boolean result = this.nameMap.containsKey(CODE639_PRIMARY);
259         return result;
260     }
261
262     /**
263      * グローバル文字列が登録されているか判定する。
264      * @return 登録されていればtrue
265      */
266     public boolean hasGlobalText(){
267         boolean result = this.nameMap.containsKey(CODE639_GLOBAL);
268         return result;
269     }
270
271     /**
272      * {@inheritDoc}
273      * {@link #getText()}仕様に準ずる。
274      * @param index {@inheritDoc}
275      * @return {@inheritDoc}
276      */
277     @Override
278     public char charAt(int index){
279         String text = getText();
280         char result = text.charAt(index);
281         return result;
282     }
283
284     /**
285      * {@inheritDoc}
286      * {@link #getText()}仕様に準ずる。
287      * @return {@inheritDoc}
288      */
289     @Override
290     public int length(){
291         String text = getText();
292         int result = text.length();
293         return result;
294     }
295
296     /**
297      * {@inheritDoc}
298      * {@link #getText()}仕様に準ずる。
299      * @param start {@inheritDoc}
300      * @param end {@inheritDoc}
301      * @return {@inheritDoc}
302      */
303     @Override
304     public CharSequence subSequence(int start, int end){
305         String text = getText();
306         CharSequence result = text.subSequence(start, end);
307         return result;
308     }
309
310     /**
311      * {@inheritDoc}
312      * {@link #getText()}仕様に準ずる。
313      * @return {@inheritDoc}
314      */
315     @Override
316     public String toString(){
317         return getText();
318     }
319
320 }