RatingValidator.java

package server.restful.common.validator;

import com.beust.jcommander.IParameterValidator;
import com.beust.jcommander.ParameterException;

public class RatingValidator implements IParameterValidator {

    private static final int MIN_RATING = 0;

    private static final int MAX_RATING = 10;

    @Override
    public void validate(String name, String value) throws ParameterException {
        try {
            int n = Integer.parseInt(value);
            if (n < MIN_RATING || n > MAX_RATING) {
                throw new ParameterException("Invalid " + name + " value, expected " + MIN_RATING + ".." + MAX_RATING
                        + " (found " + value + ")");
            }
        } catch (NumberFormatException e) {
            throw new ParameterException("Parameter " + name + " should be an int (found " + value + ")");
        }
    }

}