01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.constraint;
13:
14: import java.util.Map;
15:
16: import net.sf.oval.Validator;
17: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
18: import net.sf.oval.context.OValContext;
19: import net.sf.oval.internal.CollectionFactoryHolder;
20: import net.sf.oval.internal.util.StringUtils;
21:
22: /**
23: * @author Sebastian Thomschke
24: */
25: public class InstanceOfAnyCheck extends
26: AbstractAnnotationCheck<InstanceOfAny> {
27: private static final long serialVersionUID = 1L;
28:
29: private Class[] types;
30:
31: @Override
32: public void configure(final InstanceOfAny constraintAnnotation) {
33: super .configure(constraintAnnotation);
34: setTypes(constraintAnnotation.value());
35: }
36:
37: @Override
38: public Map<String, String> getMessageVariables() {
39: final Map<String, String> messageVariables = CollectionFactoryHolder
40: .getFactory().createMap(2);
41: if (types.length == 1) {
42: messageVariables.put("types", types[0].getName());
43: } else {
44: final String[] classNames = new String[types.length];
45: for (int i = 0, l = classNames.length; i < l; i++) {
46: classNames[i] = types[i].getName();
47: }
48: messageVariables.put("types", StringUtils.implode(
49: classNames, ","));
50: }
51: return messageVariables;
52: }
53:
54: /**
55: * @return the type
56: */
57: public Class[] getTypes() {
58: return types;
59: }
60:
61: public boolean isSatisfied(final Object validatedObject,
62: final Object value, final OValContext context,
63: final Validator validator) {
64: if (value == null)
65: return true;
66:
67: for (final Class type : types) {
68: if (type.isInstance(value))
69: return true;
70: }
71: return false;
72: }
73:
74: /**
75: * @param types the types to set
76: */
77: public void setTypes(final Class... types) {
78: this.types = types;
79: }
80: }
|