View Javadoc

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 String getViolationMessage(final Locale locale,
80        final Object actualValue)
81    {
82      return actualValue + ": " + getDescription(locale);
83    }
84  
85    @Override
86    public boolean isValid(final T value)
87    {
88      return isValid(value, null);
89    }
90  
91    @Override
92    public final void validate(final PropertyDescriptor propertyDescriptor,
93        final T value) throws PropertyValidationException
94    {
95      validate(propertyDescriptor, value, null);
96    }
97  
98    @Override
99    public final void validate(final PropertyDescriptor propertyDescriptor,
100       final T value, final Class<?> group) throws PropertyValidationException
101   {
102     final boolean valid = isValid(value, group);
103 
104     if (!valid)
105     {
106       throw PropertyValidationException.invalid(propertyDescriptor,
107           Collections.singletonList(this), value);
108     }
109   }
110 
111   // --- object basics --------------------------------------------------------
112 
113   /**
114    * Returns the string representation of the object.
115    *
116    * @return the string representation of the object.
117    */
118   // CHECKSTYLE:OFF
119   @Override
120   public String toString() // CHECKSTYLE:ON
121   {
122     return getDescription();
123   }
124 }