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.SortedSet;
24  import java.util.TreeSet;
25  
26  /**
27   * This class manages a {@link SortedSet} of child {@link Configuration}s
28   * as well as being a {@link Configuration} itself.
29   *
30   * @author Nathan Bubna
31   * @version $Id: Configuration.java 511959 2007-02-26 19:24:39Z nbubna $
32   */
33  public class CompoundConfiguration<C extends Configuration>
34      extends Configuration
35  {
36      private final SortedSet<C> children = new TreeSet<C>();
37  
38      protected void addChild(C newKid)
39      {
40          // check if we already have a matching child
41          C child = getChild(newKid);
42          if (child != null)
43          {
44              // compound children can just have the new props and kids
45              // added to the old ones, we don't need to replace the old
46              if (child instanceof CompoundConfiguration)
47              {
48                  ((CompoundConfiguration)child)
49                      .addConfiguration((CompoundConfiguration)newKid);
50              }
51              else
52              {
53                  // add newKid's values to childs (overwriting any dupes)
54                  child.addConfiguration(newKid);
55              }
56          }
57          else
58          {
59              // simply adopt the new kid
60              children.add(newKid);
61          }
62      }
63  
64      protected boolean removeChild(C config)
65      {
66          return children.remove(config);
67      }
68  
69      protected boolean hasChildren()
70      {
71          return !children.isEmpty();
72      }
73  
74      protected Collection<C> getChildren()
75      {
76          return children;
77      }
78  
79      protected void setChildren(Collection<C> kids)
80      {
81          for (C kid : kids)
82          {
83              addChild(kid);
84          }
85      }
86  
87      protected C getChild(C kid)
88      {
89          for (C child : children)
90          {
91              if (child.equals(kid))
92              {
93                  return child;
94              }
95          }
96          return null;
97      }
98  
99      public void addConfiguration(CompoundConfiguration<C> config)
100     {
101         // add config's children to our own
102         setChildren(config.getChildren());
103 
104         // add config's properties to ours
105         super.addConfiguration(config);
106     }
107 
108     @Override
109     public void validate()
110     {
111         super.validate();
112 
113         for (C child : children)
114         {
115             child.validate();
116         }
117     }
118 
119     protected void appendChildren(StringBuilder out, String childrenName, String childDelim)
120     {
121         if (hasChildren())
122         {
123             if (hasProperties())
124             {
125                 out.append(" and ");
126             }
127             else
128             {
129                 out.append(" with ");
130             }
131             out.append(children.size());
132             out.append(' ');
133             out.append(childrenName);
134             for (C child : children)
135             {
136                 out.append(child);
137                 out.append(childDelim);
138             }
139         }
140     }
141 
142     @Override
143     public int hashCode()
144     {
145         // add the super's and the kid's
146         return super.hashCode() + children.hashCode();
147     }
148 
149     @Override
150     public boolean equals(Object obj)
151     {
152         // must be of this type and have super.equals() be true
153         if (!(obj instanceof CompoundConfiguration) || !super.equals(obj))
154         {
155             return false;
156         }
157         else
158         {
159             // they're of the same type
160             CompoundConfiguration<C> that = (CompoundConfiguration<C>)obj;
161             // if their children are equal, they're equal
162             return this.children.equals(that.children);
163         }
164     }
165 
166 }