View Javadoc

1   /*
2    * Copyright 2012-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.properties.report.data;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.ObjectUtils;
22  
23  /**
24   * Provides comments for the values of a property.
25   */
26  public final class ValueComment
27  {
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    // --- members --------------------------------------------------------------
33  
34    /**
35     * The summary of the type of the values. May be <code>null</code>.
36     */
37    private final String summary;
38  
39    /**
40     * The map of values of the type to their comments.
41     */
42    private final Map<Object, String> valueCommentMap =
43        new HashMap<Object, String>();
44  
45    // ****************************** Initializer *******************************
46  
47    // ****************************** Constructors ******************************
48  
49    /**
50     * Default constructor.
51     *
52     * @param summary the summary of the type of the values.
53     */
54    public ValueComment(final String summary)
55    {
56      this.summary = summary;
57    }
58  
59    // ****************************** Inner Classes *****************************
60  
61    // ********************************* Methods ********************************
62  
63    // --- init -----------------------------------------------------------------
64  
65    // --- get&set --------------------------------------------------------------
66  
67    /**
68     * Returns the summary of the type of the values. May be <code>null</code>.
69     *
70     * @return the summary of the type of the values.
71     */
72    public String getSummary()
73    {
74      return summary;
75    }
76  
77    // --- business -------------------------------------------------------------
78  
79    /**
80     * Adds the comment for the given value.
81     *
82     * @param value the value whose comment is to be added.
83     * @param comment the comment to the value.
84     */
85    public void addValueComment(final Object value, final String comment)
86    {
87      valueCommentMap.put(value, comment);
88    }
89  
90    /**
91     * Returns the comment for the given value.
92     *
93     * @param value the value whose comment is requested.
94     * @return the comment to the value or <code>null</code> if there is no
95     *         comment for taht value.
96     */
97    public String getValueComment(final Object value)
98    {
99      String comment = valueCommentMap.get(value);
100     if (comment == null)
101     {
102       comment = valueCommentMap.get(ObjectUtils.toString(value, null));
103     }
104     return comment;
105   }
106 
107   // --- object basics --------------------------------------------------------
108 
109 }