001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.validation.validator;
018
019import java.io.Serializable;
020
021import org.apache.wicket.validation.IValidatable;
022
023/**
024 * Validator for checking if a given value falls within [min,max] range.
025 * 
026 * If either min or max are {@code null} they are not checked.
027 * 
028 * <p>
029 * Resource keys:
030 * <ul>
031 * <li>{@code RangeValidator.exact} if min==max</li>
032 * <li>{@code RangeValidator.range} if both min and max are not {@code null}</li>
033 * <li>{@code RangeValidator.minimum} if max is {@code null}</li>
034 * <li>{@code RangeValidator.maximum} if min is {@code null}</li>
035 * </ul>
036 * </p>
037 * 
038 * <p>
039 * Error Message Variables:
040 * <ul>
041 * <li>{@code name}: the id of {@code Component} that failed</li>
042 * <li>{@code label}: the label of the {@code Component} (either comes from
043 * {@code FormComponent.labelModel} or resource key {@code <form-id>.<form-component-id>}</li>
044 * <li>{@code input}: the input value</li>
045 * <li>{@code minimum}: the minimum allowed value</li>
046 * <li>{@code maximum}: the maximum allowed value</li>
047 * </ul>
048 * </p>
049 * 
050 * @param <Z>
051 *            type of validatable
052 * 
053 * @author igor
054 */
055public class RangeValidator<Z extends Comparable<? super Z> & Serializable> extends
056        AbstractRangeValidator<Z, Z>
057{
058        private static final long serialVersionUID = 1L;
059
060        /**
061         * @param minimum
062         * @param maximum
063         * @return a {@link RangeValidator} that validates if a value falls within a range
064         */
065        public static <T extends Comparable<? super T> & Serializable> RangeValidator<T> range(T minimum,
066                T maximum)
067        {
068                return new RangeValidator<>(minimum, maximum);
069        }
070
071        /**
072         * @param minimum
073         * @return a {@link RangeValidator} that validates if a value is a least {@code minimum}
074         */
075        public static <T extends Comparable<? super T> & Serializable> RangeValidator<T> minimum(T minimum)
076        {
077                return new RangeValidator<>(minimum, null);
078        }
079
080        /**
081         * @param maximum
082         * @return a {@link RangeValidator} that validates if a value is a most {@code maximum}
083         */
084        public static <T extends Comparable<? super T> & Serializable> RangeValidator<T> maximum(T maximum)
085        {
086                return new RangeValidator<>(null, maximum);
087        }
088
089        /**
090         * Constructor that sets the minimum and maximum values.
091         * 
092         * @param minimum
093         *            the minimum value
094         * @param maximum
095         *            the maximum value
096         */
097        public RangeValidator(Z minimum, Z maximum)
098        {
099                setRange(minimum, maximum);
100        }
101
102        /**
103         * Constructor used for subclasses who want to set the range using
104         * {@link #setRange(Comparable, Comparable)}
105         */
106        protected RangeValidator()
107        {
108        }
109
110        @Override
111        protected Z getValue(IValidatable<Z> validatable)
112        {
113                return validatable.getValue();
114        }
115}