001: package net.firstpartners.nounit.ui.common;
002:
003: /**
004: * Title: NoUnit - Identify Classes that are not being unit Tested
005: *
006: * Copyright (C) 2001 Paul Browne , FirstPartners.net
007: *
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * @author Paul Browne
024: * @version 0.7
025: */
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Vector;
030:
031: /**
032: * An abstract package that encapsulates a hashtable to provide conversion of
033: * command-line arguments
034: */
035: abstract public class AbstractPackage {
036:
037: //Holds inner value pairs
038: protected HashMap innerValuePairs = new HashMap(); // overwritten later if required
039:
040: /**
041: * default constructor
042: */
043: protected AbstractPackage() {
044: }
045:
046: /**
047: * Constructor builds Package using Standard Values (as from command Line)
048: * Overwrites any Previous Values
049: * @param inValues pairs as generated by the command line
050: */
051: protected AbstractPackage(Object[] inValues) {
052:
053: if (innerValuePairs == null) {
054: innerValuePairs = new HashMap();
055: }
056:
057: this .addValue(inValues);
058:
059: }
060:
061: /**
062: * Add set of KeyName - ValueName (in one array) to object ,
063: * used by Constructor , and classes inheriting from this one
064: * Overwrites any existing values with same name
065: * @param inValues - Array of keys and values to add
066: */
067: protected void addValue(Object[] inValues) {
068:
069: if (inValues != null && inValues.length > 0) {
070:
071: for (int a = 0; a < inValues.length; a = a + 2) {
072: innerValuePairs.put(inValues[a], inValues[a + 1]);
073: }
074: }
075:
076: }
077:
078: /**
079: * gets the value associated with the key name , as passed in from the Servlet, command line
080: * @param keyName - the name the value is stored under.
081: * @return associatedValue - the value that was set under this name
082: */
083: public Object getValue(String keyName) {
084:
085: Object associatedValue = null;
086:
087: // Check for empty key , or empty value store
088: if (keyName == null || keyName.equals("")
089: || innerValuePairs.isEmpty()) {
090: return null;
091: }
092:
093: associatedValue = innerValuePairs.get(keyName);
094:
095: return associatedValue;
096: }
097:
098: /**
099: * adds a value to the internal store
100: * Overwrites any existing values with same name
101: * @param keyName to store the object under for later retrieval
102: * @param storeObject to keep internally
103: */
104:
105: public void addValue(Object keyName, Object storeObject) {
106:
107: innerValuePairs.put(keyName, storeObject);
108:
109: }
110:
111: /**
112: * Gets the names of values stored in this webpackage
113: * @return returnEnum of Names values are stored under in this package
114: */
115: public Iterator getStoreNames() {
116:
117: //Inner Variables
118: Iterator returnList = null;
119:
120: if (innerValuePairs != null) {
121: returnList = innerValuePairs.keySet().iterator();
122: }
123:
124: return returnList;
125:
126: }
127:
128: /**
129: * Method to return a hashtable of value pairs
130: * @return innerValuePairs
131: */
132: public HashMap getValuePairs() {
133:
134: return innerValuePairs;
135: }
136:
137: /**
138: * Set the Values in the web package , given a hashtable
139: * Overwrites any existing values with same name
140: * @param valuesToAdd
141: */
142: public void addValues(HashMap valuesToAdd) {
143:
144: //Internal Objects
145: Object tmpKey;
146: Object tmpValue;
147: Iterator myList = valuesToAdd.keySet().iterator();
148:
149: //Loop through incoming hashtable , adding values to internal hashtable
150: while (myList.hasNext()) {
151:
152: tmpKey = myList.next();
153: tmpValue = valuesToAdd.get(tmpKey);
154:
155: addValue(tmpKey, tmpValue);
156:
157: }
158:
159: }
160:
161: /**
162: * Get the Value as a String (Convience Method)
163: * @param keyName of Value to find
164: * @return valueString that is stored under this string , "" if nothing
165: */
166: public String getString(String keyName) {
167:
168: String valueString = "";
169:
170: //Get Object and cast if possible
171: Object tmpObject = this .getValue(keyName);
172:
173: if ((tmpObject != null) && (tmpObject instanceof String)) {
174: valueString = (String) tmpObject;
175:
176: }
177: return valueString;
178: }
179:
180: /**
181: * Get the Value as a Vector (Convience Method)
182: * @param keyName of Value to find
183: * @return valueVector that is stored under this string , empty Vector
184: * if no value stored internally under this name
185: */
186: public Vector getVector(String keyName) {
187:
188: Vector valueVector = new Vector();
189:
190: //Get Object and cast if possible
191: Object tmpObject = this .getValue(keyName);
192:
193: //Null value? - return empty Vector
194: if (tmpObject == null) {
195: return new Vector();
196: }
197:
198: //Already a vector
199: if ((tmpObject != null) && (tmpObject instanceof Vector)) {
200: valueVector = (Vector) tmpObject;
201: }
202:
203: //Any other object ... remake as vector
204: if ((tmpObject != null) && (!(tmpObject instanceof Vector))) {
205: valueVector.add(tmpObject);
206: }
207:
208: return valueVector;
209: }
210:
211: /**
212: * Get the Value as an int (Convience Method)
213: * @param keyName of Value to find
214: * @return int that is stored under this string , empty Vector
215: * if no value stored internally under this name
216: */
217: public int getInt(String keyName) {
218:
219: int returnInt = 0;
220:
221: //Get Object and cast if possible
222: Object tmpObject = this .getValue(keyName);
223: Integer tmpInteger;
224:
225: try {
226:
227: // If stored as integer ...
228: if ((tmpObject != null) && (tmpObject instanceof Integer)) {
229: tmpInteger = (Integer) tmpObject;
230: returnInt = tmpInteger.intValue();
231: }
232:
233: //If stored as string ...
234: if ((tmpObject != null) && (tmpObject instanceof String)) {
235: tmpInteger = new Integer((String) tmpObject);
236: returnInt = tmpInteger.intValue();
237: }
238: } catch (NumberFormatException nfe) {
239:
240: // Do Nothing - value will be returned as zero
241:
242: } catch (ClassCastException cce) {
243:
244: // Do Nothing - value will be returned as zero
245:
246: }
247:
248: return returnInt;
249: }
250:
251: /**
252: * Prints the internal represenation of the Package to an output stream for debugging
253: * @param out - java.io.PrintStream to print to
254: */
255: public void printToOutputStream(java.io.PrintStream out) {
256:
257: Iterator tmpList = innerValuePairs.keySet().iterator();
258: Object tmpObject;
259:
260: while (tmpList.hasNext()) {
261:
262: tmpObject = tmpList.next();
263: out.print("" + tmpObject.toString() + ":");
264: out.println(getValue(tmpObject.toString()));
265:
266: }
267:
268: }
269:
270: /**
271: * Prints the internal represenation of the Package to an output stream for debugging
272: * @return out - java.io.PrintStream to print to
273: */
274: public String toString() {
275:
276: Iterator tmpList = innerValuePairs.keySet().iterator();
277: Object tmpObject;
278: StringBuffer out = new StringBuffer();
279:
280: while (tmpList.hasNext()) {
281:
282: tmpObject = tmpList.next();
283: out.append("" + tmpObject.toString() + ":");
284: out.append(getValue(tmpObject.toString()));
285: out.append("\n");
286:
287: }
288:
289: return out.toString();
290: }
291:
292: /**
293: * Add the value pairs to the internal store - do not overwrite , but convert to vector
294: * @param key object
295: * @param value object
296: */
297: public void addAdditionalValue(String key, Object value) {
298:
299: //Method level variables
300: Object storedObject = null;
301: Vector tmpVector = null;
302:
303: //Check for existing Value
304: storedObject = getValue(key);
305:
306: //Case 1 : no existing value
307: if (storedObject == null) {
308: this .addValue(key, value);
309: }
310:
311: //Case 2 : one existing value
312: if (storedObject instanceof String) {
313: tmpVector = new Vector();
314:
315: //Add Existing Object
316: tmpVector.add(storedObject);
317:
318: //Add New Object
319: tmpVector.add(value);
320:
321: //Store Vector under key
322: this .addValue(key, tmpVector);
323: }
324:
325: //Case 3 : multiple existing values
326: if (storedObject instanceof Vector) {
327:
328: //Get Existing Vector
329: tmpVector = (Vector) storedObject;
330:
331: //Add New Object
332: tmpVector.add(value);
333:
334: //Store update Vector under key
335: this .addValue(key, tmpVector);
336: }
337:
338: }
339:
340: /**
341: * Add the value pairs to the internal store<BR>
342: * Overwrites any existing values with same name
343: * @param inValues - Array to add to Value pairs e.g. key1 , value1, key2 , value2
344: */
345: public void addValues(String[] inValues) {
346:
347: //Method level variables
348: String tmpKey = null;
349:
350: //Loop through and remove any existing values from the internal store
351: for (int a = 0; a < inValues.length; a = a + 2) {
352: tmpKey = inValues[a];
353: innerValuePairs.remove(tmpKey);
354: }
355:
356: //Loop through and add values to internal store
357: if (inValues != null && inValues.length > 0) {
358:
359: //Add Objects to internal Hashtable , allow for multiples
360: for (int a = 0; a < inValues.length; a = a + 2) {
361:
362: this .addAdditionalValue(inValues[a], inValues[a + 1]);
363:
364: }
365:
366: }
367:
368: }
369:
370: /**
371: * Remove a value from the internal store
372: * @param key of object to remove
373: */
374: public void remove(Object key) {
375: innerValuePairs.remove(key);
376: }
377:
378: /**
379: * Moves Objects from one key to another
380: * will overwrite anything (previously)held at the new key
381: * @param oldKey - String
382: * @param newKey - String
383: */
384: public void move(String oldKey, String newKey) {
385:
386: //Check the Old and new Keys are not null
387: if ((oldKey == null || (newKey == null))) {
388: return;
389: }
390:
391: //Get Object stored & make sure it isn't null
392: Object tmpObject = this .getValue(oldKey);
393: if (tmpObject == null) {
394: return;
395: }
396:
397: // do the move
398: this .remove(oldKey);
399: this .addValue(newKey, tmpObject);
400:
401: }
402:
403: /**
404: * adds a value to the internal store , only if previous value was null / empty
405: * @param keyName to store the object under for later retrieval
406: * @param storeObject to keep internally
407: */
408:
409: public void addIfEmpty(String keyName, Object storeObject) {
410:
411: if ((this .getValue(keyName) == null)
412: || (this .getString(keyName).equals(""))) {
413: this.addValue(keyName, storeObject);
414: }
415:
416: }
417: }
|