KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > PropertyList


1 /*
2  * PropertyList.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: PropertyList.java,v 1.1.1.1 2002/09/24 16:07:57 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 public final class PropertyList
29 {
30     private HashMap JavaDoc map;
31
32     public PropertyList()
33     {
34     }
35
36     public Object JavaDoc getProperty(Property property)
37     {
38         if (map != null)
39             return map.get(property);
40         return null;
41     }
42
43     public void setProperty(Property property, Object JavaDoc value)
44     {
45         if (map == null)
46             map = new HashMap JavaDoc();
47         map.put(property, value);
48     }
49
50     public void setProperty(Property property, boolean value)
51     {
52         setProperty(property, value ? Boolean.TRUE : Boolean.FALSE);
53     }
54
55     public void setProperty(Property property, int value)
56     {
57         setProperty(property, new Integer JavaDoc(value));
58     }
59
60     public boolean setPropertyFromString(Property property, String JavaDoc value)
61     {
62         if (property.isBooleanProperty()) {
63             if (value.equals("true") || value.equals("1")) {
64                 setProperty(property, true);
65                 return true;
66             }
67             if (value.equals("false") || value.equals("0")) {
68                 setProperty(property, false);
69                 return true;
70             }
71             return false; // Invalid boolean value.
72
}
73         if (property.isIntegerProperty()) {
74             try {
75                 setProperty(property, Integer.parseInt(value));
76                 return true;
77             }
78             catch (NumberFormatException JavaDoc e) {
79                 return false; // Invalid integer value.
80
}
81         }
82         setProperty(property, value);
83         return true;
84     }
85
86     public boolean removeProperty(Property property)
87     {
88         return map.remove(property) != null;
89     }
90
91     public boolean getBooleanProperty(Property property)
92     {
93         Object JavaDoc value = getProperty(property);
94         if (!(value instanceof Boolean JavaDoc))
95             value = property.getDefaultValue();
96         return ((Boolean JavaDoc)value).booleanValue();
97     }
98
99     public int getIntegerProperty(Property property)
100     {
101         Object JavaDoc value = getProperty(property);
102         if (!(value instanceof Integer JavaDoc))
103             value = property.getDefaultValue();
104         return ((Integer JavaDoc)value).intValue();
105     }
106
107     public String JavaDoc getStringProperty(Property property)
108     {
109         Object JavaDoc value = getProperty(property);
110         if (!(value instanceof String JavaDoc))
111             value = property.getDefaultValue();
112         return (String JavaDoc) value;
113     }
114
115     public Iterator JavaDoc keyIterator()
116     {
117         if (map != null)
118             return map.keySet().iterator();
119         return null;
120     }
121
122     public Set JavaDoc keySet()
123     {
124         if (map != null)
125             return map.keySet();
126         return null;
127     }
128
129     public int size()
130     {
131         return map != null ? map.size() : 0;
132     }
133
134     public void putAll(PropertyList other)
135     {
136         if (other.map != null && other.map.size() > 0) {
137             if (map == null)
138                 map = new HashMap JavaDoc();
139             map.putAll(other.map);
140         }
141     }
142 }
143
Popular Tags