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 /**
019 * HybsEntry.java は、å?部に final 定義された文字å?の、key と value を持つ、å?クラスですã?
020 *
021 * 全変数は、public final 宣è¨?�•れており、外部より取得できますが、設定できませんã€?
022 * こã?クラスは、コンストラクタで設定されたキーと設定å?を変える事が出来ませんã€?
023 * よって、デフォルトコンストラクタを持たなã�?�Ÿめã?java.io.Serializable インターフェースは
024 * 持ちません。またã?å†?ƒ¨の値を変更できなã�?‚º、clone() をするå¿?¦�がなã�?�Ÿめã?
025 * java.lang.Cloneable インターフェースも実è£?�—てã�?�¾せんã€?
026 * HybsEntry オブジェクトã?同ä¸??を確保するには、equals( Object ) と、hashCode() メソãƒ?ƒ‰ã‚?
027 * オーバã?ライドしておくå¿?¦�がありますã?同ä¸??の条件は、key と value がã?ともに
028 * String.equals の関係を持てば、æ?立することとしますã?
029 *
030 * @version 4.0
031 * @author Kazuhiko Hasegawa
032 * @since JDK5.0,
033 */
034 public final class HybsEntry implements Comparable<HybsEntry> { // 4.3.3.6 (2008/11/15) Generics警告対å¿?
035 /** å†?ƒ¨のキーæƒ?? */
036 private final String key ;
037 /** å†?ƒ¨のバリューæƒ?? */
038 private final String value ;
039 /** å†?ƒ¨のコメント情報 */
040 private final String comment ; // 4.0.0 (2005/01/31) 追åŠ?
041
042 // 4.0.0 (2005/01/31) private �
043 private final int hCode ;
044 // private static final ValueComparator valueComp = new ValueComparator();
045
046 /**
047 * コンストラクタ
048 * å†?ƒ¨変数への値の設定ã?、このコンストラクターで行われますã?
049 * key への null セãƒ?ƒˆは認められません。value へは、セãƒ?ƒˆできますã?
050 * コメントã?、ゼロæ–?—å?("") で、å?期化されますã?
051 *
052 * @param key キー
053 * @param value 値
054 * @throws IllegalArgumentException key に null がセãƒ?ƒˆされたå?å�?
055 */
056 public HybsEntry( final String key,final String value ) {
057 this( key,value,"" );
058 }
059
060 /**
061 * コンストラクタ
062 * å†?ƒ¨変数への値の設定ã?、このコンストラクターで行われますã?
063 * key への null セãƒ?ƒˆは認められません。value へは、セãƒ?ƒˆできますã?
064 *
065 * @param key キー
066 * @param value 値
067 * @param comment コメンãƒ?
068 * @throws IllegalArgumentException key に null がセãƒ?ƒˆされたå?å�?
069 */
070 public HybsEntry( final String key,final String value,final String comment ) {
071 if( key == null ) {
072 String errMsg = "key への null セãƒ?ƒˆは認められませんã€? ;
073 throw new IllegalArgumentException( errMsg );
074 }
075
076 this.key = key;
077 this.value = value;
078 this.comment = comment;
079 hCode = ( key + ',' + value + '.' + comment ).hashCode();
080 }
081
082 /**
083 * エントリに対応するキーを返しますã?
084 *
085 * @return エントリに対応するキー
086 */
087 public String getKey() { return key; }
088
089 /**
090 * エントリに対応するå?を返しますã?
091 *
092 * @return エントリに対応するå?
093 */
094 public String getValue() { return value; }
095
096 /**
097 * エントリに対応するコメントを返しますã?
098 *
099 * @return エントリに対応するコメンãƒ?
100 */
101 public String getComment() { return comment; }
102
103 /**
104 * HybsEntry の設定されてã�?‚‹値を変更しますã?
105 * これは、設定å?を変更した新しい HybsEntry を作æ?して返しますã?
106 * なおã?value がã?å†?ƒ¨の値と等しã�?™‚(equals がæ?立する時)自åˆ??身を返しますã?
107 *
108 * @param newValue 新しい値
109 *
110 * @return エントリー HybsEntry
111 */
112 public HybsEntry getValue( final String newValue ) {
113 if( ( newValue == null && value == null ) ||
114 ( newValue != null && newValue.equals( value ) ) ) {
115 return this;
116 }
117 else {
118 return new HybsEntry( key,newValue,comment );
119 }
120 }
121
122 /**
123 * 自然比è¼?ƒ¡ソãƒ?ƒ‰
124 * インタフェース Comparable の 実è£?�§すã?
125 * HybsEntryのé ?º�ã?、key のé ?º�であらわされますã?
126 * 同ä¸?eyの場合ã??Œvalue のé ?•ªになりますã?
127 *
128 * @param other 比è¼?¯¾象のObject
129 *
130 * @return こã?オブジェクトがæŒ?®šされたオブジェクトより小さã�??合ã?è²??整数、等しã�??合ã?ゼロ、大きい場合ã?正の整数
131 * @throws ClassCastException æŒ?®šされたオブジェクトがキャストできなã�??合ã?
132 */
133 // public int compareTo( final Object object ) {
134 // HybsEntry other = (HybsEntry)object; // キャスト失敗で、ClassCastException
135 public int compareTo( final HybsEntry other ) { // 4.3.3.6 (2008/11/15) Generics警告対å¿?
136 int comp = key.compareTo( other.key );
137
138 if( comp == 0 ) {
139 if( value != null ) { comp = value.compareTo( other.value ); }
140 else { comp = ( other.value == null ) ? 0 : -1 ; }
141
142 if( comp == 0 ) {
143 comp = comment.compareTo( other.comment );
144 }
145 }
146 return comp ;
147 }
148
149 /**
150 * こã?オブジェクトと他ã?オブジェクトが等しã�?�‹どã�?�‹を示しますã?
151 * インタフェース Comparable の 実è£?�«関連して、å?定義してã�?�¾すã?
152 * HybsEntryは、key が等しくã?かつ valueが同ä¸??場合にã€?
153 * 等しã�?�¨判断されますã?
154 *
155 * @param object 比è¼?¯¾象の参ç?オブジェクãƒ?
156 *
157 * @return 引数にæŒ?®šされたオブジェクトとこã?オブジェクトが等しã�??合ã? true、そã�?�§なã�??合ã? false
158 */
159 @Override
160 public boolean equals( final Object object ) {
161 if( object instanceof HybsEntry ) {
162 HybsEntry other = (HybsEntry)object;
163 return ( key.equals( other.key ) ) &&
164 ( value != null && value.equals( other.value ) );
165 }
166 return false ;
167 }
168
169 /**
170 * オブジェクトã?ハッシュコードå?を返しますã?
171 * こã?メソãƒ?ƒ‰は、java.util.Hashtable によって提供されるような
172 * ハッシュãƒ??ブルで使用するために用意されてã�?�¾すã?
173 * equals( Object ) メソãƒ?ƒ‰をオーバã?ライトしたå?合ã?、hashCode() メソãƒ?ƒ‰ã‚?
174 * å¿?�š 記述するå¿?¦�がありますã?
175 * ここでは、key と value の合æ?したæ–?—å?のハッシュコードå?を返しますã?
176 *
177 * @return こã?オブジェクトã?ハッシュコードå?
178 *
179 */
180 @Override
181 public int hashCode() {
182 return hCode ;
183 }
184
185 /**
186 * オブジェクトã?識別子として?Œ詳細なユーザーæƒ??を返しますã?
187 *
188 * @return 詳細なユーザーæƒ??
189 */
190 @Override
191 public String toString() {
192 return "key=[" + key + "],value=[" + value + "],comment=[" + comment + "]" ;
193 }
194
195 /**
196 * 設定å?のé ?º�を表ã�?Comparator を返しますã?
197 * HybsEntryクラス自身は、key のé ?•ªで自然é ?º�付けを行う、Comparable インターフェースã‚?
198 * 実è£?�—てã�?�¾すã?しかしã?設定å?でソートするå?合ã?、この
199 * Comparator インターフェースを実è£?�—たå?部クラスを使用することでã€?
200 * 対応å?来ますã?
201 *
202 * @return 設定å?のé ?º�を表ã�?Comparator
203 */
204 // public Comparator getValueComparator() {
205 // return valueComp ;
206 // }
207
208 /**
209 * オブジェクトã?識別子として?Œ詳細なユーザーæƒ??を返しますã?
210 *
211 * @return 詳細なユーザーæƒ??
212 */
213 // private static class ValueComparator implements Comparator {
214 // /**
215 // * value のé ?º�付けのために 2 つの引数を比è¼?�—ますã?
216 // * æœ??の引数ã�?2 番目の引数より小さã�??合ã?è²??整数、両方が等しã�??合ã? 0ã€?
217 // * æœ??の引数ã�?2 番目の引数より大きい場合ã?正の整数を返しますã?
218 // *
219 // * @param o1 Object 比è¼?¯¾象のæœ??のオブジェクãƒ?
220 // * @param o2 Objectb比è¼?¯¾象の 2 番目のオブジェクãƒ?
221 // * @return æœ??の引数ã�?2 番目の引数より小さã�??合ã?è²??整数、両方が等しã�??合ã? 0、最初ã?引数ã�?2 番目の引数より大きい場合ã?正の整数
222 // * @throws ClassCastException - 引数の型がこã?コンパレータによる比è¼?‚’妨げる場å�?
223 // */
224 // public int compare(Object o1, Object o2) {
225 // HybsEntry e1 = (HybsEntry)o1 ; // キャスト失敗で、ClassCastException
226 // HybsEntry e2 = (HybsEntry)o2 ; // キャスト失敗で、ClassCastException
227 //
228 // int comp = 0;
229 // if( e1 == null && e2 == null ) { comp = 0; }
230 // else if( e1 == null && e2 != null ) { comp = -1; }
231 // else if( e1 != null && e2 == null ) { comp = 1; }
232 // else if( e1 != null && e2 != null ) {
233 // if( e1.value == null && e2.value == null ) { comp = 0; }
234 // else if( e1.value == null && e2.value != null ) { comp = -1; }
235 // else if( e1.value != null && e2.value == null ) { comp = 1; }
236 // else {
237 // comp = e1.value.compareTo( e2.value );
238 // }
239 // }
240 // return comp ;
241 // }
242 // }
243 }