View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import static net.sf.oval.Validator.getCollectionFactory;
16  
17  import java.io.Serializable;
18  import java.lang.reflect.Constructor;
19  import java.util.Map;
20  
21  import net.sf.oval.Validator;
22  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
23  import net.sf.oval.context.OValContext;
24  import net.sf.oval.exception.ReflectionException;
25  import net.sf.oval.internal.util.Assert;
26  
27  /**
28   * @author Sebastian Thomschke
29   */
30  public class CheckWithCheck extends AbstractAnnotationCheck<CheckWith>
31  {
32  	public interface SimpleCheck extends Serializable
33  	{
34  		boolean isSatisfied(Object validatedObject, Object value);
35  	}
36  
37  	public interface SimpleCheckWithMessageVariables extends SimpleCheck
38  	{
39  		Map<String, ? extends Serializable> createMessageVariables();
40  	}
41  
42  	private static final long serialVersionUID = 1L;
43  
44  	private boolean ignoreIfNull;
45  	private SimpleCheck simpleCheck;
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	@Override
51  	public void configure(final CheckWith constraintAnnotation)
52  	{
53  		super.configure(constraintAnnotation);
54  		setSimpleCheck(constraintAnnotation.value());
55  		setIgnoreIfNull(constraintAnnotation.ignoreIfNull());
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	@Override
62  	public Map<String, ? extends Serializable> createMessageVariables()
63  	{
64  		final Map<String, Serializable> messageVariables = getCollectionFactory().createMap(4);
65  
66  		if (simpleCheck instanceof SimpleCheckWithMessageVariables)
67  		{
68  			final Map<String, ? extends Serializable> simpleCheckMessageVariables = ((SimpleCheckWithMessageVariables) simpleCheck)
69  					.createMessageVariables();
70  			if (simpleCheckMessageVariables != null) messageVariables.putAll(simpleCheckMessageVariables);
71  		}
72  		messageVariables.put("ignoreIfNull", Boolean.toString(ignoreIfNull));
73  		messageVariables.put("simpleCheck", simpleCheck.getClass().getName());
74  		return messageVariables;
75  	}
76  
77  	/**
78  	 * @return the simpleCheck
79  	 */
80  	public SimpleCheck getSimpleCheck()
81  	{
82  		return simpleCheck;
83  	}
84  
85  	/**
86  	 * @return the ignoreIfNull
87  	 */
88  	public boolean isIgnoreIfNull()
89  	{
90  		return ignoreIfNull;
91  	}
92  
93  	/**
94  	 * {@inheritDoc}
95  	 */
96  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
97  			final Validator validator) throws ReflectionException
98  	{
99  		if (valueToValidate == null && ignoreIfNull) return true;
100 
101 		return simpleCheck.isSatisfied(validatedObject, valueToValidate);
102 	}
103 
104 	/**
105 	 * @param ignoreIfNull the ignoreIfNull to set
106 	 */
107 	public void setIgnoreIfNull(final boolean ignoreIfNull)
108 	{
109 		this.ignoreIfNull = ignoreIfNull;
110 		requireMessageVariablesRecreation();
111 	}
112 
113 	/**
114 	 * @param simpleCheckType the simpleCheckType to set
115 	 * @throws IllegalArgumentException if <code>simpleCheckType == null</code>
116 	 */
117 	public void setSimpleCheck(final Class< ? extends SimpleCheck> simpleCheckType) throws ReflectionException,
118 			IllegalArgumentException
119 	{
120 		Assert.argumentNotNull("simpleCheckType", simpleCheckType);
121 
122 		try
123 		{
124 			final Constructor< ? extends SimpleCheck> ctor = simpleCheckType
125 					.getDeclaredConstructor((Class< ? >[]) null);
126 			ctor.setAccessible(true);
127 			simpleCheck = ctor.newInstance();
128 		}
129 		catch (final Exception ex)
130 		{
131 			throw new ReflectionException("Cannot instantiate an object of type  " + simpleCheckType.getName(), ex);
132 		}
133 		requireMessageVariablesRecreation();
134 	}
135 
136 	/**
137 	 * @param simpleCheck the simpleCheck to set
138 	 * @throws IllegalArgumentException if <code>simpleCheck == null</code>
139 	 */
140 	public void setSimpleCheck(final SimpleCheck simpleCheck) throws IllegalArgumentException
141 	{
142 		Assert.argumentNotNull("simpleCheck", simpleCheck);
143 
144 		this.simpleCheck = simpleCheck;
145 		requireMessageVariablesRecreation();
146 	}
147 }