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.constraints;
17
18 import java.util.Locale;
19
20 import org.apache.commons.lang.StringUtils;
21
22 import de.smartics.properties.api.core.domain.PropertyValueRange;
23 import de.smartics.properties.spi.core.constraint.AbstractPropertyConstraint;
24
25 /**
26 * Constraint that requires values to be part of an enumeration.
27 *
28 * @param <T> the type of the property value.
29 */
30 public final class PropertyRangeConstraint<T> extends AbstractPropertyConstraint<T>
31 {
32 // ********************************* Fields *********************************
33
34 // --- constants ------------------------------------------------------------
35
36 /**
37 * The class version identifier.
38 */
39 private static final long serialVersionUID = 1L;
40
41 // --- members --------------------------------------------------------------
42
43 /**
44 * The list of valid properties.
45 * <p>
46 * A value of <code>null</code> tells that there is no range defined.
47 * </p>
48 */
49 private final PropertyValueRange<T> valueRange;
50
51 // ****************************** Initializer *******************************
52
53 // ****************************** Constructors ******************************
54
55 /**
56 * Default constructor.
57 *
58 * @param valueRange the list of valid properties.
59 */
60 public PropertyRangeConstraint(final PropertyValueRange<T> valueRange)
61 {
62 this.valueRange = valueRange;
63 }
64
65 // ****************************** Inner Classes *****************************
66
67 // ********************************* Methods ********************************
68
69 // --- init -----------------------------------------------------------------
70
71 // --- get&set --------------------------------------------------------------
72
73 /**
74 * Returns the list of valid properties.
75 * <p>
76 * A value of <code>null</code> tells that there is no range defined.
77 * </p>
78 *
79 * @return the list of valid properties.
80 */
81 public PropertyValueRange<T> getValueRange()
82 {
83 return valueRange;
84 }
85
86 // --- business -------------------------------------------------------------
87
88 @Override
89 public String getDescription(final Locale locale)
90 {
91 if(valueRange == null)
92 {
93 return StringUtils.EMPTY;
94 }
95
96 // TODO Localize!
97 final StringBuilder buffer = new StringBuilder();
98
99 buffer.append("Select one of the following values: ");
100 buffer.append(valueRange.toString(locale));
101
102 return buffer.toString();
103 }
104
105 @Override
106 public boolean isValid(final T value)
107 {
108 return (valueRange != null && isMember(value));
109 }
110
111 private boolean isMember(final T value)
112 {
113 return valueRange.getValues().contains(value);
114 }
115
116 // --- object basics --------------------------------------------------------
117
118 }