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.SortedSet;
24 import java.util.TreeSet;
25
26
27
28
29
30
31
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
41 C child = getChild(newKid);
42 if (child != null)
43 {
44
45
46 if (child instanceof CompoundConfiguration)
47 {
48 ((CompoundConfiguration)child)
49 .addConfiguration((CompoundConfiguration)newKid);
50 }
51 else
52 {
53
54 child.addConfiguration(newKid);
55 }
56 }
57 else
58 {
59
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
102 setChildren(config.getChildren());
103
104
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
146 return super.hashCode() + children.hashCode();
147 }
148
149 @Override
150 public boolean equals(Object obj)
151 {
152
153 if (!(obj instanceof CompoundConfiguration) || !super.equals(obj))
154 {
155 return false;
156 }
157 else
158 {
159
160 CompoundConfiguration<C> that = (CompoundConfiguration<C>)obj;
161
162 return this.children.equals(that.children);
163 }
164 }
165
166 }