View Javadoc

1   /*
2    * Copyright 2006-2012 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.util.report;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.NullArgumentException;
25  
26  import de.smartics.maven.util.report.link.ExternalReport;
27  import de.smartics.maven.util.report.link.ExternalReportFactory;
28  import de.smartics.maven.util.report.link.LinkConstructorStrategy;
29  import de.smartics.maven.util.report.link.ReportId;
30  import de.smartics.util.lang.Arguments;
31  
32  /**
33   * Manages report instances.
34   */
35  public class ExternalReportReferences
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * An empty report reference instance that prohibits adding information.
43     */
44    public static final ExternalReportReferences EMPTY =
45        new ExternalReportReferences(null)
46        {
47          public boolean registerReport(final boolean create,
48              final ReportId reportId, final LinkConstructorStrategy strategy)
49          {
50            throw new UnsupportedOperationException(
51                "It is not allowed to add to the empty report references.");
52          }
53  
54          public void addReportId(final ReportId reportId)
55          {
56            throw new UnsupportedOperationException(
57                "It is not allowed to add report IDs.");
58          }
59        };
60  
61    // --- members --------------------------------------------------------------
62  
63    /**
64     * The factory to create new reports.
65     */
66    private final ExternalReportFactory factory;
67  
68    /**
69     * The map of all registered reports.
70     */
71    private final Map<ReportId, ExternalReport> reports =
72        new HashMap<ReportId, ExternalReport>();
73  
74    /**
75     * The ordered list of report IDs to render.
76     */
77    private final List<ReportId> reportIds = new ArrayList<ReportId>();
78  
79    // ****************************** Initializer *******************************
80  
81    // ****************************** Constructors ******************************
82  
83    /**
84     * Default constructor.
85     */
86    public ExternalReportReferences(final ExternalReportFactory factory)
87    {
88      this.factory = factory;
89    }
90  
91    // ****************************** Inner Classes *****************************
92  
93    // ********************************* Methods ********************************
94  
95    // --- init -----------------------------------------------------------------
96  
97    // --- get&set --------------------------------------------------------------
98  
99    /**
100    * Adds the report ID to the list of IDs to render report references.
101    *
102    * @param reportId the report ID to add.
103    * @throws NullArgumentException if {@code reportId} is <code>null</code>.
104    */
105   public void addReportId(final ReportId reportId) throws NullArgumentException
106   {
107     Arguments.checkNotNull("reportId", reportId);
108 
109     reportIds.add(reportId);
110   }
111 
112   /**
113    * Returns the list of report IDs that are to referenced.
114    *
115    * @return the list of report IDs
116    */
117   public List<ReportId> getReportIds()
118   {
119     return Collections.unmodifiableList(reportIds);
120   }
121 
122   /**
123    * Returns the ordered list of report IDs to render.
124    *
125    * @return the ordered list of report IDs to render.
126    */
127   public List<ExternalReport> getReports()
128   {
129     return getReports(getReportIds());
130   }
131 
132   // --- business -------------------------------------------------------------
133 
134   /**
135    * Registers a report with the given identifier and strategy.
136    * <p>
137    * Note that no report will be registered, if the factory of this instance is
138    * not set.
139    * </p>
140    *
141    * @param reportId the identifier of the report to register.
142    * @param strategy the link constructor strategy to use.
143    * @return <code>true</code> if the report has been created and registered,
144    *         <code>false</code> otherwise.
145    */
146   public boolean registerReport(final ReportId reportId,
147       final LinkConstructorStrategy strategy)
148   {
149     return registerReport(true, reportId, strategy);
150   }
151 
152   /**
153    * Registers a report with the given identifier and strategy with a guard. The
154    * guard allows to conveniently call this method without the burden of
155    * specifying an if block on the caller's side.
156    * <p>
157    * Note that no report will be registered, if the factory of this instance is
158    * not set.
159    * </p>
160    *
161    * @param create convenience flag to not register the instance.
162    * @param reportId the identifier of the report to register.
163    * @param strategy the link constructor strategy to use.
164    * @return <code>true</code> if the report has been created and registered,
165    *         <code>false</code> otherwise.
166    */
167   public boolean registerReport(final boolean create, final ReportId reportId,
168       final LinkConstructorStrategy strategy)
169   {
170     if (create && factory != null)
171     {
172       Arguments.checkNotNull("reportId", reportId);
173 
174       final ExternalReport report =
175           factory.createExternalReport(reportId, strategy);
176       final boolean created = report != null;
177       if (created)
178       {
179         reports.put(reportId, report);
180       }
181       return created;
182     }
183 
184     return false;
185   }
186 
187   /**
188    * Returns the report with the given ID.
189    *
190    * @param reportId the ID of the requested report.
191    * @return the requested report instance or <code>null</code> if the ID does
192    *         not reference to such an instance.
193    */
194   public ExternalReport get(final ReportId reportId)
195   {
196     return reports.get(reportId);
197   }
198 
199   /**
200    * Returns the list of reports with the given IDs.
201    *
202    * @param reportIds the IDs of the requested reports.
203    * @return the list of reports, which may be empty, but is never
204    *         <code>null</code>.
205    */
206   public List<ExternalReport> getReports(final List<ReportId> reportIds)
207   {
208     final List<ExternalReport> reports =
209         new ArrayList<ExternalReport>(reportIds.size());
210 
211     for (final ReportId reportId : reportIds)
212     {
213       reports.add(get(reportId));
214     }
215 
216     return reports;
217   }
218 
219   // --- object basics --------------------------------------------------------
220 
221 }