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 }