KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > ui > common > AbstractPackage


1 package net.firstpartners.nounit.ui.common;
2
3 /**
4  * Title: NoUnit - Identify Classes that are not being unit Tested
5  *
6  * Copyright (C) 2001 Paul Browne , FirstPartners.net
7  *
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * @author Paul Browne
24  * @version 0.7
25  */

26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Vector JavaDoc;
30
31
32  /**
33   * An abstract package that encapsulates a hashtable to provide conversion of
34   * command-line arguments
35   */

36 abstract public class AbstractPackage {
37
38    //Holds inner value pairs
39
protected HashMap JavaDoc innerValuePairs = new HashMap JavaDoc(); // overwritten later if required
40

41    /**
42     * default constructor
43     */

44    protected AbstractPackage() {}
45
46    /**
47    * Constructor builds Package using Standard Values (as from command Line)
48    * Overwrites any Previous Values
49    * @param inValues pairs as generated by the command line
50    */

51   protected AbstractPackage(Object JavaDoc[] inValues) {
52       
53     if (innerValuePairs == null) {
54       innerValuePairs = new HashMap JavaDoc();
55     }
56
57     this.addValue(inValues);
58
59   }
60
61   /**
62    * Add set of KeyName - ValueName (in one array) to object ,
63    * used by Constructor , and classes inheriting from this one
64    * Overwrites any existing values with same name
65    * @param inValues - Array of keys and values to add
66    */

67   protected void addValue(Object JavaDoc[] 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   /**
82    * gets the value associated with the key name , as passed in from the Servlet, command line
83    * @param keyName - the name the value is stored under.
84    * @return associatedValue - the value that was set under this name
85    */

86   public Object JavaDoc getValue(String JavaDoc keyName) {
87
88     Object JavaDoc associatedValue = null;
89
90     // Check for empty key , or empty value store
91
if (keyName==null || keyName.equals("")|| innerValuePairs.isEmpty()) {
92       return null;
93     }
94
95     associatedValue = innerValuePairs.get(keyName);
96
97     return associatedValue;
98   }
99
100   /**
101    * adds a value to the internal store
102    * Overwrites any existing values with same name
103    * @param keyName to store the object under for later retrieval
104    * @param storeObject to keep internally
105    */

106
107   public void addValue(Object JavaDoc keyName , Object JavaDoc storeObject){
108
109     innerValuePairs.put(keyName,storeObject);
110
111   }
112
113     /**
114      * Gets the names of values stored in this webpackage
115      * @return returnEnum of Names values are stored under in this package
116      */

117     public Iterator JavaDoc getStoreNames() {
118
119       //Inner Variables
120
Iterator JavaDoc returnList =null;
121
122       if (innerValuePairs!=null) {
123         returnList = innerValuePairs.keySet().iterator();
124       }
125
126       return returnList;
127
128     }
129
130    /**
131      * Method to return a hashtable of value pairs
132      * @return innerValuePairs
133      */

134     public HashMap JavaDoc getValuePairs() {
135
136       return innerValuePairs;
137     }
138
139
140   /**
141    * Set the Values in the web package , given a hashtable
142    * Overwrites any existing values with same name
143    * @param valuesToAdd
144    */

145   public void addValues(HashMap JavaDoc valuesToAdd){
146
147     //Internal Objects
148
Object JavaDoc tmpKey;
149     Object JavaDoc tmpValue;
150     Iterator JavaDoc myList = valuesToAdd.keySet().iterator();
151
152     //Loop through incoming hashtable , adding values to internal hashtable
153
while (myList.hasNext()){
154
155       tmpKey = myList.next();
156       tmpValue = valuesToAdd.get(tmpKey);
157
158       addValue(tmpKey,tmpValue);
159
160     }
161
162   }
163
164   /**
165    * Get the Value as a String (Convience Method)
166    * @param keyName of Value to find
167    * @return valueString that is stored under this string , "" if nothing
168    */

169    public String JavaDoc getString(String JavaDoc keyName) {
170
171       String JavaDoc valueString ="";
172
173       //Get Object and cast if possible
174
Object JavaDoc tmpObject = this.getValue(keyName);
175
176       if ((tmpObject!=null)&&(tmpObject instanceof String JavaDoc)) {
177         valueString = (String JavaDoc)tmpObject;
178
179       }
180       return valueString;
181    }
182
183   /**
184    * Get the Value as a Vector (Convience Method)
185    * @param keyName of Value to find
186    * @return valueVector that is stored under this string , empty Vector
187    * if no value stored internally under this name
188    */

189    public Vector JavaDoc getVector(String JavaDoc keyName) {
190
191       Vector JavaDoc valueVector =new Vector JavaDoc();
192
193       //Get Object and cast if possible
194
Object JavaDoc tmpObject = this.getValue(keyName);
195
196       //Null value? - return empty Vector
197
if (tmpObject == null) {
198         return new Vector JavaDoc();
199       }
200
201       //Already a vector
202
if ((tmpObject!=null)&&(tmpObject instanceof Vector JavaDoc)) {
203         valueVector = (Vector JavaDoc)tmpObject;
204       }
205
206       //Any other object ... remake as vector
207
if ((tmpObject!=null)&&(!(tmpObject instanceof Vector JavaDoc))) {
208         valueVector.add(tmpObject);
209       }
210
211       return valueVector;
212    }
213
214
215   /**
216    * Get the Value as an int (Convience Method)
217    * @param keyName of Value to find
218    * @return int that is stored under this string , empty Vector
219    * if no value stored internally under this name
220    */

221    public int getInt(String JavaDoc keyName) {
222
223       int returnInt =0;
224
225       //Get Object and cast if possible
226
Object JavaDoc tmpObject = this.getValue(keyName);
227       Integer JavaDoc tmpInteger;
228
229       try {
230
231         // If stored as integer ...
232
if ((tmpObject!=null)&&(tmpObject instanceof Integer JavaDoc)) {
233           tmpInteger = (Integer JavaDoc)tmpObject;
234           returnInt = tmpInteger.intValue();
235         }
236
237         //If stored as string ...
238
if ((tmpObject!=null)&&(tmpObject instanceof String JavaDoc)) {
239           tmpInteger = new Integer JavaDoc((String JavaDoc)tmpObject);
240           returnInt = tmpInteger.intValue();
241         }
242       } catch (NumberFormatException JavaDoc nfe) {
243
244         // Do Nothing - value will be returned as zero
245

246       } catch (ClassCastException JavaDoc cce) {
247
248         // Do Nothing - value will be returned as zero
249

250       }
251
252       return returnInt;
253    }
254
255   
256
257    /**
258     * Prints the internal represenation of the Package to an output stream for debugging
259     * @param out - java.io.PrintStream to print to
260     */

261    public void printToOutputStream(java.io.PrintStream JavaDoc out) {
262
263        Iterator JavaDoc tmpList = innerValuePairs.keySet().iterator();
264        Object JavaDoc 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   /**
277     * Prints the internal represenation of the Package to an output stream for debugging
278     * @return out - java.io.PrintStream to print to
279     */

280    public String JavaDoc toString() {
281
282        Iterator JavaDoc tmpList = innerValuePairs.keySet().iterator();
283        Object JavaDoc tmpObject;
284        StringBuffer JavaDoc out = new StringBuffer JavaDoc();
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   /**
299    * Add the value pairs to the internal store - do not overwrite , but convert to vector
300    * @param key object
301    * @param value object
302    */

303    public void addAdditionalValue(String JavaDoc key , Object JavaDoc value) {
304
305       //Method level variables
306
Object JavaDoc storedObject =null;
307       Vector JavaDoc tmpVector = null;
308
309           //Check for existing Value
310
storedObject = getValue(key);
311
312             //Case 1 : no existing value
313
if(storedObject==null) {
314               this.addValue(key,value);
315             }
316
317             //Case 2 : one existing value
318
if (storedObject instanceof String JavaDoc) {
319               tmpVector = new Vector JavaDoc();
320
321                 //Add Existing Object
322
tmpVector.add(storedObject);
323
324                 //Add New Object
325
tmpVector.add(value);
326
327                 //Store Vector under key
328
this.addValue(key,tmpVector);
329             }
330
331             //Case 3 : multiple existing values
332
if (storedObject instanceof Vector JavaDoc) {
333
334                 //Get Existing Vector
335
tmpVector = (Vector JavaDoc)storedObject;
336
337                 //Add New Object
338
tmpVector.add(value);
339
340                 //Store update Vector under key
341
this.addValue(key,tmpVector);
342             }
343
344    }
345
346
347   /**
348    * Add the value pairs to the internal store<BR>
349    * Overwrites any existing values with same name
350    * @param inValues - Array to add to Value pairs e.g. key1 , value1, key2 , value2
351    */

352   public void addValues(String JavaDoc[] inValues) {
353
354
355       //Method level variables
356
String JavaDoc tmpKey = null;
357
358       //Loop through and remove any existing values from the internal store
359
for (int a=0 ; a<inValues.length ; a=a+2) {
360           tmpKey = inValues[a];
361           innerValuePairs.remove(tmpKey);
362        }
363
364       //Loop through and add values to internal store
365
if (inValues!=null && inValues.length >0){
366
367         //Add Objects to internal Hashtable , allow for multiples
368
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    /**
379      * Remove a value from the internal store
380      * @param key of object to remove
381      */

382     public void remove(Object JavaDoc key) {
383       innerValuePairs.remove(key);
384     }
385
386     /**
387      * Moves Objects from one key to another
388      * will overwrite anything (previously)held at the new key
389      * @param oldKey - String
390      * @param newKey - String
391      */

392     public void move (String JavaDoc oldKey , String JavaDoc newKey) {
393
394
395       //Check the Old and new Keys are not null
396
if ((oldKey==null||(newKey==null))) {
397          return;
398       }
399
400      //Get Object stored & make sure it isn't null
401
Object JavaDoc tmpObject = this.getValue(oldKey);
402      if (tmpObject==null) {
403          return;
404      }
405
406       // do the move
407
this.remove(oldKey);
408       this.addValue(newKey,tmpObject);
409
410     }
411
412  /**
413    * adds a value to the internal store , only if previous value was null / empty
414    * @param keyName to store the object under for later retrieval
415    * @param storeObject to keep internally
416    */

417
418   public void addIfEmpty(String JavaDoc keyName , Object JavaDoc storeObject){
419
420     if ((this.getValue(keyName)==null)||(this.getString(keyName).equals(""))) {
421       this.addValue(keyName,storeObject);
422     }
423
424   }
425 }
Popular Tags