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
20
21 /**
22 * Signals that the a read-only property was requested to be updated.
23 */
24 public class ReadOnlyPropertyException extends PropertyDescriptorException
25 {
26 // ********************************* Fields *********************************
27
28 // --- constants ------------------------------------------------------------
29
30 /**
31 * The class version identifier.
32 * <p>
33 * The value of this constant is {@value}.
34 */
35 private static final long serialVersionUID = 1L;
36
37 // --- members --------------------------------------------------------------
38
39 /**
40 * The current value of the property that has not been changed.
41 *
42 * @serial
43 */
44 private final Serializable currentValue;
45
46 /**
47 * The value the property was requested to change to but has been rejected.
48 *
49 * @serial
50 */
51 private final Serializable rejectedValue;
52
53 // ****************************** Initializer *******************************
54
55 // ****************************** Constructors ******************************
56
57 /**
58 * Convenience constructor with no message and no root cause.
59 *
60 * @param propertyDescriptor the descriptor of the property raising the
61 * exception.
62 * @param currentValue the current value of the property that has not been
63 * changed.
64 * @param rejectedValue the value the property was requested to change to but
65 * has been rejected.
66 */
67 public ReadOnlyPropertyException(final PropertyDescriptor propertyDescriptor,
68 final Object currentValue, final Object rejectedValue)
69 {
70 this(null, null, propertyDescriptor, currentValue, rejectedValue);
71 }
72
73 /**
74 * Convenience constructor with no message.
75 *
76 * @param cause the cause (which is saved for later retrieval by the
77 * {@link #getCause()} method). (A <tt>null</tt> value is permitted,
78 * and indicates that the cause is nonexistent or unknown.)
79 * @param propertyDescriptor the descriptor of the property raising the
80 * exception.
81 * @param currentValue the current value of the property that has not been
82 * changed.
83 * @param rejectedValue the value the property was requested to change to but
84 * has been rejected.
85 */
86 public ReadOnlyPropertyException(final Throwable cause,
87 final PropertyDescriptor propertyDescriptor, final Object currentValue,
88 final Object rejectedValue)
89 {
90 this(null, cause, propertyDescriptor, currentValue, rejectedValue);
91 }
92
93 /**
94 * Default constructor.
95 *
96 * @param message the detail message (which is saved for later retrieval by
97 * the {@link #getMessage()} method).
98 * @param cause the cause (which is saved for later retrieval by the
99 * {@link #getCause()} method). (A <tt>null</tt> value is permitted,
100 * and indicates that the cause is nonexistent or unknown.)
101 * @param propertyDescriptor the descriptor of the property raising the
102 * exception.
103 * @param currentValue the current value of the property that has not been
104 * changed.
105 * @param rejectedValue the value the property was requested to change to but
106 * has been rejected.
107 */
108 public ReadOnlyPropertyException(final String message, final Throwable cause,
109 final PropertyDescriptor propertyDescriptor, final Object currentValue,
110 final Object rejectedValue)
111 {
112 super(message == null ? createMessage(propertyDescriptor, currentValue,
113 rejectedValue) : message, cause, propertyDescriptor);
114
115 this.currentValue = makeSerializable(currentValue);
116 this.rejectedValue = makeSerializable(rejectedValue);
117 }
118
119 // ****************************** Inner Classes *****************************
120
121 // ********************************* Methods ********************************
122
123 // --- init -----------------------------------------------------------------
124
125 private static String createMessage(
126 final PropertyDescriptor propertyDescriptor, final Object currentValue,
127 final Object rejectedValue)
128 {
129 return String.format(
130 "Value '%s' of read-only property '%s' cannot be changed to '%s'.",
131 currentValue, propertyDescriptor.getKey(), rejectedValue);
132 }
133
134 private static Serializable makeSerializable(final Object value)
135 {
136 return value instanceof Serializable ? (Serializable) value : String
137 .valueOf(value);
138 }
139
140 // --- get&set --------------------------------------------------------------
141
142 /**
143 * Returns the current value of the property that has not been changed.
144 *
145 * @return the current value of the property that has not been changed.
146 */
147 public final Serializable getCurrentValue()
148 {
149 return currentValue;
150 }
151
152 /**
153 * Returns the value the property was requested to change to but has been
154 * rejected.
155 *
156 * @return the value the property was requested to change to but has been
157 * rejected.
158 */
159 public final Serializable getRejectedValue()
160 {
161 return rejectedValue;
162 }
163
164 // --- business -------------------------------------------------------------
165
166 // --- object basics --------------------------------------------------------
167
168 }