View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.config.generator.model;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import net.sf.ehcache.config.FactoryConfiguration;
25  import net.sf.ehcache.config.generator.model.elements.FactoryConfigurationElement;
26  
27  /***
28   * An abstract implementation of {@link NodeElement}. Overrides {@link #equals(Object)} and {@link #hashCode()} methods by comparing the fully
29   * qualified name of this element -- {@link #getFQName()}
30   *
31   * @author Abhishek Sanoujam
32   *
33   */
34  public abstract class AbstractNodeElement implements NodeElement {
35  
36      /***
37       * List of attributes
38       */
39      protected final List<NodeAttribute> attributes = new ArrayList<NodeAttribute>();
40  
41      /***
42       * List of child elements
43       */
44      protected final List<NodeElement> children = new ArrayList<NodeElement>();
45  
46      /***
47       * The parent
48       */
49      protected NodeElement parent;
50  
51      /***
52       * Whether this element is optional
53       */
54      protected boolean optional;
55  
56      /***
57       * the inner string content
58       */
59      protected String innerContent;
60  
61      /***
62       * Constructor accepting the parent of this element
63       *
64       * @param parent
65       */
66      public AbstractNodeElement(NodeElement parent) {
67          this.parent = parent;
68      }
69  
70      /***
71       * {@inheritDoc}
72       */
73      public abstract String getName();
74  
75      /***
76       * {@inheritDoc}
77       */
78      public NodeElement getParent() {
79          return parent;
80      }
81  
82      /***
83       * {@inheritDoc}
84       */
85      public List<NodeAttribute> getAttributes() {
86          return attributes;
87      }
88  
89      /***
90       * {@inheritDoc}
91       */
92      public List<NodeElement> getChildElements() {
93          return children;
94      }
95  
96      /***
97       * {@inheritDoc}
98       */
99      public void addAttribute(NodeAttribute attribute) {
100         if (attribute == null) {
101             return;
102         }
103         this.attributes.add(attribute);
104     }
105 
106     /***
107      * {@inheritDoc}
108      */
109     public void addChildElement(NodeElement childElement) {
110         if (childElement == null) {
111             return;
112         }
113         this.children.add(childElement);
114     }
115 
116     /***
117      * {@inheritDoc}
118      */
119     public boolean isOptional() {
120         return optional;
121     }
122 
123     /***
124      * {@inheritDoc}
125      */
126     public void setOptional(boolean optional) {
127         this.optional = optional;
128     }
129 
130     /***
131      * {@inheritDoc}
132      */
133     public boolean hasChildren() {
134         return children.isEmpty();
135     }
136 
137     /***
138      * {@inheritDoc}
139      */
140     public String getInnerContent() {
141         return innerContent;
142     }
143 
144     /***
145      * {@inheritDoc}
146      */
147     public void setInnerContent(String content) {
148         this.innerContent = content;
149     }
150 
151     /***
152      * Helper method that adds all the {@link FactoryConfiguration} from the parameter as child elements by creating
153      * {@link FactoryConfigurationElement} for each of them
154      *
155      * @param element
156      *            the element in which the child elements will be added
157      * @param name
158      *            name to be used for the child element(s)
159      * @param factoryConfigurations
160      *            the {@link FactoryConfiguration}'s
161      */
162     public static void addAllFactoryConfigsAsChildElements(NodeElement element, String name,
163             Collection<? extends FactoryConfiguration> factoryConfigurations) {
164         if (factoryConfigurations == null || factoryConfigurations.size() == 0) {
165             return;
166         }
167         for (NodeElement child : getAllFactoryElements(element, name, factoryConfigurations)) {
168             element.addChildElement(child);
169         }
170     }
171 
172     /***
173      * Helper method that creates {@link FactoryConfigurationElement}'s from a collection of {@link FactoryConfiguration}'s
174      *
175      * @param parent
176      *            the parent for each of the create {@link FactoryConfigurationElement}
177      * @param name
178      *            name of the element(s)
179      * @param factoryConfigurations
180      *            the {@link FactoryConfiguration}'s
181      * @return list of {@link FactoryConfigurationElement}
182      */
183     public static List<FactoryConfigurationElement> getAllFactoryElements(NodeElement parent, String name,
184             Collection<? extends FactoryConfiguration> factoryConfigurations) {
185         List<FactoryConfigurationElement> elements = new ArrayList<FactoryConfigurationElement>();
186         for (FactoryConfiguration config : factoryConfigurations) {
187             elements.add(new FactoryConfigurationElement(parent, name, config));
188         }
189         return elements;
190     }
191 
192     /***
193      * {@inheritDoc}
194      */
195     public String getFQName() {
196         return getFQName(this, ".");
197     }
198 
199     /***
200      * {@inheritDoc}
201      */
202     public String getFQName(String delimiter) {
203         return getFQName(this, delimiter);
204     }
205 
206     private static String getFQName(NodeElement element, String delimiter) {
207         LinkedList<NodeElement> hierarchy = new LinkedList<NodeElement>();
208         NodeElement curr = element;
209         while (curr != null) {
210             hierarchy.addFirst(curr);
211             curr = curr.getParent();
212         }
213         StringBuilder sb = new StringBuilder();
214         while (!hierarchy.isEmpty()) {
215             sb.append(hierarchy.removeFirst().getName());
216             if (!hierarchy.isEmpty()) {
217                 sb.append(delimiter);
218             }
219         }
220         return sb.toString();
221     }
222 
223     /***
224      * {@inheritDoc}
225      */
226     @Override
227     public int hashCode() {
228         final int prime = 31;
229         int result = 1;
230         result = prime * result + ((getFQName() == null) ? 0 : getFQName().hashCode());
231         return result;
232     }
233 
234     /***
235      * {@inheritDoc}
236      */
237     @Override
238     public boolean equals(Object obj) {
239         if (this == obj) {
240             return true;
241         }
242         if (obj == null) {
243             return false;
244         }
245         if (!(obj instanceof NodeElement)) {
246             return false;
247         }
248         NodeElement other = (NodeElement) obj;
249         if (getFQName() == null) {
250             if (other.getFQName() != null) {
251                 return false;
252             }
253         } else if (!getFQName().equals(other.getFQName())) {
254             return false;
255         }
256         return true;
257     }
258 
259     /***
260      * {@inheritDoc}
261      */
262     @Override
263     public String toString() {
264         return "AbstractElement [FQName=" + getFQName() + "]";
265     }
266 
267     /***
268      * {@inheritDoc}
269      */
270     public void accept(NodeElementVisitor visitor) {
271         visitor.visit(this);
272     }
273 
274 }