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  /***
20   * Implementation of the {@link NodeAttribute} interface
21   *
22   * @author Abhishek Sanoujam
23   *
24   */
25  public class SimpleNodeAttribute implements NodeAttribute {
26  
27      private final String name;
28      private String value;
29      private String defaultValue;
30      private boolean optional = true;
31  
32      /***
33       * Constructor accepting the name of the attribute
34       *
35       * @param name
36       *            the name of the attribute
37       */
38      public SimpleNodeAttribute(String name) {
39          this(name, (String) null);
40      }
41  
42      /***
43       * Constructor accepting name and Enum value of the attribute
44       *
45       * @param name
46       *            the name of the attribute
47       * @param value
48       *            the Enum value of the attribute
49       */
50      public SimpleNodeAttribute(String name, Enum value) {
51          this(name, value.name().toLowerCase());
52      }
53  
54      /***
55       * Constructor accepting name and int value of the attribute
56       *
57       * @param name
58       *            the name of the attribute
59       * @param value
60       *            the int value of the attribute
61       */
62      public SimpleNodeAttribute(String name, int value) {
63          this(name, String.valueOf(value));
64      }
65  
66      /***
67       * Constructor accepting name and long value of the attribute
68       *
69       * @param name
70       *            the name of the attribute
71       * @param value
72       *            the long value of the attribute
73       */
74      public SimpleNodeAttribute(String name, long value) {
75          this(name, String.valueOf(value));
76      }
77  
78      /***
79       * Constructor accepting name and boolean value of the attribute
80       *
81       * @param name
82       *            the name of the attribute
83       * @param value
84       *            the boolean value of the attribute
85       */
86      public SimpleNodeAttribute(String name, boolean value) {
87          this(name, String.valueOf(value));
88      }
89  
90      /***
91       * Constructor accepting name and String value of the attribute
92       *
93       * @param name
94       *            the name of the attribute
95       * @param value
96       *            the String value of the attribute
97       */
98      public SimpleNodeAttribute(String name, String value) {
99          this.name = name;
100         this.value = value;
101     }
102 
103     /***
104      * {@inheritDoc}
105      */
106     public String getName() {
107         return name;
108     }
109 
110     /***
111      * {@inheritDoc}
112      */
113     public String getValue() {
114         return value;
115     }
116 
117     /***
118      * {@inheritDoc}
119      */
120     public boolean isOptional() {
121         return optional;
122     }
123 
124     /***
125      * {@inheritDoc}
126      */
127     public void setOptional(boolean optional) {
128         this.optional = optional;
129     }
130 
131     /***
132      * {@inheritDoc}
133      */
134     public String getDefaultValue() {
135         return defaultValue;
136     }
137 
138     /***
139      * {@inheritDoc}
140      */
141     public void setDefaultValue(String defaultValue) {
142         this.defaultValue = defaultValue;
143     }
144 
145     /***
146      * {@inheritDoc}
147      */
148     public void setValue(String value) {
149         this.value = value;
150     }
151 
152     /***
153      * {@inheritDoc}
154      */
155     @Override
156     public int hashCode() {
157         final int prime = 31;
158         int result = 1;
159         result = prime * result + ((name == null) ? 0 : name.hashCode());
160         return result;
161     }
162 
163     /***
164      * {@inheritDoc}
165      */
166     @Override
167     public boolean equals(Object obj) {
168         if (this == obj) {
169             return true;
170         }
171         if (obj == null) {
172             return false;
173         }
174         if (!(obj instanceof NodeAttribute)) {
175             return false;
176         }
177         NodeAttribute other = (NodeAttribute) obj;
178         if (name == null) {
179             if (other.getName() != null) {
180                 return false;
181             }
182         } else if (!name.equals(other.getName())) {
183             return false;
184         }
185         return true;
186     }
187 
188     /***
189      * {@inheritDoc}
190      */
191     @Override
192     public String toString() {
193         return "SimpleAttribute [name=" + name + "]";
194     }
195 
196     /***
197      * {@inheritDoc}
198      */
199     public SimpleNodeAttribute optional(boolean optional) {
200         this.optional = optional;
201         return this;
202     }
203 
204     /***
205      * {@inheritDoc}
206      */
207     public SimpleNodeAttribute defaultValue(String defaultValue) {
208         this.defaultValue = defaultValue;
209         return this;
210     }
211 
212     /***
213      * Same as {@link #defaultValue(String)} using String.valueOf(defaultValue)
214      *
215      * @param defaultValue
216      *            the default value
217      * @return the same instance
218      */
219     public SimpleNodeAttribute defaultValue(boolean defaultValue) {
220         return this.defaultValue(String.valueOf(defaultValue));
221     }
222 
223     /***
224      * Same as {@link #defaultValue(String)} using String.valueOf(defaultValue)
225      *
226      * @param defaultValue
227      *            the default value
228      * @return the same instance
229      */
230     public SimpleNodeAttribute defaultValue(int defaultValue) {
231         return this.defaultValue(String.valueOf(defaultValue));
232     }
233 
234     /***
235      * Same as {@link #defaultValue(String)} using String.valueOf(defaultValue)
236      *
237      * @param defaultValue
238      *            the default value
239      * @return the same instance
240      */
241     public SimpleNodeAttribute defaultValue(Enum defaultValue) {
242         return this.defaultValue(defaultValue.name().toLowerCase());
243     }
244 
245     /***
246      * Same as {@link #defaultValue(String)} using String.valueOf(defaultValue)
247      *
248      * @param defaultValue
249      *            the default value
250      * @return the same instance
251      */
252     public SimpleNodeAttribute defaultValue(long defaultValue) {
253         return this.defaultValue(String.valueOf(defaultValue));
254     }
255 
256 }