View Javadoc

1   /*
2    * Copyright 2007-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.exceptions.report.data;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import javax.annotation.concurrent.NotThreadSafe;
23  
24  import com.google.common.collect.ArrayListMultimap;
25  
26  import de.smartics.exceptions.report.app.ReportException;
27  
28  /**
29   * Base implementation of an exception codes report that stores all items and
30   * reported problems in-memory.
31   */
32  @NotThreadSafe
33  public abstract class AbstractExceptionCodesReport implements
34      ExceptionCodesReport
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The list of problems encountered while parsing sources and fetching report
44     * information.
45     */
46    private final List<ReportProblem> problems = new ArrayList<ReportProblem>();
47  
48    /**
49     * Map to recognize unique key constraint violations.
50     */
51    private final ArrayListMultimap<String, ExceptionCodeReportItem> keyUniquenessMap =
52        ArrayListMultimap.create();
53  
54    // ****************************** Initializer *******************************
55  
56    // ****************************** Constructors ******************************
57  
58    /**
59     * Default constructor.
60     */
61    protected AbstractExceptionCodesReport()
62    {
63    }
64  
65    // ****************************** Inner Classes *****************************
66  
67    // ********************************* Methods ********************************
68  
69    // --- init -----------------------------------------------------------------
70  
71    // --- get&set --------------------------------------------------------------
72  
73    /**
74     * {@inheritDoc}
75     *
76     * @see de.smartics.properties.report.data.PropertyReport#getProblems()
77     */
78    public final List<ReportProblem> getProblems()
79    {
80      return Collections.unmodifiableList(problems);
81    }
82  
83    // --- business -------------------------------------------------------------
84  
85    @Override
86    public final void addProblem(final ReportProblem problem)
87    {
88      problems.add(problem);
89    }
90  
91    /**
92     * {@inheritDoc}
93     *
94     * @see de.smartics.properties.report.data.PropertyReport#hasProblems()
95     */
96    public final boolean hasProblems()
97    {
98      return !problems.isEmpty();
99    }
100 
101   /**
102    * {@inheritDoc}
103    * <p>
104    * Registers a report item to manage the alias mapping and check for
105    * uniqueness constraint violations.
106    * </p>
107    *
108    * @see de.smartics.properties.report.data.PropertyReport#handle(de.smartics.properties.report.data.PropertyReportItem)
109    */
110   // CHECKSTYLE:OFF Method can be overridden by sub classes.
111   @Override
112   public void add(final ExceptionCodeReportItem item) throws ReportException
113   // CHECKSTYLE:ON
114   {
115     register(item);
116   }
117 
118   /**
119    * Registers a report item to check for uniqueness constraint violations.
120    *
121    * @param item the report item to manage.
122    */
123   protected final void register(final ExceptionCodeReportItem item)
124   {
125     final String name = item.getName();
126 
127     keyUniquenessMap.put(name, item);
128     checkForUniqueness(name);
129   }
130 
131   private void checkForUniqueness(final String name)
132   {
133     final List<ExceptionCodeReportItem> items = keyUniquenessMap.get(name);
134     if (items.size() > 1)
135     {
136       final StringBuilder buffer = new StringBuilder(1024);
137       buffer.append("Duplicate exception code '" + name + "' found:");
138       for (final ExceptionCodeReportItem duplicate : items)
139       {
140         buffer.append("\n -> ").append(duplicate.getSource());
141       }
142       final String message = buffer.toString();
143       final ReportProblem problem = new ReportProblem(message);
144       addProblem(problem);
145     }
146   }
147 
148   // --- object basics --------------------------------------------------------
149 
150 }