/* Copyright 2005-2006 David N. Welton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.hecl;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* The Properties class is used to parse command line
* arguments. Its basic usage pattern is like so: a new Properties is
* instantiated with default properties and values, then setProps is
* called with argv. At that point the rest of the command can go on,
* and for every prop that's needed, it can be fetched with getProp.
*
* @author David N. Welton
* @version 1.0
*/
public class Properties {
protected Hashtable props;
/**
* Creates a new Properties instance with no default
* properties.
*
*/
public Properties () {
props = new Hashtable();
}
/**
* Creates a new Properties instance with default
* properties and their values.
*
* @param defaultprops an Object[] value
*/
public Properties (Object [] defaultprops) {
props = new Hashtable();
for (int i = 0; i < defaultprops.length; i+=2) {
props.put((String)defaultprops[i], (Thing)defaultprops[i+1]);
}
}
/**
* The setProps method sets properties with their
* values from the command line argv. The number of Things
* handled must be even.
*
* @param argv a Thing[] value
* @param offset an int value
* @exception HeclException if an error occurs
*/
public void setProps(Thing[] argv, int offset)
throws HeclException {
if ((argv.length - offset) % 2 != 0) {
throw new HeclException("Properties must be name-value pairs");
}
for(int i = offset; i < argv.length; i +=2) {
setProp(argv[i].toString(), argv[i+1]);
}
}
/**
* The setProp method sets a single property to some
* value.
*
* @param name a String value
* @param val a Thing value
*/
public void setProp(String name, Thing val) {
props.put(name.toLowerCase(), (Object)val);
}
/**
* The getProp method fetches the value of a
* property.
*
* @param name a String value
* @return a Thing value
*/
public Thing getProp(String name) {
return (Thing)props.get(name);
}
/**
* The getProp method with two arguments fetches the
* value of the property given by 'name', or, if that does not
* exist, returns a default value.
*
* @param name a String value
* @param defaultVal a Thing value
* @return a Thing value
*/
public Thing getProp(String name, Thing defaultVal) {
Thing val = getProp(name);
return (val == null) ? defaultVal : val;
}
/**
* The existsProp method is used to determine if a
* property exists or not.
*
* @param name a String value
* @return a boolean value
*/
public boolean existsProp(String name) {
return props.containsKey(name);
}
/**
* The getAndDelProp method fetches and deletes the
* value associated with 'name', or returns null if there is no
* value associated with it.
*
* @param name a String value
* @return a Thing value
*/
public Thing getAndDelProp(String name) {
return (Thing)props.remove(name);
}
/**
* delProp removes a property from the property set.
*
* @param name a String value
*/
public void delProp(String name) {
props.remove(name);
}
/**
* getProps converts the properties back into an
* array of Things.
*
* @return an array of Things, being property name
* and property value.
*/
public Thing[] getProps() {
int n = props.size();
Thing[] t = new Thing[2*n];
Enumeration names = props.keys();
Enumeration vals = props.elements();
int i = 0;
while(names.hasMoreElements()) {
t[i++] = new Thing((String)names.nextElement());
t[i++] = (Thing)vals.nextElement();
}
return t;
}
/**
* isEmpty test if property set is empty.
*
* @return true if it's empty, flase otherwise.
*/
public boolean isEmpty() {
return props.isEmpty();
}
}
Ads help maintain this website.