001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 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     *******************************************************************************/
013    package net.sf.oval.constraint;
014    
015    import static net.sf.oval.Validator.getCollectionFactory;
016    
017    import java.util.Map;
018    
019    import net.sf.oval.Validator;
020    import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
021    import net.sf.oval.context.OValContext;
022    import net.sf.oval.internal.util.StringUtils;
023    
024    /**
025     * @author Sebastian Thomschke
026     */
027    public class InstanceOfCheck extends AbstractAnnotationCheck<InstanceOf>
028    {
029            private static final long serialVersionUID = 1L;
030    
031            private Class< ? >[] types;
032    
033            @Override
034            public void configure(final InstanceOf constraintAnnotation)
035            {
036                    super.configure(constraintAnnotation);
037                    setTypes(constraintAnnotation.value());
038            }
039    
040            @Override
041            protected Map<String, String> createMessageVariables()
042            {
043                    final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
044                    if (types.length == 1)
045                            messageVariables.put("types", types[0].getName());
046                    else
047                    {
048                            final String[] classNames = new String[types.length];
049                            for (int i = 0, l = classNames.length; i < l; i++)
050                                    classNames[i] = types[i].getName();
051                            messageVariables.put("types", StringUtils.implode(classNames, ","));
052                    }
053                    return messageVariables;
054            }
055    
056            /**
057             * @return the type
058             */
059            public Class< ? >[] getTypes()
060            {
061                    return types;
062            }
063    
064            public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
065                            final Validator validator)
066            {
067                    if (valueToValidate == null) return true;
068    
069                    for (final Class< ? > type : types)
070                            if (!type.isInstance(valueToValidate)) return false;
071                    return true;
072            }
073    
074            /**
075             * @param types the types to set
076             */
077            public void setTypes(final Class< ? >... types)
078            {
079                    this.types = types;
080                    requireMessageVariablesRecreation();
081            }
082    }