1 package net.firstpartners.nounit.ui.common; 2 3 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Vector ; 30 31 32 36 abstract public class AbstractPackage { 37 38 protected HashMap innerValuePairs = new HashMap (); 41 44 protected AbstractPackage() {} 45 46 51 protected AbstractPackage(Object [] inValues) { 52 53 if (innerValuePairs == null) { 54 innerValuePairs = new HashMap (); 55 } 56 57 this.addValue(inValues); 58 59 } 60 61 67 protected void addValue(Object [] inValues) { 68 69 if (inValues!=null && inValues.length >0){ 70 71 for (int a=0 ; a<inValues.length; a=a+2) { 72 innerValuePairs.put(inValues[a],inValues[a+1]); 73 } 74 } 75 76 77 } 78 79 80 81 86 public Object getValue(String keyName) { 87 88 Object associatedValue = null; 89 90 if (keyName==null || keyName.equals("")|| innerValuePairs.isEmpty()) { 92 return null; 93 } 94 95 associatedValue = innerValuePairs.get(keyName); 96 97 return associatedValue; 98 } 99 100 106 107 public void addValue(Object keyName , Object storeObject){ 108 109 innerValuePairs.put(keyName,storeObject); 110 111 } 112 113 117 public Iterator getStoreNames() { 118 119 Iterator returnList =null; 121 122 if (innerValuePairs!=null) { 123 returnList = innerValuePairs.keySet().iterator(); 124 } 125 126 return returnList; 127 128 } 129 130 134 public HashMap getValuePairs() { 135 136 return innerValuePairs; 137 } 138 139 140 145 public void addValues(HashMap valuesToAdd){ 146 147 Object tmpKey; 149 Object tmpValue; 150 Iterator myList = valuesToAdd.keySet().iterator(); 151 152 while (myList.hasNext()){ 154 155 tmpKey = myList.next(); 156 tmpValue = valuesToAdd.get(tmpKey); 157 158 addValue(tmpKey,tmpValue); 159 160 } 161 162 } 163 164 169 public String getString(String keyName) { 170 171 String valueString =""; 172 173 Object tmpObject = this.getValue(keyName); 175 176 if ((tmpObject!=null)&&(tmpObject instanceof String )) { 177 valueString = (String )tmpObject; 178 179 } 180 return valueString; 181 } 182 183 189 public Vector getVector(String keyName) { 190 191 Vector valueVector =new Vector (); 192 193 Object tmpObject = this.getValue(keyName); 195 196 if (tmpObject == null) { 198 return new Vector (); 199 } 200 201 if ((tmpObject!=null)&&(tmpObject instanceof Vector )) { 203 valueVector = (Vector )tmpObject; 204 } 205 206 if ((tmpObject!=null)&&(!(tmpObject instanceof Vector ))) { 208 valueVector.add(tmpObject); 209 } 210 211 return valueVector; 212 } 213 214 215 221 public int getInt(String keyName) { 222 223 int returnInt =0; 224 225 Object tmpObject = this.getValue(keyName); 227 Integer tmpInteger; 228 229 try { 230 231 if ((tmpObject!=null)&&(tmpObject instanceof Integer )) { 233 tmpInteger = (Integer )tmpObject; 234 returnInt = tmpInteger.intValue(); 235 } 236 237 if ((tmpObject!=null)&&(tmpObject instanceof String )) { 239 tmpInteger = new Integer ((String )tmpObject); 240 returnInt = tmpInteger.intValue(); 241 } 242 } catch (NumberFormatException nfe) { 243 244 246 } catch (ClassCastException cce) { 247 248 250 } 251 252 return returnInt; 253 } 254 255 256 257 261 public void printToOutputStream(java.io.PrintStream out) { 262 263 Iterator tmpList = innerValuePairs.keySet().iterator(); 264 Object tmpObject; 265 266 while (tmpList.hasNext()) { 267 268 tmpObject=tmpList.next(); 269 out.print(""+tmpObject.toString()+":"); 270 out.println(getValue(tmpObject.toString())); 271 272 } 273 274 } 275 276 280 public String toString() { 281 282 Iterator tmpList = innerValuePairs.keySet().iterator(); 283 Object tmpObject; 284 StringBuffer out = new StringBuffer (); 285 286 while (tmpList.hasNext()) { 287 288 tmpObject=tmpList.next(); 289 out.append(""+tmpObject.toString()+":"); 290 out.append(getValue(tmpObject.toString())); 291 out.append("\n"); 292 293 } 294 295 return out.toString(); 296 } 297 298 303 public void addAdditionalValue(String key , Object value) { 304 305 Object storedObject =null; 307 Vector tmpVector = null; 308 309 storedObject = getValue(key); 311 312 if(storedObject==null) { 314 this.addValue(key,value); 315 } 316 317 if (storedObject instanceof String ) { 319 tmpVector = new Vector (); 320 321 tmpVector.add(storedObject); 323 324 tmpVector.add(value); 326 327 this.addValue(key,tmpVector); 329 } 330 331 if (storedObject instanceof Vector ) { 333 334 tmpVector = (Vector )storedObject; 336 337 tmpVector.add(value); 339 340 this.addValue(key,tmpVector); 342 } 343 344 } 345 346 347 352 public void addValues(String [] inValues) { 353 354 355 String tmpKey = null; 357 358 for (int a=0 ; a<inValues.length ; a=a+2) { 360 tmpKey = inValues[a]; 361 innerValuePairs.remove(tmpKey); 362 } 363 364 if (inValues!=null && inValues.length >0){ 366 367 for (int a=0 ; a<inValues.length ; a=a+2) { 369 370 this.addAdditionalValue(inValues[a],inValues[a+1]); 371 372 } 373 374 } 375 376 } 377 378 382 public void remove(Object key) { 383 innerValuePairs.remove(key); 384 } 385 386 392 public void move (String oldKey , String newKey) { 393 394 395 if ((oldKey==null||(newKey==null))) { 397 return; 398 } 399 400 Object tmpObject = this.getValue(oldKey); 402 if (tmpObject==null) { 403 return; 404 } 405 406 this.remove(oldKey); 408 this.addValue(newKey,tmpObject); 409 410 } 411 412 417 418 public void addIfEmpty(String keyName , Object storeObject){ 419 420 if ((this.getValue(keyName)==null)||(this.getString(keyName).equals(""))) { 421 this.addValue(keyName,storeObject); 422 } 423 424 } 425 } | Popular Tags |