1 /*
2 * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.testdoc.report.index;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import net.jcip.annotations.NotThreadSafe;
26
27 import org.apache.commons.lang.StringUtils;
28
29 /**
30 * The implementation provides a static view on sections. The sections, once
31 * added, cannot be modified afterwards.
32 *
33 * @param <T> the type of the items added to this section.
34 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
35 * @version $Revision:591 $
36 */
37 @NotThreadSafe
38 public class StaticSection<T extends Serializable> implements Serializable,
39 Section<T>
40 {
41 // ********************************* Fields *********************************
42
43 // --- constants ------------------------------------------------------------
44
45 /**
46 * The class version identifier.
47 * <p>
48 * The value of this constant is {@value}.
49 */
50 private static final long serialVersionUID = 1L;
51
52 // --- members --------------------------------------------------------------
53
54 /**
55 * The name of this section. Must not be <code>null</code>.
56 *
57 * @serial
58 */
59 private final String name;
60
61 /**
62 * The properties associated with this section.
63 *
64 * @serial
65 */
66 private final Map<String, ? extends Serializable> properties;
67
68 /**
69 * The list of sub sections to this section.
70 *
71 * @serial
72 */
73 private final List<Section<T>> subSections = new ArrayList<Section<T>>();
74
75 /**
76 * The test documentation instances associated with this section.
77 *
78 * @serial
79 */
80 private final List<T> items = new ArrayList<T>();
81
82 // ****************************** Initializer *******************************
83
84 // ****************************** Constructors ******************************
85
86 /**
87 * Convenience constructor with an empty set of properties.
88 *
89 * @param name the name of this section.
90 * @throws IllegalArgumentException if <code>name</code> is blank.
91 */
92 public StaticSection(final String name)
93 {
94 this(name, null);
95 }
96
97 /**
98 * Default constructor.
99 *
100 * @param name the name of this section.
101 * @param properties the properties associated with this section. May be
102 * <code>null</code>.
103 * @throws IllegalArgumentException if <code>name</code> is blank.
104 */
105 public StaticSection(final String name,
106 final Map<String, Serializable> properties)
107 throws IllegalArgumentException
108 {
109 checkArguments(name);
110 this.name = name;
111 this.properties =
112 (properties != null ? new HashMap<String, Serializable>(properties)
113 : new HashMap<String, Serializable>());
114 }
115
116 // ****************************** Inner Classes *****************************
117
118 // ********************************* Methods ********************************
119
120 // --- init -----------------------------------------------------------------
121
122 // --- get&set --------------------------------------------------------------
123
124 private static void checkArguments(final String name)
125 throws IllegalArgumentException
126 {
127 if (StringUtils.isBlank(name))
128 {
129 throw new IllegalArgumentException(
130 "The name of the section must not be blank.");
131 }
132 }
133
134 /**
135 * {@inheritDoc}
136 *
137 * @see de.smartics.testdoc.report.index.Section#getName()
138 */
139 @Override
140 public String getName()
141 {
142 return name;
143 }
144
145 /**
146 * {@inheritDoc}
147 *
148 * @see de.smartics.testdoc.report.index.Section#getProperties()
149 */
150 @Override
151 public Map<String, ? extends Serializable> getProperties()
152 {
153 return properties;
154 }
155
156 // --- business -------------------------------------------------------------
157
158 /**
159 * {@inheritDoc}
160 */
161 @Override
162 public void addSubSection(final Section<T> subSection)
163 {
164 subSections.add(subSection);
165 }
166
167 /**
168 * {@inheritDoc}
169 *
170 * @see de.smartics.testdoc.report.index.Section#getSubSections()
171 */
172 @Override
173 public List<Section<T>> getSubSections()
174 {
175 return Collections.unmodifiableList(subSections);
176 }
177
178 /**
179 * {@inheritDoc}
180 */
181 @Override
182 public void addItem(final T item)
183 {
184 items.add(item);
185 }
186
187 /**
188 * {@inheritDoc}
189 *
190 * @see de.smartics.testdoc.report.index.Section#getItems()
191 */
192 @Override
193 public List<T> getItems()
194 {
195 return Collections.unmodifiableList(items);
196 }
197
198 /**
199 * {@inheritDoc}
200 *
201 * @see de.smartics.testdoc.report.index.Section#isEmpty()
202 */
203 @Override
204 public boolean isEmpty()
205 {
206 if (items.isEmpty())
207 {
208 for (final Section<T> subSection : subSections)
209 {
210 if (!subSection.isEmpty())
211 {
212 return false;
213 }
214 }
215 }
216
217 return true;
218 }
219
220 /**
221 * {@inheritDoc}
222 *
223 * @see de.smartics.testdoc.report.index.Section#containsSubSections()
224 */
225 @Override
226 public boolean containsSubSections()
227 {
228 return !subSections.isEmpty();
229 }
230
231 // --- object basics --------------------------------------------------------
232
233 }