Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertyValidationException |
|
|
1.7142857142857142;1,714 |
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.List; |
|
20 | ||
21 | ||
22 | ||
23 | /** |
|
24 | * Signals that the value does not match the given constraints. |
|
25 | */ |
|
26 | public class PropertyValidationException extends PropertyDescriptorException |
|
27 | { |
|
28 | // ********************************* Fields ********************************* |
|
29 | ||
30 | // --- constants ------------------------------------------------------------ |
|
31 | ||
32 | /** |
|
33 | * The class version identifier. |
|
34 | * <p> |
|
35 | * The value of this constant is {@value}. |
|
36 | */ |
|
37 | private static final long serialVersionUID = 1L; |
|
38 | ||
39 | // --- members -------------------------------------------------------------- |
|
40 | ||
41 | /** |
|
42 | * The property constraints that failed to be met. |
|
43 | * |
|
44 | * @serial |
|
45 | */ |
|
46 | private final List<? extends PropertyConstraint<?>> constraints; |
|
47 | ||
48 | /** |
|
49 | * The value of the property that does not meet the constraints. |
|
50 | * |
|
51 | * @serial |
|
52 | */ |
|
53 | private final Serializable value; |
|
54 | ||
55 | // ****************************** Initializer ******************************* |
|
56 | ||
57 | // ****************************** Constructors ****************************** |
|
58 | ||
59 | /** |
|
60 | * Convenience constructor with no message and no root cause. |
|
61 | * |
|
62 | * @param propertyDescriptor the descriptor of the property raising the |
|
63 | * exception. |
|
64 | * @param constraints the property constraints that failed to be met. |
|
65 | * @param value the value of the property that does not meet the constraints. |
|
66 | * If the value is not serializable, its string representation is |
|
67 | * stored. |
|
68 | */ |
|
69 | public PropertyValidationException( |
|
70 | final PropertyDescriptor propertyDescriptor, |
|
71 | final List<? extends PropertyConstraint<?>> constraints, |
|
72 | final Object value) |
|
73 | { |
|
74 | 0 | this(createMessage(propertyDescriptor, constraints, value), |
75 | propertyDescriptor, constraints, value); |
|
76 | 0 | } |
77 | ||
78 | /** |
|
79 | * Convenience constructor with no root cause. |
|
80 | * |
|
81 | * @param message the detail message (which is saved for later retrieval by |
|
82 | * the {@link #getMessage()} method). |
|
83 | * @param propertyDescriptor the descriptor of the property raising the |
|
84 | * exception. |
|
85 | * @param constraints the property constraints that failed to be met. |
|
86 | * @param value the value of the property that does not meet the constraints. |
|
87 | * If the value is not serializable, its string representation is |
|
88 | * stored. |
|
89 | */ |
|
90 | public PropertyValidationException(final String message, |
|
91 | final PropertyDescriptor propertyDescriptor, |
|
92 | final List<? extends PropertyConstraint<?>> constraints, |
|
93 | final Object value) |
|
94 | { |
|
95 | 0 | this(message, null, propertyDescriptor, constraints, value); |
96 | 0 | } |
97 | ||
98 | /** |
|
99 | * Convenience constructor with no message. |
|
100 | * |
|
101 | * @param cause the cause (which is saved for later retrieval by the |
|
102 | * {@link #getCause()} method). (A <tt>null</tt> value is permitted, |
|
103 | * and indicates that the cause is nonexistent or unknown.) |
|
104 | * @param propertyDescriptor the descriptor of the property raising the |
|
105 | * exception. |
|
106 | * @param constraints the property constraints that failed to be met. |
|
107 | * @param value the value of the property that does not meet the constraints. |
|
108 | * If the value is not serializable, its string representation is |
|
109 | * stored. |
|
110 | */ |
|
111 | public PropertyValidationException(final Throwable cause, |
|
112 | final PropertyDescriptor propertyDescriptor, |
|
113 | final List<? extends PropertyConstraint<?>> constraints, |
|
114 | final Object value) |
|
115 | { |
|
116 | 0 | this(null, cause, propertyDescriptor, constraints, value); |
117 | 0 | } |
118 | ||
119 | /** |
|
120 | * Default constructor. |
|
121 | * |
|
122 | * @param message the detail message (which is saved for later retrieval by |
|
123 | * the {@link #getMessage()} method). |
|
124 | * @param cause the cause (which is saved for later retrieval by the |
|
125 | * {@link #getCause()} method). (A <tt>null</tt> value is permitted, |
|
126 | * and indicates that the cause is nonexistent or unknown.) |
|
127 | * @param propertyDescriptor the descriptor of the property raising the |
|
128 | * exception. |
|
129 | * @param constraints the property constraints that failed to be met. |
|
130 | * @param value the value of the property that does not meet the constraints. |
|
131 | * If the value is not serializable, its string representation is |
|
132 | * stored. |
|
133 | */ |
|
134 | public PropertyValidationException(final String message, |
|
135 | final Throwable cause, final PropertyDescriptor propertyDescriptor, |
|
136 | final List<? extends PropertyConstraint<?>> constraints, |
|
137 | final Object value) |
|
138 | { |
|
139 | 0 | super(message, cause, propertyDescriptor); |
140 | ||
141 | 0 | this.constraints = constraints; |
142 | 0 | this.value = |
143 | value instanceof Serializable ? (Serializable) value : String |
|
144 | .valueOf(value); |
|
145 | 0 | } |
146 | ||
147 | // ****************************** Inner Classes ***************************** |
|
148 | ||
149 | // ********************************* Methods ******************************** |
|
150 | ||
151 | // --- init ----------------------------------------------------------------- |
|
152 | ||
153 | private static String createMessage( |
|
154 | final PropertyDescriptor propertyDescriptor, |
|
155 | final List<? extends PropertyConstraint<?>> constraints, |
|
156 | final Object value) |
|
157 | { |
|
158 | 0 | final StringBuilder buffer = new StringBuilder(); |
159 | ||
160 | 0 | buffer.append("Invalid value '" |
161 | + value |
|
162 | + '\'' |
|
163 | + (value != null ? " (" + value.getClass().getSimpleName() |
|
164 | + ')' : "") + " for property '" |
|
165 | + propertyDescriptor.getKey() + "':"); |
|
166 | 0 | if (constraints != null && !constraints.isEmpty()) |
167 | { |
|
168 | 0 | for (final PropertyConstraint<?> constraint : constraints) |
169 | { |
|
170 | 0 | buffer.append('\n').append(constraint.getDescription()); |
171 | } |
|
172 | } |
|
173 | ||
174 | 0 | return buffer.toString(); |
175 | } |
|
176 | ||
177 | // --- get&set -------------------------------------------------------------- |
|
178 | ||
179 | /** |
|
180 | * Returns the property constraint that failed to be met. |
|
181 | * |
|
182 | * @return the property constraint that failed to be met. |
|
183 | */ |
|
184 | public final List<? extends PropertyConstraint<?>> getConstraints() |
|
185 | { |
|
186 | 0 | return constraints; |
187 | } |
|
188 | ||
189 | /** |
|
190 | * Returns the value of the property that does not meet the constraints. |
|
191 | * |
|
192 | * @return the value of the property that does not meet the constraints. |
|
193 | */ |
|
194 | public final Serializable getValue() |
|
195 | { |
|
196 | 0 | return value; |
197 | } |
|
198 | ||
199 | // --- business ------------------------------------------------------------- |
|
200 | ||
201 | // --- object basics -------------------------------------------------------- |
|
202 | ||
203 | } |