Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NodeState |
|
| 1.4;1.4 |
1 | package org.jtheque.core.managers.state; | |
2 | ||
3 | /* | |
4 | * This file is part of JTheque. | |
5 | * | |
6 | * JTheque is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License. | |
9 | * | |
10 | * JTheque is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | import org.jtheque.utils.StringUtils; | |
20 | import org.jtheque.utils.collections.CollectionUtils; | |
21 | ||
22 | import java.util.ArrayList; | |
23 | import java.util.Collection; | |
24 | ||
25 | /** | |
26 | * A node of a state. | |
27 | * | |
28 | * @author Baptiste Wicht | |
29 | */ | |
30 | public final class NodeState { | |
31 | private final String name; | |
32 | private Collection<NodeState> childrens; | |
33 | private String text; | |
34 | private Collection<NodeStateAttribute> attributes; | |
35 | ||
36 | /** | |
37 | * Construct a new NodeState. | |
38 | * | |
39 | * @param name The name of the node. | |
40 | */ | |
41 | public NodeState(String name) { | |
42 | 0 | super(); |
43 | ||
44 | 0 | this.name = name; |
45 | ||
46 | 0 | childrens = new ArrayList<NodeState>(10); |
47 | 0 | attributes = new ArrayList<NodeStateAttribute>(10); |
48 | 0 | } |
49 | ||
50 | /** | |
51 | * Construct a new NodeState. | |
52 | * | |
53 | * @param name The name of the node. | |
54 | * @param text The text of the node. | |
55 | */ | |
56 | public NodeState(String name, String text) { | |
57 | 0 | super(); |
58 | ||
59 | 0 | this.name = name; |
60 | 0 | this.text = text; |
61 | ||
62 | 0 | childrens = new ArrayList<NodeState>(10); |
63 | 0 | attributes = new ArrayList<NodeStateAttribute>(10); |
64 | 0 | } |
65 | ||
66 | /** | |
67 | * Return the name of the node. | |
68 | * | |
69 | * @return The name. | |
70 | */ | |
71 | public String getName() { | |
72 | 0 | return name; |
73 | } | |
74 | ||
75 | /** | |
76 | * Return the childrens of the node. | |
77 | * | |
78 | * @return A List containing all the NodeState children. | |
79 | */ | |
80 | public Collection<NodeState> getChildrens() { | |
81 | 0 | return childrens; |
82 | } | |
83 | ||
84 | /** | |
85 | * Set the childrens of the node. | |
86 | * | |
87 | * @param childrens The childrens. | |
88 | */ | |
89 | public void setChildrens(Collection<NodeState> childrens) { | |
90 | 0 | this.childrens = CollectionUtils.copyOf(childrens); |
91 | 0 | } |
92 | ||
93 | /** | |
94 | * Add a simple child value. | |
95 | * | |
96 | * @param name The name of the node. | |
97 | * @param value The value of the node. | |
98 | */ | |
99 | public void addSimpleChildValue(String name, String value) { | |
100 | 0 | childrens.add(new NodeState(name, value)); |
101 | 0 | } |
102 | ||
103 | /** | |
104 | * Return the text of the node. | |
105 | * | |
106 | * @return The text of the node. | |
107 | */ | |
108 | public String getText() { | |
109 | 0 | return text; |
110 | } | |
111 | ||
112 | /** | |
113 | * Set the text of the node. | |
114 | * | |
115 | * @param text The text of the node. | |
116 | */ | |
117 | public void setText(String text) { | |
118 | 0 | this.text = text; |
119 | 0 | } |
120 | ||
121 | /** | |
122 | * Return the attributes of the node. | |
123 | * | |
124 | * @return A List containing all the attributes. | |
125 | */ | |
126 | public Collection<NodeStateAttribute> getAttributes() { | |
127 | 0 | return attributes; |
128 | } | |
129 | ||
130 | /** | |
131 | * Set the attributes of the node. | |
132 | * | |
133 | * @param attributes A List containing all the attributes. | |
134 | */ | |
135 | public void setAttributes(Collection<NodeStateAttribute> attributes) { | |
136 | 0 | this.attributes = CollectionUtils.copyOf(attributes); |
137 | 0 | } |
138 | ||
139 | /** | |
140 | * Indicate if the node has children or not. | |
141 | * | |
142 | * @return true if the node has children else false. | |
143 | */ | |
144 | public boolean hasChildren() { | |
145 | 0 | return childrens != null && !childrens.isEmpty(); |
146 | } | |
147 | ||
148 | /** | |
149 | * Indicate if the node has attribute or not. | |
150 | * | |
151 | * @return true if the node has attribute else false. | |
152 | */ | |
153 | public boolean hasAttribute() { | |
154 | 0 | return attributes != null && !attributes.isEmpty(); |
155 | } | |
156 | ||
157 | /** | |
158 | * Set an attribute. | |
159 | * | |
160 | * @param key The key of the attribute. | |
161 | * @param value The value of the attribute. | |
162 | */ | |
163 | public void setAttribute(String key, String value) { | |
164 | 0 | NodeStateAttribute attribute = new NodeStateAttribute(key, value); |
165 | ||
166 | 0 | attributes.add(attribute); |
167 | 0 | } |
168 | ||
169 | /** | |
170 | * Return the attribute value. | |
171 | * | |
172 | * @param key The name of the attribute. | |
173 | * | |
174 | * @return The value of the attribute or null if the attribute doesn't exist. | |
175 | */ | |
176 | public String getAttributeValue(String key) { | |
177 | 0 | String value = null; |
178 | ||
179 | 0 | for (NodeStateAttribute attribute : attributes) { |
180 | 0 | if (attribute.getKey().equals(key)) { |
181 | 0 | value = attribute.getValue(); |
182 | 0 | break; |
183 | } | |
184 | } | |
185 | ||
186 | 0 | return value; |
187 | } | |
188 | ||
189 | /** | |
190 | * Return the integer attribute value. | |
191 | * | |
192 | * @param key The name of the attribute. | |
193 | * | |
194 | * @return The int value of the attribute or 0 if the attribute doesn't exist. | |
195 | */ | |
196 | public int getIntAttributeValue(String key){ | |
197 | 0 | String value = getAttributeValue(key); |
198 | ||
199 | 0 | if(StringUtils.isNotEmpty(value)){ |
200 | 0 | return Integer.parseInt(value); |
201 | } | |
202 | ||
203 | 0 | return 0; |
204 | } | |
205 | } |