Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: LongRangeValidator.java,v 1.46.4.2 2008/12/08 18:07:13 rlubke Exp $
   */
  
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   * 
   * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   * 
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
  * and Distribution License("CDDL") (collectively, the "License").  You
  * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * 
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  * 
  * Contributor(s):
  * 
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package javax.faces.validator;
 
 

LongRangeValidator is a Validator that checks the value of the corresponding component against specified minimum and maximum values. The following algorithm is implemented:

  • If the passed value is null, exit immediately.
  • If the current component value is not a floating point type, or a String that is convertible to long, throw a ValidatorException containing a TYPE_MESSAGE_ID message.
  • If both a maximum and minimum property has been configured on this Validator, check the component value against both limits. If the component value is not within this specified range, throw a ValidatorException containing a NOT_IN_RANGE_MESSAGE_ID message.
  • If a maximum property has been configured on this Validator, check the component value against this limit. If the component value is greater than the specified maximum, throw a ValidatorException containing a MAXIMUM_MESSAGE_ID message.
  • If a minimum property has been configured on this Validator, check the component value against this limit. If the component value is less than the specified minimum, throw a ValidatorException containing a MINIMUM_MESSAGE_ID message.

For all of the above cases that cause a ValidatorException to be thrown, if there are parameters to the message that match up with validator parameters, the values of these parameters must be converted using the javax.faces.convert.Converter registered in the application under the converter id javax.faces.Number. This allows the values to be localized according to the current Locale.

 
 
 public class LongRangeValidator implements ValidatorStateHolder {
 
     // ------------------------------------------------------ Manifest Constants
 

    

The standard converter id for this converter.

 
     public static final String VALIDATOR_ID = "javax.faces.LongRange";


    

The message identifier of the javax.faces.application.FacesMessage to be created if the maximum value check fails. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the configured maximum value.
  • {1} replaced by a String whose value is the label of the input component that produced this message.
    public static final String MAXIMUM_MESSAGE_ID =
         "javax.faces.validator.LongRangeValidator.MAXIMUM";


    

The message identifier of the javax.faces.application.FacesMessage to be created if the minimum value check fails. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the configured minimum value.
  • {1} replaced by a String whose value is the label of the input component that produced this message.
    public static final String MINIMUM_MESSAGE_ID =
         "javax.faces.validator.LongRangeValidator.MINIMUM";

    

The message identifier of the javax.faces.application.FacesMessage to be created if the maximum or minimum value check fails, and both the maximum and minimum values for this validator have been set. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the configured minimum value.
  • {1} replaced by the configured maximum value.
  • {2} replaced by a String whose value is the label of the input component that produced this message.
    public static final String NOT_IN_RANGE_MESSAGE_ID =
         "javax.faces.validator.LongRangeValidator.NOT_IN_RANGE";

    

The message identifier of the javax.faces.application.FacesMessage to be created if the current value of this component is not of the correct type. The message format string for this message may optionally include a {0} placeholder that will be replaced by a String whose value is the label of the input component that produced this message.

    public static final String TYPE_MESSAGE_ID =
         "javax.faces.validator.LongRangeValidator.TYPE";
    // ------------------------------------------------------------ Constructors


    

Construct a Validator with no preconfigured limits.

    public LongRangeValidator() {
        super();
    }


    

Construct a Validator with the specified preconfigured limit.

Parameters:
maximum Maximum value to allow
    public LongRangeValidator(long maximum) {
        super();
        setMaximum(maximum);
    }


    

Construct a Validator with the specified preconfigured limits.

Parameters:
maximum Maximum value to allow
minimum Minimum value to allow
    public LongRangeValidator(long maximumlong minimum) {
        super();
        setMaximum(maximum);
        setMinimum(minimum);
    }
    // -------------------------------------------------------------- Properties
    private Long maximum;


    

Return the maximum value to be enforced by this Validator.

    public long getMaximum() {
        return (this. != null ? this. : 0);
    }


    

Set the maximum value to be enforced by this Validator.

Parameters:
maximum The new maximum value
    public void setMaximum(long maximum) {
        this. = maximum;
    }
    private Long minimum;


    

Return the minimum value to be enforced by this Validator.

    public long getMinimum() {
        return (this. != null ? this. : 0);
    }


    

Set the minimum value to be enforced by this Validator.

Parameters:
minimum The new minimum value
    public void setMinimum(long minimum) {
        this. = minimum;
    }
    // ------------------------------------------------------- Validator Methods

    
    public void validate(FacesContext context,
                         UIComponent component,
                         Object valuethrows ValidatorException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
        if (value != null) {
            try {
                long converted = longValue(value);
                if (isMaximumSet() &&
                     (converted > )) {
                    if (isMinimumSet()) {
                        throw new ValidatorException(MessageFactory.getMessage
                             (context,
                                  ,
                                  stringValue(componentcontext),
                                  stringValue(componentcontext),
                                  MessageFactory.getLabel(contextcomponent)));
                    } else {
                        throw new ValidatorException(MessageFactory.getMessage
                             (context,
                                  ,
                                  stringValue(componentcontext),
                                  MessageFactory.getLabel(contextcomponent)));
                    }
                }
                if (isMinimumSet() &&
                     (converted < )) {
                    if (isMaximumSet()) {
                        throw new ValidatorException(MessageFactory.getMessage
                             (context,
                                  ,
                                  stringValue(componentcontext),
                                  stringValue(componentcontext),
                                  MessageFactory.getLabel(contextcomponent)));
                    } else {
                        throw new ValidatorException(MessageFactory.getMessage
                             (context,
                                  ,
                                  stringValue(componentcontext),
                                  MessageFactory.getLabel(contextcomponent)));
                    }
                }
            } catch (NumberFormatException e) {
                throw new ValidatorException(MessageFactory.getMessage
                     (context,
                          MessageFactory.getLabel(contextcomponent)), e);
            }
        }
    }
    public boolean equals(Object otherObj) {
        if (!(otherObj instanceof LongRangeValidator)) {
            return false;
        }
        LongRangeValidator other = (LongRangeValidatorotherObj;
        return ((this.getMaximum() == other.getMaximum())
                && (this.getMinimum() == other.getMinimum())
                && (this.isMaximumSet() == other.isMaximumSet())
                && (this.isMinimumSet() == other.isMinimumSet()));
    }
    public int hashCode() {
        int hashCode = Long.valueOf(getMinimum()).hashCode()
             + Long.valueOf(getMaximum()).hashCode()
             + Boolean.valueOf(isMinimumSet()).hashCode()
             + Boolean.valueOf(isMaximumSet()).hashCode();
        return (hashCode);
    }
    // --------------------------------------------------------- Private Methods


    

Return the specified attribute value, converted to a long.

Parameters:
attributeValue The attribute value to be converted
Throws:
java.lang.NumberFormatException if conversion is not possible
    private static long longValue(Object attributeValue)
         throws NumberFormatException {
        if (attributeValue instanceof Number) {
            return (((NumberattributeValue).longValue());
        } else {
            return (Long.parseLong(attributeValue.toString()));
        }
    }
    private static String stringValue(UIComponent component,
                                      Long toConvert,
                                      FacesContext context) {
        Converter converter =
             context.getApplication().createConverter("javax.faces.Number");
        return converter.getAsString(contextcomponenttoConvert);
    }
    private boolean isMinimumSet() {
        return ( != null);
    }
    private boolean isMaximumSet() {
        return ( != null);
    }
    // ----------------------------------------------------- StateHolder Methods
    public Object saveState(FacesContext context) {
        Object values[] = new Object[2];
        values[0] = ;
        values[1] = ;
        return (values);
    }
    public void restoreState(FacesContext contextObject state) {
        Object values[] = (Object[]) state;
         = (Longvalues[0];
         = (Longvalues[1];
    }
    private boolean transientValue = false;
    public boolean isTransient() {
        return (this.);
    }
    public void setTransient(boolean transientValue) {
        this. = transientValue;
    }
New to GrepCode? Check out our FAQ X