KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxtags > helpers > Item


1 /**
2  * Copyright 2007 Jens Kapitza
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 package org.ajaxtags.helpers;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.apache.commons.lang.builder.ToStringBuilder;
23
24 /**
25  * A generic item class, basically representing a name-value pair.
26  *
27  * @author Darren L. Spurgeon
28  * @author Jens Kapitza
29  * @version $Revision: 1.6 $ $Date: 2007/07/22 18:04:50 $ $Author: jenskapitza $
30  */

31 class Item<T> {
32
33     /**
34      * set all attributes
35      *
36      * @param attributes
37      * the attributes to set
38      */

39     public void setAllAttributes(Map JavaDoc<String JavaDoc, String JavaDoc> attributes) {
40         if (attributes != null) {
41             for (String JavaDoc key : attributes.keySet()) {
42                 setAttributes(key, attributes.get(key));
43             }
44         }
45     }
46
47     /**
48      *
49      * @return the key set of the attributes
50      */

51     public Set JavaDoc<String JavaDoc> getAttributeKeySet() {
52         return this.attributes.keySet();
53     }
54
55     private Map JavaDoc<String JavaDoc, String JavaDoc> attributes = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
56
57     /**
58      * removes an attribute
59      *
60      * @param name
61      * the name of attribute
62      */

63     public void removeAttribute(final String JavaDoc name) {
64         this.attributes.remove(name);
65     }
66
67     /**
68      * clear the attributes
69      */

70     public void clearAttribute() {
71         this.attributes.clear();
72     }
73
74     /**
75      * set an attribute to extend the item
76      *
77      * @param name
78      * the name for the attrubute
79      * @param value
80      * the value for the attribute
81      */

82     public void setAttributes(final String JavaDoc name, final String JavaDoc value) {
83         setAttributes(name, value, false);
84     }
85
86     /**
87      * set an attribute to extend the item
88      *
89      * @param name
90      * the name for the attrubute
91      * @param value
92      * the value for the attribute
93      * @param evenIfNull
94      * set attribute even if it is null
95      */

96     public void setAttributes(final String JavaDoc name, final String JavaDoc value,
97             boolean evenIfNull) {
98         if (value == null && !evenIfNull) {
99             return; // don't set null attributes
100
}
101         this.attributes.put(name.toLowerCase(), value);
102     }
103
104     /**
105      * read the attribute value
106      *
107      * @param name
108      * the attribute name
109      * @return the value of attribute <code>name</code>
110      */

111     public String JavaDoc getAttributeValue(final String JavaDoc name) {
112         return this.attributes.get(name);
113     }
114
115     protected String JavaDoc name;
116
117     protected T value;
118
119     protected boolean asData;
120
121     /**
122      * Constructor for Item.
123      */

124     public Item() {
125         super();
126     }
127
128     /**
129      * Constructor for Item.
130      *
131      * @param name
132      * the name for the item
133      * @param value
134      * the value
135      * @param asData
136      * response as CDATA
137      */

138     public Item(String JavaDoc name, T value, boolean asData) {
139         super();
140         this.name = name;
141         this.value = value;
142         this.asData = asData;
143     }
144
145     /**
146      * @return Returns the name.
147      */

148     public String JavaDoc getName() {
149         return this.name;
150     }
151
152     /**
153      * @param name
154      * The name to set.
155      */

156     public void setName(String JavaDoc name) {
157         this.name = name;
158     }
159
160     /**
161      * @return Returns the value.
162      */

163     public T getValue() {
164         return this.value;
165     }
166
167     /**
168      * @param value
169      * The value to set.
170      */

171     public void setValue(T value) {
172         this.value = value;
173     }
174
175     /**
176      * @return Returns the asCData.
177      */

178     public boolean isAsCData() {
179         return this.asData;
180     }
181
182     /**
183      * @param asData
184      * The asData to set.
185      */

186     public void setAsData(boolean asData) {
187         this.asData = asData;
188     }
189
190     /**
191      * @see java.lang.Object#toString()
192      */

193     @Override JavaDoc
194     public String JavaDoc toString() {
195         ToStringBuilder builder = new ToStringBuilder(this).append("name",
196                 this.name).append("value", this.value).append("asData",
197                 this.asData);
198         // alle attribute hinzufuegen
199
for (String JavaDoc key : this.attributes.keySet()) {
200             builder.append(key, this.attributes.get(key));
201         }
202         return builder.toString();
203     }
204
205 }
Popular Tags