/*
 * DomUI Java User Interface library
 * Copyright (c) 2010 by Frits Jalvingh, Itris B.V.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * See the "sponsors" file for a list of supporters.
 *
 * The latest version of DomUI and related code, support and documentation
 * can be found at http://www.domui.org/
 * The contact for the project is Frits Jalvingh <jal@etc.to>.
 */
package to.etc.domui.converter;

import javax.annotation.*;

import to.etc.domui.dom.errors.*;
import to.etc.domui.trouble.*;
import to.etc.domui.util.*;

public class MaxMinValidator implements IValueValidator<Number> {
	private Number m_max, m_min;

	@Nullable
	private final UIMessage m_msg;

	/**
	 * Create a validator comparing to these INCLUSIVE bounds.
	 * @param max
	 * @param min
	 * @param msg If specified this error message will be shown, otherwise default error message is shown.
	 */
	public MaxMinValidator(Number min, Number max, @Nullable UIMessage msg) {
		m_max = max;
		m_min = min;
		m_msg = msg;
	}

	/**
	 * Create a validator comparing to these INCLUSIVE bounds.
	 * @param max
	 * @param min
	 */
	public MaxMinValidator(Number min, Number max) {
		this(min, max, null);
	}

	/**
	 * Sigh. Of course Number does not implement Comparable, because that would
	 * be useful.
	 * @see to.etc.domui.converter.IValueValidator#validate(java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void validate(Number input) throws Exception {
		Class< ? > ac = input.getClass();
		if(m_max.getClass() == ac && m_min.getClass() == ac && input instanceof Comparable< ? >) {
			int r = ((Comparable<Number>) input).compareTo(m_min);
			if(r < 0) {
				throwError(Msgs.V_TOOSMALL, m_min);
			}
			r = ((Comparable<Number>) input).compareTo(m_max);
			if(r > 0) {
				throwError(Msgs.V_TOOLARGE, m_max);
			}
		} else {
			if(input.doubleValue() > m_max.doubleValue())
				throwError(Msgs.V_TOOLARGE, m_max);
			if(input.doubleValue() < m_min.doubleValue())
				throwError(Msgs.V_TOOSMALL, m_min);
		}
	}

	private void throwError(String vToosmall, Number val) {
		if(m_msg != null) {
			throw new ValidationException(m_msg.getBundle(), m_msg.getCode(), m_msg.getParameters());
		} else {
			throw new ValidationException(vToosmall, val.toString());
		}
	}
}

@@ -1292,16 +1292,26 @@
 mber%3E %7B%0A
+%09@Nonnull%0A
 %09private
@@ -1597,32 +1597,41 @@
 MaxMinValidator(
+@Nonnull 
 Number min, Numb
@@ -1617,32 +1617,41 @@
 null Number min,
+ @Nonnull
  Number max, @Nu
@@ -1847,16 +1847,25 @@
 lidator(
+@Nonnull 
 Number m
@@ -1867,16 +1867,25 @@
 ber min,
+ @Nonnull
  Number 
@@ -2781,25 +2781,38 @@
 ror(
-String vToosma
+@Nonnull String code, @Nonnu
 ll
-,
  Num
@@ -2985,17 +2985,12 @@
 ion(
-vToosmall
+code
 , va

Fix review comments