001 /*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016 package org.opengion.fukurou.util;
017
018 import java.util.Map ;
019 import java.util.LinkedHashMap ;
020 import java.util.Locale;
021 import java.util.Iterator;
022
023 /**
024 * EnumType.java ã¯ã€å?通的ã«ä½¿ç”¨ã•れã‚?æ–?—åž‹é¸æŠžãƒ•ã‚£ãƒ¼ãƒ«ãƒ‰ã‚’ç°¡ç´?Œ–ã™ã‚‹ã‚¯ãƒ©ã‚¹ã§ã™ã?
025 * JDK5.0 より導å?ã•れã?enum ã«é¡žä¼¼ã®æ©Ÿè?ã‚’æä¾›ã—ã¾ã™ãŒã€å?部çš?«ã‚ˆã‚Šç‰¹åŒ–ã—ã?
026 * 機è?ã‚’æä¾›ã—ã¾ã™ã?
027 * 具体的ã«ã¯ã€ãƒ‡ãƒãƒƒã‚°æƒ??ã®ç°¡æ˜“å?åŠ›ã‚„ã€æ–‡å—å?入力時ã®åŒ?«é–¢ä¿‚ãƒã‚§ãƒ?‚¯ã€?
028 * ãƒ?ƒ•ォルトå?(åˆæœŸå€¤)ã®ç™»éŒ²ãªã©ã§ã™ã?
029 * åˆæœŸå€¤ã«ã¯ã€String,int,boolean ã®?“ã‚¿ã‚¤ãƒ—ãŒæŒ?®šã§ãã¾ã™ã?
030 *
031 * @version 4.0
032 * @author Kazuhiko Hasegawa
033 * @since JDK5.0,
034 */
035 public final class EnumType<T extends Comparable<T>> { // 4.3.3.6 (2008/11/15) Genericsè¦å‘Šå¯¾å¿?
036 /** シスãƒ?ƒ ä¾å˜ã?改行記å·ã‚’ã‚»ãƒ?ƒˆã—ã¾ã™ã? */
037 private static final String CR = System.getProperty("line.separator");
038
039 /** ?˜ã¤åˆ??スペã?スã§ã™ã? */
040 private static final String SPACE = " " ;
041
042 private final Map<String,Types<T>> typemap = new LinkedHashMap<String,Types<T>>();
043
044 private final String title ;
045 private final T defVal ;
046
047 /**
048 * タイトルã¨åˆæœŸå€¤ã‚’指定ã—ã¦æ§‹ç¯‰ã™ã‚?コンストラクター
049 * nval メソãƒ?ƒ‰ã‚’使用ã™ã‚‹å ´åˆã«ã€åˆ©ç”¨ã—ã¾ã™ã?
050 *
051 * @param title タイトル
052 * @param val åˆæœŸå€¤
053 */
054 public EnumType( final String title,final T val ) {
055 this.title = title ;
056 defVal = val ;
057 }
058
059 /**
060 * ã‚ーã¨ãã?説æ˜?メãƒ?‚»ãƒ¼ã‚¸)を追åŠ?—ã¾ã™ã?
061 * å†?ƒ¨ã‚ャãƒ?‚·ãƒ¥(Map)ã«è¿½åŠ?—ã¾ã™ã?
062 * 通常ã®ãƒ¡ã‚½ãƒ?ƒ‰åãªã‚‰ã?put ã?add ã§ã™ãŒã€return ã«
063 * 自åˆ??身を記述ã§ãã‚‹ãŸã‚ã€å?期è¨å®?コンストラクタ)?‹å?è¨å®šã‚’
064 * 連続ã—ã¦è¨˜è¿°ã™ã‚‹ã“ã¨ãŒå?æ¥ã‚?append メソãƒ?ƒ‰ã«ã¡ãªã‚“ã§å‘½åã—ã¦ã?¾ã™ã?
065 *
066 * @param key ã‚ー
067 * @param msg メãƒ?‚»ãƒ¼ã‚¸
068 *
069 * @return 自�?身
070 */
071 public EnumType<T> append( final T key, final String msg ) {
072 typemap.put( String.valueOf( key ).toUpperCase( Locale.JAPAN ),new Types<T>( key,msg ) );
073 return this ;
074 }
075
076 /**
077 * æ–?—å?相当ã?è¨å®šå?よりã€å¯¾å¿œã™ã‚?T オブジェクトを返ã—ã¾ã™ã?
078 * T ã¯ã€ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ä½œæ?時ã«ã€new EnumType<T> ã§æŒ?®šã™ã‚‹ã‚ªãƒ–ジェクトã§ã™ã?
079 * å¼•æ•°ã®æ–?—å?ã¯ã€?String.valueOf( T ) ã§å¾—られるæ–?—å?ã§ã™ã?
080 * 引数ãŒã?null ã‹ã?é•·ã•ゼãƒã®æ–?—å?ã®å ´åˆã?ã€ã‚³ãƒ³ã‚¹ãƒˆãƒ©ã‚¯ã‚¿ã§æŒ?®šã—ã?
081 * åˆæœŸå€¤ãŒè¿”ã•れã¾ã™ã?
082 * T ã« Boolean ã‚?Integer を指定ã—ã¦ã?‚‹å ´åˆã?ã€ã‚¢ãƒ³ãƒœã‚¯ã‚·ãƒ³ã‚°æ©Ÿè?ã«ã‚ˆã‚Šã€?
083 * boolean ã‚?int ã«è‡ªå‹•çš„ã«ã‚ャストã•れã¾ã™ã?
084 *
085 * @param strKey æ–?—å?相当ã?è¨å®šå?
086 *
087 * @return strKeyã«å¯¾å¿œã™ã‚‹ã‚ªãƒ–ジェクãƒ?
088 * @throws IllegalArgumentException 引数ãŒMapã«å˜åœ¨ã—ãªã‹ã£ãŸå?å?nullã¯OK)
089 */
090 public T nval( final String strKey ) {
091 if( strKey != null && strKey.length() > 0 ) {
092 String upKey = strKey.toUpperCase( Locale.JAPAN );
093 if( typemap.containsKey( upKey ) ) {
094 Types<T> type = typemap.get( upKey );
095 return type.getKey();
096 }
097 else {
098 String errMsg = title + " ç¯?›²è¨å®šã‚¨ãƒ©ãƒ¼"
099 + CR + "引数 [" + strKey + "] ã¯ã€å?部ã«ãƒžãƒƒãƒ—ã•れã¦ã?¾ã›ã‚“ã€?
100 + CR + toString() ;
101 throw new IllegalArgumentException( errMsg );
102 }
103 }
104 return defVal ;
105 }
106
107 /**
108 * åˆæœŸå€¤ã‚’è¿”ã—ã¾ã™ã?
109 * T ã« Boolean ã‚?Integer を指定ã—ã¦ã?‚‹å ´åˆã?ã€ã‚¢ãƒ³ãƒœã‚¯ã‚·ãƒ³ã‚°æ©Ÿè?ã«ã‚ˆã‚Šã€?
110 * boolean ã‚?int ã«è‡ªå‹•çš„ã«ã‚ャストã•れã¾ã™ã?
111 *
112 * @return åˆæœŸå€¤ã‚ªãƒ–ジェクãƒ?
113 */
114 public T getDefault() {
115 return defVal ;
116 }
117
118 /**
119 * è¨å®šã—ã?T ãŒå˜åœ¨ã—ã¦ã?‚‹ã‹ã©ã?‹ã‚’è¿”ã—ã¾ã™ã?
120 * å†?ƒ¨ã«å€¤ã‚’å–り込んã 後ã§ä½¿ç”¨ã™ã‚‹ã€å˜åœ¨ãƒã‚§ãƒ?‚¯ã§ã™ã?
121 * 通常ã€nval ã§ å–り込んã 後ã?ã€ãƒã‚§ãƒ?‚¯ä¸è¦ã§ã™ã?
122 * 引数ã?null ã®å ´åˆã?ã€false ã‚’è¿”ã—ã¾ã™ã?
123 *
124 * @param key T è¨å®šã—ã?Tオブジェクãƒ?
125 *
126 * @return å˜åœ¨ã™ã‚‹:true / å˜åœ¨ã—ãªã?false
127 */
128 public boolean contains( final T key ) {
129 if( key == null ) {
130 return false;
131 }
132 return typemap.containsKey( String.valueOf( key ).toUpperCase( Locale.JAPAN ) );
133 }
134
135 /**
136 * å†?ƒ¨ã®æ–?—å?表ç¾ã‚’è¿”ã—ã¾ã™ã?
137 *
138 * @return å†?ƒ¨ã®æ–?—å?表ç¾
139 */
140 @Override
141 public String toString() {
142 // Types<T>[] types = typemap.values().toArray( new Types[typemap.size()] );
143 // StringBuilder buf = new StringBuilder();
144 // buf.append( CR );
145 // for( int i=0; i<types.length; i++ ) {
146 // buf.append( SPACE ).append( types[i].toString() ).append( CR );
147 // }
148 // return buf.toString();
149
150 // 4.3.3.6 (2008/11/15) Genericsè¦å‘Šå¯¾å¿?, toArray ã‹ã‚‰ã€Iterator ã«ã€å¤‰æ›´
151 Iterator<Types<T>> ite = typemap.values().iterator();
152 StringBuilder buf = new StringBuilder();
153 buf.append( CR );
154 while( ite.hasNext() ) {
155 buf.append( SPACE ).append( ite.next() ).append( CR );
156 }
157 return buf.toString();
158 }
159
160 /**
161 * å†?ƒ¨ã‚ªãƒ–ジェクトを管ç?™ã‚‹ç‚ºã® インナã?クラス
162 * ã‚ーオブジェクトã¨ãã?説明ã?ペアを管ç?—ã¾ã™ã?
163 *
164 * @version 4.0
165 * @author Kazuhiko Hasegawa
166 * @since JDK5.0,
167 */
168 private static class Types<T> {
169 private final T key ;
170 private final String msg ;
171
172 /**
173 * ã‚ーã¨èª¬æ˜Žã‚’æŒ?®šã—ãŸã‚³ãƒ³ã‚¹ãƒˆãƒ©ã‚¯ã‚¿
174 *
175 * @param key T ã‚ーオブジェクãƒ?
176 * @param msg 説�
177 * @throws IllegalArgumentException ã‚ーオブジェクトãŒnullã®å ´å?
178 */
179 public Types( final T key,final String msg ) {
180 if( key == null ) {
181 String errMsg = "key ã«ã¯ null ã‚’ã‚»ãƒ?ƒˆã§ãã¾ã›ã‚“ã€? ;
182 throw new IllegalArgumentException( errMsg );
183 }
184
185 this.key = key;
186 this.msg = msg;
187 }
188
189 /**
190 * ã‚ーオブジェクトを返ã—ã¾ã™ã?
191 *
192 * @return key T ã‚ーオブジェクãƒ?
193 */
194 public T getKey() { return key; }
195
196 /**
197 * 説明を返ã—ã¾ã™ã?
198 *
199 * @return msg String 説�
200 */
201 public String getMsg() { return msg; }
202
203 /**
204 * å†?ƒ¨ã®æ–?—å?表ç¾ã‚’è¿”ã—ã¾ã™ã?
205 *
206 * @return å†?ƒ¨ã®æ–?—å?表ç¾
207 */
208 public String toString() {
209 return key + " : " + msg ;
210 }
211 }
212 }