001: /*
002: * PropertyList.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: PropertyList.java,v 1.1.1.1 2002/09/24 16:07:57 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Set;
027:
028: public final class PropertyList {
029: private HashMap map;
030:
031: public PropertyList() {
032: }
033:
034: public Object getProperty(Property property) {
035: if (map != null)
036: return map.get(property);
037: return null;
038: }
039:
040: public void setProperty(Property property, Object value) {
041: if (map == null)
042: map = new HashMap();
043: map.put(property, value);
044: }
045:
046: public void setProperty(Property property, boolean value) {
047: setProperty(property, value ? Boolean.TRUE : Boolean.FALSE);
048: }
049:
050: public void setProperty(Property property, int value) {
051: setProperty(property, new Integer(value));
052: }
053:
054: public boolean setPropertyFromString(Property property, String value) {
055: if (property.isBooleanProperty()) {
056: if (value.equals("true") || value.equals("1")) {
057: setProperty(property, true);
058: return true;
059: }
060: if (value.equals("false") || value.equals("0")) {
061: setProperty(property, false);
062: return true;
063: }
064: return false; // Invalid boolean value.
065: }
066: if (property.isIntegerProperty()) {
067: try {
068: setProperty(property, Integer.parseInt(value));
069: return true;
070: } catch (NumberFormatException e) {
071: return false; // Invalid integer value.
072: }
073: }
074: setProperty(property, value);
075: return true;
076: }
077:
078: public boolean removeProperty(Property property) {
079: return map.remove(property) != null;
080: }
081:
082: public boolean getBooleanProperty(Property property) {
083: Object value = getProperty(property);
084: if (!(value instanceof Boolean))
085: value = property.getDefaultValue();
086: return ((Boolean) value).booleanValue();
087: }
088:
089: public int getIntegerProperty(Property property) {
090: Object value = getProperty(property);
091: if (!(value instanceof Integer))
092: value = property.getDefaultValue();
093: return ((Integer) value).intValue();
094: }
095:
096: public String getStringProperty(Property property) {
097: Object value = getProperty(property);
098: if (!(value instanceof String))
099: value = property.getDefaultValue();
100: return (String) value;
101: }
102:
103: public Iterator keyIterator() {
104: if (map != null)
105: return map.keySet().iterator();
106: return null;
107: }
108:
109: public Set keySet() {
110: if (map != null)
111: return map.keySet();
112: return null;
113: }
114:
115: public int size() {
116: return map != null ? map.size() : 0;
117: }
118:
119: public void putAll(PropertyList other) {
120: if (other.map != null && other.map.size() > 0) {
121: if (map == null)
122: map = new HashMap();
123: map.putAll(other.map);
124: }
125: }
126: }
|