001    package org.apache.commons.contract.constraints;
002    
003    import org.apache.commons.contract.Context;
004    import org.apache.commons.i18n.bundles.ErrorBundle;
005    import org.apache.commons.i18n.bundles.TextBundle;
006    
007    public class Unconstrained implements Constraints {
008        public final static Unconstrained UNCONSTRAINED = new Unconstrained();
009    
010        private Class clazz;
011        
012        public Unconstrained() {
013            this.clazz = null;
014        }
015        
016        public Unconstrained(Class clazz) {
017            this.clazz = clazz;
018        }
019        
020        public Object cast(Object value, Context context) throws CastException {
021            return value;
022        }
023        
024        public void validate(Object value, Context context) throws ValidationException {
025            if ( clazz != null && !clazz.isInstance(value) ) {
026                throw new ValidationException(new ErrorBundle("invalidObjectType", new Object[] { clazz, value }));
027            }
028        }
029        
030        public TextBundle verboseConstraints() {
031            if ( clazz == null ) {
032                return new TextBundle("unconstrained");
033            } else {
034                return new TextBundle("constrainedUnconstrained");
035            }
036        }
037    }