1 5 package com.teamkonzept.lib; 6 7 import java.util.*; 8 9 12 13 public class TKHashtable extends Hashtable { 14 15 18 public TKHashtable() 19 { 20 } 21 22 25 public TKHashtable( int initialCapacity ) 26 { 27 super( initialCapacity ); 28 } 29 30 34 public TKHashtable(Object key, Object val ) 35 { 36 put( key, val ); 37 } 38 39 43 public TKHashtable( Object init[][] ) 44 { 45 super( init.length ); 46 for( int n=0; n<init.length; n++ ) { 47 put( init[n][0], init[n][1] ); 48 } 49 } 50 51 55 public boolean containsKey( Object keyList[] ) 56 { 57 return get( keyList ) != null; 58 } 59 60 65 public Object get( Object keyList[] ) 66 { 67 Object curr = this; 68 for( int i=0; i<keyList.length; i++ ){ 69 if( curr instanceof Hashtable && ((Hashtable)curr).containsKey( keyList[i] ) ){ 70 curr = ((Hashtable)curr).get( keyList[i] ); 71 } 72 else{ 73 return null; 74 } 75 } 76 return curr; 77 } 78 79 85 public Object put( Object keyList[], Object val ) 86 { 87 Object curr = this; 88 int toScan = keyList.length-1; 89 for( int i=0; i<toScan; i++){ 90 Object key = keyList[i]; 91 Object next = ((Hashtable)curr).get( key ); 92 if( (next == null) || ! (next instanceof Hashtable) ) { 93 next = new TKHashtable(); 94 ((Hashtable)curr).put( key, next ); 95 } 96 curr = next; 97 } 98 return ((Hashtable)curr).put( keyList[toScan], val ); 99 } 100 101 117 public void extend( Object key, Object val ) 118 { 119 Object oldVal = get( key ); 120 121 if( oldVal == null || oldVal instanceof TKNull ) { 122 put( key, val ); 123 } 124 else if( !(val instanceof TKNull) ) { 125 if( (val instanceof Hashtable) && (oldVal instanceof TKHashtable) ) { 126 ((TKHashtable)oldVal).concat( (Hashtable) val ); 127 } 128 else if( oldVal instanceof TKVector ) { 129 ((TKVector)oldVal).concat( val ); 130 } 131 else if( val instanceof TKVector ) { 132 TKVector newVal = (TKVector) ((TKVector)val).clone(); 133 newVal.insertElementAt( oldVal, 0 ); 134 put( key, newVal ); 135 } 136 else { 137 Object [] newValDef = { oldVal, val }; 138 if( oldVal != val && 139 ( oldVal.getClass() != val.getClass() || 140 !oldVal.equals(val) ) 141 ) { 142 put( key, new TKVector( newValDef ) ); 143 } 144 } 145 } 146 } 147 148 152 public void concat( Hashtable hash ) 153 { 154 155 Enumeration keys = hash.keys(); 156 157 while( keys.hasMoreElements() ) { 158 Object key = keys.nextElement(); 159 extend( key, hash.get( key ) ); 160 } 161 } 162 163 168 public void merge( Hashtable hash ) 169 { 170 171 Enumeration keys = hash.keys(); 172 173 while( keys.hasMoreElements() ) { 174 Object key = keys.nextElement(); 175 if( !containsKey( key ) ) { 176 put( key, hash.get( key ) ); 177 } 178 } 179 } 180 181 184 185 public String toPerlString() 186 { 187 return toPerlString(0, true); 188 } 189 190 193 194 public String toPerlString(int indentCount, boolean withNewline) 195 { 196 String result = ""; 197 String indent = ""; 198 if( withNewline ) { 199 for( int i=0; i<=indentCount; i++ ) indent += " "; 200 } 201 202 Enumeration keys = keys(); 203 204 while( keys.hasMoreElements() ) { 205 Object val, key; 206 207 key = keys.nextElement(); 208 val = get( key ); 209 result += indent + "'" + key.toString() + "'=>" 210 + ( val instanceof TKHashtable 211 ? ((TKHashtable)val).toPerlString( indentCount+1, withNewline ) 212 : ( val instanceof TKVector 213 ? ((TKVector)val).toPerlString( indentCount+1, withNewline ) 214 : "'"+val.toString()+"'" 215 ) 216 ) 217 + ( keys.hasMoreElements() ? "," : "" ) 218 + ( withNewline ? "\n" : "" ); 219 } 220 return "{" + (withNewline?"\n":"") + result + indent + "}"; 221 222 } 223 230 public static TKHashtable concat( TKHashtable h1, TKHashtable h2 ) 231 { 232 if( h1 == null ) { 233 if( h2 == null ) { 234 return null; 235 } 236 return (TKHashtable) h2.clone(); 237 } 238 TKHashtable res = (TKHashtable) h1.clone(); 239 res.concat( h2 ); 240 return res; 241 } 242 243 244 251 public static TKHashtable merge( TKHashtable h1, TKHashtable h2 ) 252 { 253 if( h1 == null ) { 254 if( h2 == null ) { 255 return null; 256 } 257 return (TKHashtable) h2.clone(); 258 } 259 TKHashtable res = (TKHashtable) h1.clone(); 260 res.merge( h2 ); 261 return res; 262 } 263 } 264 | Popular Tags |