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