1 /*
2 * Copyright 2008-2010 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
17 package de.smartics.maven.issues.bugzilla;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.eclipse.mylyn.tasks.core.data.TaskData;
27
28 /**
29 * Provides information for sections within a version.
30 *
31 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
32 * @version $Revision:591 $
33 */
34 public class Sections implements Iterable<Sections.Section>
35 {
36 // ********************************* Fields *********************************
37
38 // --- constants ------------------------------------------------------------
39
40 // --- members --------------------------------------------------------------
41
42 /**
43 * The map stores the bugs in the order read from the issue management system
44 * in different sections.
45 */
46 private final Map<String, List<TaskData>> sectionsMap =
47 new LinkedHashMap<String, List<TaskData>>();
48
49 // ****************************** Initializer *******************************
50
51 // ****************************** Constructors ******************************
52
53 /**
54 * Default constructor.
55 *
56 * @param sectionNames the list of sections to be put to the sections
57 * instance. Sections not mentioned here will be ignored.
58 */
59 public Sections(final List<String> sectionNames)
60 {
61 init(sectionNames);
62 }
63
64 // ****************************** Inner Classes *****************************
65
66 /**
67 * Container for the tasks belonging to one section.
68 */
69 public static final class Section
70 {
71 // ******************************** Fields ********************************
72
73 // --- constants ----------------------------------------------------------
74
75 // --- members ------------------------------------------------------------
76
77 /**
78 * The name of the section.
79 */
80 private final String sectionName;
81
82 /**
83 * The tasks belonging to the section.
84 */
85 private final List<TaskData> tasks;
86
87 // ***************************** Initializer ******************************
88
89 // ***************************** Constructors *****************************
90
91 /**
92 * Default constructor.
93 *
94 * @param sectionName the name of the section.
95 * @param tasks the tasks belonging to the section.
96 */
97 private Section(final String sectionName, final List<TaskData> tasks)
98 {
99 this.sectionName = sectionName;
100 this.tasks = Collections.unmodifiableList(tasks);
101 }
102
103 // ***************************** Inner Classes ****************************
104
105 // ******************************** Methods *******************************
106
107 // --- init ---------------------------------------------------------------
108
109 // --- get&set ------------------------------------------------------------
110
111 /**
112 * Returns the name of the section.
113 *
114 * @return the name of the section.
115 */
116 public String getSectionName()
117 {
118 return sectionName;
119 }
120
121 /**
122 * Returns the tasks belonging to the section.
123 *
124 * @return the tasks belonging to the section.
125 */
126 public List<TaskData> getTasks()
127 {
128 return tasks;
129 }
130
131 // --- business -----------------------------------------------------------
132
133 // --- object basics ------------------------------------------------------
134 }
135
136 /**
137 * Implementation of the iterator over sections using the iterator of the
138 * underlying map.
139 */
140 private final class SectionsIterator implements Iterator<Section>
141 {
142 // ******************************** Fields ********************************
143
144 // --- constants ----------------------------------------------------------
145
146 // --- members ------------------------------------------------------------
147
148 /**
149 * The iterator on the map of section information.
150 */
151 private final Iterator<Map.Entry<String, List<TaskData>>> i = sectionsMap
152 .entrySet().iterator();
153
154 // ***************************** Initializer ******************************
155
156 // ***************************** Constructors *****************************
157
158 // ***************************** Inner Classes ****************************
159
160 // ******************************** Methods *******************************
161
162 // --- init ---------------------------------------------------------------
163
164 // --- get&set ------------------------------------------------------------
165
166 // --- business -----------------------------------------------------------
167
168 /**
169 * {@inheritDoc}
170 */
171 public boolean hasNext()
172 {
173 return i.hasNext();
174 }
175
176 /**
177 * {@inheritDoc}
178 */
179 public Section next()
180 {
181 final Map.Entry<String, List<TaskData>> entry = i.next();
182 return new Section(entry.getKey(), entry.getValue()); // NOPMD
183 }
184
185 /**
186 * Not supported.
187 *
188 * @throws UnsupportedOperationException always.
189 */
190 public void remove() throws UnsupportedOperationException
191 {
192 throw new UnsupportedOperationException(
193 "Removing sections not supported.");
194 }
195
196 // --- object basics ------------------------------------------------------
197
198 }
199
200 // ********************************* Methods ********************************
201
202 // --- init -----------------------------------------------------------------
203
204 /**
205 * Initializes the data structures of this instance.
206 *
207 * @param sectionNames the list of sections to be put to the sections
208 * instance. Sections not mentioned here will be ignored.
209 */
210 private void init(final List<String> sectionNames)
211 {
212 for (String section : sectionNames)
213 {
214 sectionsMap.put(section, new ArrayList<TaskData>()); // NOPMD
215 }
216 }
217
218 // --- get&set --------------------------------------------------------------
219
220 /**
221 * {@inheritDoc}
222 *
223 * @see java.lang.Iterable#iterator()
224 */
225 public Iterator<Section> iterator()
226 {
227 return new SectionsIterator();
228 }
229
230 // --- business -------------------------------------------------------------
231
232 /**
233 * Adds the given <code>taskData</code> to the given section if the
234 * <code>sectionName</code> is contained in this instance.
235 *
236 * @param sectionName the name of the section to add to.
237 * @param taskData the information to add to the section.
238 * @return <code>true</code> if the element has been added to the requested
239 * section, <code>false</code> if not because the section requested is
240 * not managed by this instance.
241 */
242 public boolean addToSection(final String sectionName, final TaskData taskData)
243 {
244 if (containsKey(sectionName))
245 {
246 sectionsMap.get(sectionName).add(taskData);
247 return true;
248 }
249 return false;
250 }
251
252 /**
253 * Checks if the given <code>sectionName</code> is contained in this section
254 * instance.
255 *
256 * @param sectionName the name of the section to check if contained.
257 * @return <code>true</code> if the section is part of this instance,
258 * <code>false</code> otherwise.
259 */
260 public boolean containsKey(final String sectionName)
261 {
262 return sectionsMap.containsKey(sectionName);
263 }
264
265 // --- object basics --------------------------------------------------------
266
267 }