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.spi.core.constraint; 17 18 import java.util.Collections; 19 import java.util.Locale; 20 21 import de.smartics.properties.api.core.domain.PropertyConstraint; 22 import de.smartics.properties.api.core.domain.PropertyDescriptor; 23 import de.smartics.properties.api.core.domain.PropertyValidationException; 24 25 /** 26 * Base implementation of the {@link PropertyConstraint} interface. 27 * 28 * @param <T> the type of the property value. 29 */ 30 public abstract class AbstractPropertyConstraint<T> implements 31 PropertyConstraint<T> 32 { 33 // ********************************* Fields ********************************* 34 35 // --- constants ------------------------------------------------------------ 36 37 /** 38 * The class version identifier. 39 */ 40 private static final long serialVersionUID = 1L; 41 42 // --- members -------------------------------------------------------------- 43 44 // ****************************** Initializer ******************************* 45 46 // ****************************** Constructors ****************************** 47 48 /** 49 * Default constructor. 50 */ 51 protected AbstractPropertyConstraint() 52 { 53 } 54 55 // ****************************** Inner Classes ***************************** 56 57 // ********************************* Methods ******************************** 58 59 // --- init ----------------------------------------------------------------- 60 61 // --- get&set -------------------------------------------------------------- 62 63 // --- business ------------------------------------------------------------- 64 65 // CHECKSTYLE:OFF 66 @Override 67 public boolean isMandatoryConstraint() // CHECKSTYLE:ON 68 { 69 return false; 70 } 71 72 @Override 73 public final String getDescription() 74 { 75 return getDescription(Locale.getDefault()); 76 } 77 78 @Override 79 public final void validate(final PropertyDescriptor propertyDescriptor, 80 final T value) throws PropertyValidationException 81 { 82 final boolean valid = isValid(value); 83 84 if (!valid) 85 { 86 throw new PropertyValidationException(propertyDescriptor, 87 Collections.singletonList(this), value); 88 } 89 } 90 91 // --- object basics -------------------------------------------------------- 92 93 /** 94 * Returns the string representation of the object. 95 * 96 * @return the string representation of the object. 97 */ 98 // CHECKSTYLE:OFF 99 @Override 100 public String toString() // CHECKSTYLE:ON 101 { 102 return getDescription(); 103 } 104 }