001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.constraint;
013:
014: import java.io.Serializable;
015: import java.lang.reflect.Constructor;
016: import java.util.Map;
017:
018: import net.sf.oval.Validator;
019: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
020: import net.sf.oval.context.OValContext;
021: import net.sf.oval.exception.ReflectionException;
022: import net.sf.oval.internal.CollectionFactoryHolder;
023:
024: /**
025: * @author Sebastian Thomschke
026: */
027: public class CheckWithCheck extends AbstractAnnotationCheck<CheckWith> {
028: public interface SimpleCheck extends Serializable {
029: boolean isSatisfied(Object validatedObject, Object value);
030: }
031:
032: private static final long serialVersionUID = 1L;
033:
034: private boolean ignoreIfNull;
035: private SimpleCheck simpleCheck;
036:
037: @Override
038: public void configure(final CheckWith constraintAnnotation) {
039: super .configure(constraintAnnotation);
040: setSimpleCheck(constraintAnnotation.value());
041: setIgnoreIfNull(constraintAnnotation.ignoreIfNull());
042: }
043:
044: @Override
045: public Map<String, String> getMessageVariables() {
046: final Map<String, String> messageVariables = CollectionFactoryHolder
047: .getFactory().createMap(4);
048: messageVariables.put("ignoreIfNull", Boolean
049: .toString(ignoreIfNull));
050: messageVariables.put("simpleCheck", simpleCheck.getClass()
051: .getName());
052: return messageVariables;
053: }
054:
055: /**
056: * @return the simpleCheck
057: */
058: public SimpleCheck getSimpleCheck() {
059: return simpleCheck;
060: }
061:
062: /**
063: * @return the ignoreIfNull
064: */
065: public boolean isIgnoreIfNull() {
066: return ignoreIfNull;
067: }
068:
069: public boolean isSatisfied(final Object validatedObject,
070: final Object value, final OValContext context,
071: final Validator validator) throws ReflectionException {
072: if (value == null && ignoreIfNull)
073: return true;
074:
075: return simpleCheck.isSatisfied(validatedObject, value);
076: }
077:
078: /**
079: * @param ignoreIfNull the ignoreIfNull to set
080: */
081: public void setIgnoreIfNull(final boolean ignoreIfNull) {
082: this .ignoreIfNull = ignoreIfNull;
083: }
084:
085: /**
086: * @param simpleCheckType the simpleCheckType to set
087: */
088: public void setSimpleCheck(
089: final Class<? extends SimpleCheck> simpleCheckType)
090: throws ReflectionException, IllegalArgumentException {
091: if (simpleCheckType == null)
092: throw new IllegalArgumentException(
093: "simpleCheckType cannot be null");
094:
095: try {
096: final Constructor<? extends SimpleCheck> ctor = simpleCheckType
097: .getDeclaredConstructor((Class<?>[]) null);
098: ctor.setAccessible(true);
099: simpleCheck = ctor.newInstance();
100: } catch (final Exception ex) {
101: throw new ReflectionException(
102: "Cannot instantiate an object of type "
103: + simpleCheckType.getName(), ex);
104: }
105: }
106:
107: /**
108: * @param simpleCheck the simpleCheck to set
109: */
110: public void setSimpleCheck(final SimpleCheck simpleCheck)
111: throws IllegalArgumentException {
112: if (simpleCheck == null)
113: throw new IllegalArgumentException(
114: "simpleCheck cannot be null");
115:
116: this.simpleCheck = simpleCheck;
117: }
118: }
|