1 | |
package org.apache.commons.contract.constraints; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Arrays; |
5 | |
import java.util.Iterator; |
6 | |
import java.util.List; |
7 | |
|
8 | |
import org.apache.commons.contract.Context; |
9 | |
import org.apache.commons.i18n.bundles.ErrorBundle; |
10 | |
import org.apache.commons.i18n.bundles.TextBundle; |
11 | |
|
12 | |
public class StringConstraints implements Constraints, Castable { |
13 | 0 | public final static StringConstraints UNCONSTRAINED = new StringConstraints(); |
14 | 0 | public final static StringConstraints EMPTY = new StringConstraints(0); |
15 | 0 | public final static StringConstraints NOT_EMPTY = new StringConstraints(1,Integer.MAX_VALUE); |
16 | |
|
17 | |
protected boolean constrained; |
18 | 0 | protected List allowedValues = new ArrayList(); |
19 | 0 | protected int minimumLength = -1, maximumLength =-1; |
20 | |
protected Object defaultValue; |
21 | |
|
22 | 0 | public StringConstraints() { |
23 | 0 | this.constrained = false; |
24 | 0 | } |
25 | |
|
26 | 0 | public StringConstraints(String[] allowedValues) { |
27 | 0 | this.constrained = true; |
28 | 0 | this.allowedValues = new ArrayList(Arrays.asList(allowedValues)); |
29 | 0 | } |
30 | |
|
31 | 0 | public StringConstraints(int maximumLength) { |
32 | 0 | this.maximumLength = maximumLength; |
33 | 0 | this.constrained = true; |
34 | 0 | } |
35 | |
|
36 | 0 | public StringConstraints(int minimumLength, int maximumLength) { |
37 | 0 | this.minimumLength = minimumLength; |
38 | 0 | this.maximumLength = maximumLength; |
39 | 0 | this.constrained = true; |
40 | 0 | } |
41 | |
|
42 | |
public int getMinimumLength() { |
43 | 0 | return minimumLength; |
44 | |
} |
45 | |
|
46 | |
public void setMinimumLength(int minimumLength) { |
47 | 0 | this.minimumLength = minimumLength; |
48 | 0 | constrained = true; |
49 | 0 | } |
50 | |
|
51 | |
public int getMaximumLength() { |
52 | 0 | return maximumLength; |
53 | |
} |
54 | |
|
55 | |
public void setMaximumLength(int maximumLength) { |
56 | 0 | this.maximumLength = maximumLength; |
57 | 0 | constrained = true; |
58 | 0 | } |
59 | |
|
60 | |
public void addAllowedValue(String value) { |
61 | 0 | constrained = true; |
62 | 0 | allowedValues.add(value); |
63 | 0 | } |
64 | |
|
65 | |
public boolean isConstrained() { |
66 | 0 | return constrained; |
67 | |
} |
68 | |
|
69 | |
public boolean isEnumerable() { |
70 | 0 | return ( allowedValues.size() > 0 ); |
71 | |
} |
72 | |
|
73 | |
public String[] getAllowedValues() { |
74 | 0 | return (String [])allowedValues.toArray(new String [0]); |
75 | |
} |
76 | |
|
77 | |
public Object cast(Object value, Context context) throws CastException { |
78 | 0 | if (value instanceof String) { |
79 | 0 | return value; |
80 | 0 | } else if (value instanceof String[]) { |
81 | 0 | return ((String [])value)[0]; |
82 | |
} |
83 | 0 | return value.toString(); |
84 | |
} |
85 | |
|
86 | |
public void validate(Object value, Context context) throws ValidationException { |
87 | 0 | if ( constrained ) { |
88 | 0 | if ( isEnumerable() ) { |
89 | 0 | for ( Iterator i = allowedValues.iterator(); i.hasNext(); ) { |
90 | 0 | if (value.equals((String)i.next())) { |
91 | 0 | return; |
92 | |
} |
93 | |
} |
94 | 0 | throw new ValidationException(new ErrorBundle("invalidString", new String[] { (String)value, getEnumeratedValues() })); |
95 | |
} else { |
96 | 0 | if ( minimumLength != -1 && ((String)value).length() < minimumLength ) throw new ValidationException(new ErrorBundle("stringTooShort", new Object[] { new Integer(minimumLength) })); |
97 | 0 | if ( maximumLength != -1 && ((String)value).length() > maximumLength ) throw new ValidationException(new ErrorBundle("stringTooLong", new Object[] { new Integer(maximumLength) })); |
98 | |
} |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
public TextBundle verboseConstraints() { |
103 | 0 | if ( constrained ) { |
104 | 0 | if ( isEnumerable() ) { |
105 | 0 | return new TextBundle("enumeratedStrings", new Object[] { getEnumeratedValues() }); |
106 | |
} else { |
107 | 0 | if ( minimumLength != -1 && maximumLength == -1 ) { |
108 | 0 | return new TextBundle("stringLengthBetween", new Object[] { new Integer(minimumLength), new Integer(maximumLength) }); |
109 | 0 | } else if ( minimumLength != -1 ) { |
110 | 0 | return new TextBundle("stringLengthGreater", new Object[] { new Integer(minimumLength) }); |
111 | |
} else { |
112 | 0 | return new TextBundle("stringLengthLess", new Object[] { new Integer(maximumLength) }); |
113 | |
} |
114 | |
} |
115 | |
} else { |
116 | 0 | return new TextBundle("strings"); |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
private String getEnumeratedValues() { |
121 | 0 | StringBuffer buffer = new StringBuffer(256); |
122 | 0 | for ( int i = 0; i < getAllowedValues().length; i++ ) { |
123 | 0 | if ( i > 0 && i < getAllowedValues().length ) buffer.append(", "); |
124 | 0 | buffer.append(getAllowedValues()[i]); |
125 | |
} |
126 | 0 | return buffer.toString(); |
127 | |
} |
128 | |
} |