1 | |
package org.apache.commons.contract.constraints; |
2 | |
|
3 | |
import java.util.Iterator; |
4 | |
import java.util.List; |
5 | |
import java.util.Locale; |
6 | |
|
7 | |
import org.apache.commons.contract.Context; |
8 | |
import org.apache.commons.i18n.bundles.ErrorBundle; |
9 | |
import org.apache.commons.i18n.bundles.TextBundle; |
10 | |
|
11 | |
public class ArrayConstraints implements Constraints { |
12 | 0 | public static final ArrayConstraints UNCONSTRAINED = new ArrayConstraints(); |
13 | |
|
14 | |
protected Constraints entryValueDescriptor; |
15 | |
|
16 | 0 | public ArrayConstraints() { |
17 | 0 | entryValueDescriptor = Unconstrained.UNCONSTRAINED; |
18 | 0 | } |
19 | |
|
20 | 0 | public ArrayConstraints(Constraints entryValueDescriptor) { |
21 | 0 | this.entryValueDescriptor = entryValueDescriptor; |
22 | 0 | } |
23 | |
|
24 | |
public void setEntryValueDescriptor(Constraints entryValueDescriptor) { |
25 | 0 | this.entryValueDescriptor = entryValueDescriptor; |
26 | 0 | } |
27 | |
|
28 | |
public Constraints getEntryValueDescriptor() { |
29 | 0 | return entryValueDescriptor; |
30 | |
} |
31 | |
|
32 | |
public Object cast(Object value, Context context) throws CastException { |
33 | 0 | Object[] array = null; |
34 | 0 | if ( value instanceof String[] ) { |
35 | 0 | array = new Object[((String[])value).length]; |
36 | 0 | for ( int i = 0; i < array.length; i++ ) { |
37 | 0 | array[i] = entryValueDescriptor.cast(((String [])value)[i], context); |
38 | |
} |
39 | 0 | } else if ( value instanceof List ) { |
40 | 0 | array = new Object[((List)value).size()]; |
41 | 0 | int counter = 0; |
42 | 0 | for ( Iterator i = ((List)value).iterator(); i.hasNext(); ) { |
43 | 0 | Object entry = i.next(); |
44 | 0 | if ( entry instanceof Evaluatable ) { |
45 | |
try { |
46 | 0 | entry = ((Evaluatable)entry).evaluate(context); |
47 | 0 | } catch (Exception e) { |
48 | 0 | throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e); |
49 | 0 | } |
50 | |
} |
51 | 0 | array[counter] = entryValueDescriptor.cast(entry, context); |
52 | 0 | counter++; |
53 | 0 | } |
54 | 0 | } else if ( value instanceof Object[] ) { |
55 | 0 | array = new Object[((Object[])value).length]; |
56 | 0 | for ( int i = 0; i < array.length; i++ ) { |
57 | 0 | Object entry = ((Object[])value)[i]; |
58 | 0 | if ( entry instanceof Evaluatable ) { |
59 | |
try { |
60 | 0 | entry = ((Evaluatable)entry).evaluate(context); |
61 | 0 | } catch (Exception e) { |
62 | 0 | throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e); |
63 | 0 | } |
64 | |
} |
65 | 0 | array[i] = entryValueDescriptor.cast(entry, context); |
66 | |
} |
67 | |
} else { |
68 | 0 | throw new CastException(new ErrorBundle("uncastableArray", new Object[] { value })); |
69 | |
} |
70 | 0 | return array; |
71 | |
} |
72 | |
|
73 | |
public void validate(Object value, Context context) throws ValidationException { |
74 | |
try { |
75 | 0 | Object[] array = (Object [])value; |
76 | 0 | for ( int i = 0; i < array.length; i++ ) { |
77 | 0 | if ( array[i] != null ) { |
78 | 0 | entryValueDescriptor.validate(array[i], context); |
79 | |
} |
80 | |
} |
81 | 0 | } catch ( ValidationException exception ) { |
82 | 0 | throw new ValidationException(new ErrorBundle("invalidArrayEntry", new Object[] { value }), exception); |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | |
public TextBundle verboseConstraints() { |
87 | 0 | if ( entryValueDescriptor == Unconstrained.UNCONSTRAINED ) { |
88 | 0 | return new TextBundle("unconstrainedArray"); |
89 | |
} else { |
90 | 0 | TextBundle verbosedEntryContrstaints = entryValueDescriptor.verboseConstraints(); |
91 | 0 | return new TextBundle("constrainedArray", new String[] { verbosedEntryContrstaints.getText(Locale.getDefault()) }); |
92 | |
} |
93 | |
} |
94 | |
} |