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.lang.reflect.Array;
018    import java.util.Collection;
019    import java.util.Map;
020    
021    import net.sf.oval.Validator;
022    import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
023    import net.sf.oval.context.OValContext;
024    
025    /**
026     * @author Sebastian Thomschke
027     */
028    public class MaxSizeCheck extends AbstractAnnotationCheck<MaxSize>
029    {
030            private static final long serialVersionUID = 1L;
031    
032            private int max;
033    
034            /**
035             * {@inheritDoc}
036             */
037            @Override
038            public void configure(final MaxSize constraintAnnotation)
039            {
040                    super.configure(constraintAnnotation);
041                    setMax(constraintAnnotation.value());
042            }
043    
044            /**
045             * {@inheritDoc}
046             */
047            @Override
048            protected Map<String, String> createMessageVariables()
049            {
050                    final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
051                    messageVariables.put("max", Integer.toString(max));
052                    return messageVariables;
053            }
054    
055            /**
056             * @return the max
057             */
058            public int getMax()
059            {
060                    return max;
061            }
062    
063            /**
064             * {@inheritDoc}
065             */
066            public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
067                            final Validator validator)
068            {
069                    if (valueToValidate == null) return true;
070    
071                    if (valueToValidate instanceof Collection)
072                    {
073                            final int size = ((Collection< ? >) valueToValidate).size();
074                            return size <= max;
075                    }
076                    if (valueToValidate instanceof Map)
077                    {
078                            final int size = ((Map< ? , ? >) valueToValidate).size();
079                            return size <= max;
080                    }
081                    if (valueToValidate.getClass().isArray())
082                    {
083                            final int size = Array.getLength(valueToValidate);
084                            return size <= max;
085                    }
086                    return false;
087            }
088    
089            /**
090             * @param max the max to set
091             */
092            public void setMax(final int max)
093            {
094                    this.max = max;
095                    requireMessageVariablesRecreation();
096            }
097    }