View Javadoc

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