1 package org.apache.velocity.tools.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class Configuration implements Comparable<Configuration>
41 {
42 private final SortedSet<Property> properties = new TreeSet<Property>();
43
44 public void addProperty(Property property)
45 {
46 if (property.getName() == null)
47 {
48 throw new IllegalArgumentException("All properties must be named before they can be added to the configuration.");
49 }
50
51
52 properties.remove(property);
53
54 properties.add(property);
55 }
56
57 public boolean removeProperty(Property property)
58 {
59 return properties.remove(property);
60 }
61
62 public void setProperty(String name, Object value)
63 {
64 if (name == null)
65 {
66 throw new NullPointerException("Property name cannot be null");
67 }
68
69 Property prop = new Property();
70 prop.setName(name);
71 prop.setValue(value);
72 addProperty(prop);
73 }
74
75 public boolean removeProperty(String name)
76 {
77 Property prop = getProperty(name);
78 return properties.remove(prop);
79 }
80
81 public boolean hasProperties()
82 {
83 return !properties.isEmpty();
84 }
85
86 public Property getProperty(String name)
87 {
88 for (Property prop : properties)
89 {
90 if (name.equals(prop.getName()))
91 {
92 return prop;
93 }
94 }
95 return null;
96 }
97
98 public SortedSet<Property> getProperties()
99 {
100 return new TreeSet<Property>(properties);
101 }
102
103 public Map<String,Object> getPropertyMap()
104 {
105 Map<String,Object> map = new HashMap<String,Object>();
106 for (Property prop : properties)
107 {
108 map.put(prop.getName(), prop.getConvertedValue());
109 }
110 return map;
111 }
112
113 public void setPropertyMap(Map<String,Object> props)
114 {
115 for (Map.Entry<String,Object> entry : props.entrySet())
116 {
117 setProperty(entry.getKey(), entry.getValue());
118 }
119 }
120
121 public void setProperties(Collection<Property> props)
122 {
123 for (Property newProp : props)
124 {
125 addProperty(newProp);
126 }
127 }
128
129 public void addConfiguration(Configuration config)
130 {
131 setProperties(config.getProperties());
132 }
133
134 public void validate()
135 {
136 for (Property property : properties)
137 {
138 property.validate();
139 }
140 }
141
142 public int compareTo(Configuration config)
143 {
144 throw new UnsupportedOperationException("Configuration is abstract and cannot be compared.");
145 }
146
147 @Override
148 public int hashCode()
149 {
150 return properties.hashCode();
151 }
152
153 @Override
154 public boolean equals(Object obj)
155 {
156 if (this == obj)
157 {
158 return true;
159 }
160 else if (!(obj instanceof Configuration))
161 {
162 return false;
163 }
164 else
165 {
166
167 Configuration that = (Configuration)obj;
168
169 return this.properties.equals(that.properties);
170 }
171 }
172
173 protected void appendProperties(StringBuilder out)
174 {
175 if (hasProperties())
176 {
177 out.append("with ");
178 out.append(properties.size());
179 out.append(" properties [");
180 for (Property prop : properties)
181 {
182 out.append(prop.getKey());
183 out.append(" -");
184 out.append(prop.getType());
185 out.append("-> ");
186 out.append(prop.getValue());
187 out.append("; ");
188 }
189 out.append("]");
190 }
191 }
192
193 }