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.ConstraintTarget;
020    import net.sf.oval.Validator;
021    import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
022    import net.sf.oval.context.OValContext;
023    
024    /**
025     * @author Sebastian Thomschke
026     */
027    public class MaxCheck extends AbstractAnnotationCheck<Max>
028    {
029            private static final long serialVersionUID = 1L;
030    
031            private double max;
032    
033            /**
034             * {@inheritDoc}
035             */
036            @Override
037            public void configure(final Max constraintAnnotation)
038            {
039                    super.configure(constraintAnnotation);
040                    setMax(constraintAnnotation.value());
041            }
042    
043            /**
044             * {@inheritDoc}
045             */
046    
047            @Override
048            protected Map<String, String> createMessageVariables()
049            {
050                    final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
051                    messageVariables.put("max", Double.toString(max));
052                    return messageVariables;
053            }
054    
055            /**
056             * {@inheritDoc}
057             */
058            @Override
059            protected ConstraintTarget[] getAppliesToDefault()
060            {
061                    return new ConstraintTarget[]{ConstraintTarget.VALUES};
062            }
063    
064            /**
065             * @return the max
066             */
067            public double getMax()
068            {
069                    return max;
070            }
071    
072            /**
073             * {@inheritDoc}
074             */
075            public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
076                            final Validator validator)
077            {
078                    if (valueToValidate == null) return true;
079    
080                    if (valueToValidate instanceof Number)
081                    {
082                            final double doubleValue = ((Number) valueToValidate).doubleValue();
083                            return doubleValue <= max;
084                    }
085    
086                    final String stringValue = valueToValidate.toString();
087                    try
088                    {
089                            final double doubleValue = Double.parseDouble(stringValue);
090                            return doubleValue <= max;
091                    }
092                    catch (final NumberFormatException e)
093                    {
094                            return false;
095                    }
096            }
097    
098            /**
099             * @param max the max to set
100             */
101            public void setMax(final double max)
102            {
103                    this.max = max;
104                    requireMessageVariablesRecreation();
105            }
106    }