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 /** 23 * Signals that the value does not match the given constraints. 24 */ 25 public class PropertyValueConversionException extends 26 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 value of the property that does not meet the constraints. 43 * 44 * @serial 45 */ 46 private final Serializable value; 47 48 // ****************************** Initializer ******************************* 49 50 // ****************************** Constructors ****************************** 51 52 /** 53 * Convenience constructor with no cause. 54 * 55 * @param propertyDescriptor the key descriptor the property raising the 56 * exception. 57 * @param value the value of the property that does not meet the constraints. 58 * If the value is not serializable, its string representation is 59 * stored. 60 */ 61 public PropertyValueConversionException( 62 final PropertyDescriptor propertyDescriptor, final Object value) 63 { 64 this(null, propertyDescriptor, value); 65 } 66 67 /** 68 * Default constructor. 69 * 70 * @param cause the cause (which is saved for later retrieval by the 71 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 72 * and indicates that the cause is nonexistent or unknown.) 73 * @param propertyDescriptor the descriptor of the property raising the 74 * exception. 75 * @param value the value of the property that cannot be converted. If the 76 * value is not serializable, its string representation is stored. 77 */ 78 public PropertyValueConversionException(final Throwable cause, 79 final PropertyDescriptor propertyDescriptor, final Object value) 80 { 81 this(createMessage(cause, propertyDescriptor, value), cause, 82 propertyDescriptor, value); 83 } 84 85 /** 86 * Internal constructor to provide a custom message. 87 * 88 * @param message the detail message (which is saved for later retrieval by 89 * the {@link #getMessage()} method). 90 * @param cause the cause (which is saved for later retrieval by the 91 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 92 * and indicates that the cause is nonexistent or unknown.) 93 * @param propertyDescriptor the descriptor of the property raising the 94 * exception. 95 * @param value the value of the property that cannot be converted. If the 96 * value is not serializable, its string representation is stored. 97 */ 98 protected PropertyValueConversionException(final String message, 99 final Throwable cause, final PropertyDescriptor propertyDescriptor, 100 final Object value) 101 { 102 super(message, cause, propertyDescriptor); 103 104 this.value = 105 value instanceof Serializable ? (Serializable) value : String 106 .valueOf(value); 107 } 108 109 // ****************************** Inner Classes ***************************** 110 111 // ********************************* Methods ******************************** 112 113 // --- init ----------------------------------------------------------------- 114 115 private static String createMessage(final Throwable cause, 116 final PropertyDescriptor propertyDescriptor, final Object value) 117 { 118 final StringBuilder buffer = new StringBuilder(); 119 120 buffer.append("Conversion failed for value '" 121 + value 122 + '\'' 123 + (value != null ? " (" + value.getClass().getSimpleName() 124 + ')' : "") + " for property '" 125 + propertyDescriptor.getKey() + "'"); 126 if (cause != null) 127 { 128 buffer.append(": ").append(cause.getMessage()); 129 } 130 else 131 { 132 buffer.append('.'); 133 } 134 135 return buffer.toString(); 136 } 137 138 // --- get&set -------------------------------------------------------------- 139 140 /** 141 * Returns the value of the property that does not meet the constraints. 142 * 143 * @return the value of the property that does not meet the constraints. 144 */ 145 public final Serializable getValue() 146 { 147 return value; 148 } 149 150 // --- business ------------------------------------------------------------- 151 152 // --- object basics -------------------------------------------------------- 153 154 }