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.api.core.domain;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.LinkedHashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  /**
27   * Provides comments for the values of a property.
28   */
29  public final class PropertyValueComment implements Serializable
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The class version identifier.
39     * <p>
40     * The value of this constant is {@value}.
41     * </p>
42     */
43    private static final long serialVersionUID = 1L;
44  
45    /**
46     * The optional summary of the type of the values. May be <code>null</code>.
47     *
48     * @serial
49     */
50    private final String summary;
51  
52    /**
53     * The map of values of the type to their comments.
54     *
55     * @serial
56     */
57    private final Map<Serializable, String> valueCommentMap =
58        new LinkedHashMap<Serializable, String>();
59  
60    // ****************************** Initializer *******************************
61  
62    // ****************************** Constructors ******************************
63  
64    /**
65     * Default constructor.
66     *
67     * @param summary the summary of the type of the values.
68     */
69    public PropertyValueComment(final String summary)
70    {
71      this.summary = summary;
72    }
73  
74    // ****************************** Inner Classes *****************************
75  
76    // ********************************* Methods ********************************
77  
78    // --- init -----------------------------------------------------------------
79  
80    // --- get&set --------------------------------------------------------------
81  
82    /**
83     * Returns the summary of the type of the values. May be <code>null</code>.
84     *
85     * @return the summary of the type of the values.
86     */
87    public String getSummary()
88    {
89      return summary;
90    }
91  
92    // --- business -------------------------------------------------------------
93  
94    /**
95     * Adds the comment for the given value.
96     *
97     * @param value the value whose comment is to be added.
98     * @param comment the comment to the value.
99     */
100   public void addValueComment(final Serializable value, final String comment)
101   {
102     valueCommentMap.put(value, comment);
103   }
104 
105   /**
106    * Returns the comment for the given value.
107    *
108    * @param value the value whose comment is requested.
109    * @return the comment to the value or <code>null</code> if there is no
110    *         comment for taht value.
111    */
112   public String getValueComment(final Serializable value)
113   {
114     final String comment = valueCommentMap.get(value);
115     return comment;
116   }
117 
118   /**
119    * Returns the list of values the instance provides comments for.
120    *
121    * @return the list of values the instance provides comments for.
122    */
123   public List<Serializable> getValues()
124   {
125     return new ArrayList<Serializable>(valueCommentMap.keySet());
126   }
127 
128   // --- object basics --------------------------------------------------------
129 
130   /**
131    * Returns the string representation of the object.
132    *
133    * @return the string representation of the object.
134    */
135   @Override
136   public String toString()
137   {
138     final StringBuilder buffer = new StringBuilder();
139 
140     if (StringUtils.isNotBlank(summary))
141     {
142       buffer.append(summary).append(':');
143     }
144 
145     for (final Map.Entry<Serializable, String> entry : valueCommentMap
146         .entrySet())
147     {
148       buffer.append(' ').append(entry.getKey()).append('=')
149           .append(entry.getValue());
150     }
151 
152     return buffer.toString();
153   }
154 }