1 | |
package org.apache.commons.contract.constraints; |
2 | |
|
3 | |
import org.apache.commons.contract.Context; |
4 | |
import org.apache.commons.i18n.bundles.ErrorBundle; |
5 | |
import org.apache.commons.i18n.bundles.TextBundle; |
6 | |
|
7 | |
public class Unconstrained implements Constraints { |
8 | 0 | public final static Unconstrained UNCONSTRAINED = new Unconstrained(); |
9 | |
|
10 | |
private Class clazz; |
11 | |
|
12 | 0 | public Unconstrained() { |
13 | 0 | this.clazz = null; |
14 | 0 | } |
15 | |
|
16 | 0 | public Unconstrained(Class clazz) { |
17 | 0 | this.clazz = clazz; |
18 | 0 | } |
19 | |
|
20 | |
public Object cast(Object value, Context context) throws CastException { |
21 | 0 | return value; |
22 | |
} |
23 | |
|
24 | |
public void validate(Object value, Context context) throws ValidationException { |
25 | 0 | if ( clazz != null && !clazz.isInstance(value) ) { |
26 | 0 | throw new ValidationException(new ErrorBundle("invalidObjectType", new Object[] { clazz, value })); |
27 | |
} |
28 | 0 | } |
29 | |
|
30 | |
public TextBundle verboseConstraints() { |
31 | 0 | if ( clazz == null ) { |
32 | 0 | return new TextBundle("unconstrained"); |
33 | |
} else { |
34 | 0 | return new TextBundle("constrainedUnconstrained"); |
35 | |
} |
36 | |
} |
37 | |
} |