1
2
3
4
5
6
7 package org.weda.property.impl;
8
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.ListIterator;
15 import java.util.Set;
16 import org.apache.hivemind.util.Defense;
17 import org.weda.converter.ValueTypeConverter;
18 import org.weda.converter.ValueTypeConverterException;
19 import org.weda.enhance.InjectHivemindObject;
20 import org.weda.enhance.InjectMessages;
21 import org.weda.message.Messages;
22 import org.weda.property.ConstraintException;
23 import org.weda.property.SetConstraint;
24 import org.weda.property.SetValue;
25
26 /**
27 *
28 * @author Mikhail Titov
29 */
30 public class StaticSetConstraint
31 extends AbstractConstraint implements SetConstraint
32 {
33 private boolean valuesAliased;
34 private List<SetValue> values = new ArrayList<SetValue>();
35 private Set valuesSet;
36 private Class valueType;
37 private String conversionPattern;
38
39 @InjectMessages private Messages messages;
40 @InjectHivemindObject private ValueTypeConverter converter;
41
42 public void init() throws ConstraintException {
43 Set uniqValues = new HashSet(values.size());
44 for (SetValue setValue: values)
45 if (uniqValues.contains(setValue.getValue()))
46 throw new ConstraintException(
47 String.format(
48 "Value duplicate (%s) in the set constraint (%s)"
49 , setValue.getValue(), getId()));
50 else
51 uniqValues.add(setValue.getValue());
52
53 if (isValuesAliased()){
54 Set uniqAliases = new HashSet(values.size());
55 for (SetValue setValue: values)
56 if (uniqAliases.contains(setValue.getAlias()))
57 throw new ConstraintException(
58 String.format(
59 "Alias duplicate (%s) in the set " +
60 "constraint (%s)"
61 , setValue.getAlias(), getId()));
62 else
63 uniqAliases.add(setValue.getAlias());
64 }
65
66 for (SetValue setValue: values){
67 Object value = setValue.getValue();
68 if (value instanceof String){
69 try{
70 value = converter.convert(
71 valueType, value, conversionPattern);
72 ((SetValueImpl)setValue).setValue(value);
73 }catch (ValueTypeConverterException ex) {
74 throw new ConstraintException(
75 String.format(
76 "Can't convert set constraint value (%s) to type (%s)"
77 , value, valueType.getName())
78 , ex);
79 }
80 }
81 }
82
83 values = Collections.unmodifiableList(values);
84 valuesSet = new HashSet();
85 for (SetValue value: values)
86 valuesSet.add(value.getValue());
87 }
88
89 public void addSetValue(SetValueImpl setValue){
90 setValue.setConstraint(this);
91 values.add(setValue);
92 }
93
94 public void check(Object value, Object[] parameters) throws ConstraintException {
95 if (value!=null && !valuesSet.contains(value))
96 throw new ConstraintException(
97 messages.format("errorMessage", value));
98 }
99
100 public Iterator<SetValue> iterator() {
101 return values.iterator();
102 }
103
104 public boolean isValuesAliased() {
105 return valuesAliased;
106 }
107
108 public Class getValueType() {
109 return valueType;
110 }
111
112 public void setValueType(Class valueType) {
113 this.valueType = valueType;
114 }
115
116 public String getConversionPattern() {
117 return conversionPattern;
118 }
119
120 public void setConversionPattern(String conversionPattern) {
121 this.conversionPattern = conversionPattern;
122 }
123
124 public void setValuesAliased(boolean valuesAliased) {
125 this.valuesAliased = valuesAliased;
126 }
127
128 public Class[] getParameterTypes(Class valueType)
129 throws ConstraintException
130 {
131 return null;
132 }
133
134 }