View Javadoc

1   package org.apache.velocity.tools.config;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * <p>This base configuration class manages a set of {@link Property}s
30   * for whatever thing the instance of this class represents. When
31   * combined with another {@link Configuration} instance via
32   * {@link #addConfiguration}, the {@link Property}s of both instances are
33   * combined.</p><p>NOTE: Though this class appears {@link Comparable},
34   * the {@link #compareTo} method is unsupported. Proper comparison is
35   * left up to subclasses.</p>
36   *
37   * @author Nathan Bubna
38   * @version $Id: Configuration.java 511959 2007-02-26 19:24:39Z nbubna $
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          // remove any current properties with the same key
52          properties.remove(property);
53          // then add the new one
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             // they're of the same type
167             Configuration that = (Configuration)obj;
168             // if their properties are equal, they're equal
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 }