2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.validator.validator.IntegerValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
IntegerValidator
100%
27/27
N/A
3.143
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.validator.validator;
 19  
 
 20  
 import javax.faces.application.FacesMessage;
 21  
 import javax.faces.component.StateHolder;
 22  
 import javax.faces.component.UIComponent;
 23  
 import javax.faces.context.FacesContext;
 24  
 import javax.faces.validator.ValidatorException;
 25  
 
 26  
 /**
 27  
  * <p><code>Validator</code> implementation that will perform both format
 28  
  * and (optional) range checks on an integer value.</p>
 29  
  */
 30  8
 public final class IntegerValidator extends AbstractValidator
 31  
   implements StateHolder {
 32  
 
 33  
 
 34  
     // -------------------------------------------------------- Static Variables
 35  
 
 36  
 
 37  
     /**
 38  
      * <p>The Apache Commons Validator instance we will be using.</p>
 39  
      */
 40  
     private static final
 41  1
       org.apache.commons.validator.routines.IntegerValidator INSTANCE =
 42  
       org.apache.commons.validator.routines.IntegerValidator.getInstance();
 43  
 
 44  
 
 45  
     // -------------------------------------------------------------- Properties
 46  
 
 47  
 
 48  8
     private int maximum = Integer.MAX_VALUE;
 49  8
     private boolean maximumSet = false;
 50  
 
 51  
 
 52  
     /**
 53  
      * <p>Return the configured maximum value for this validator.</p>
 54  
      */
 55  
     public int getMaximum() {
 56  
         return this.maximum;
 57  
     }
 58  
     
 59  
 
 60  
     /**
 61  
      * <p>Set the configured maximum value for this validator.</p>
 62  
      *
 63  
      * @param maximum The new maximum value
 64  
      */
 65  
     public void setMaximum(int maximum) {
 66  8
         this.maximum = maximum;
 67  8
         this.maximumSet = true;
 68  8
     }
 69  
 
 70  
 
 71  8
     private int minimum = Integer.MIN_VALUE;
 72  8
     private boolean minimumSet = false;
 73  
 
 74  
 
 75  
     /**
 76  
      * <p>Return the configured minimum value for this validator.</p>
 77  
      */
 78  
     public int getMinimum() {
 79  
         return this.minimum;
 80  
     }
 81  
     
 82  
 
 83  
     /**
 84  
      * <p>Set the configured minimum value for this validator.</p>
 85  
      *
 86  
      * @param minimum The new minimum value
 87  
      */
 88  
     public void setMinimum(int minimum) {
 89  8
         this.minimum = minimum;
 90  8
         this.minimumSet = true;
 91  8
     }
 92  
 
 93  
 
 94  
     // ------------------------------------------------------- Validator Methods
 95  
 
 96  
 
 97  
     /** {@inheritDoc} */
 98  
     public void validate(FacesContext context,
 99  
                          UIComponent  component,
 100  
                          Object       value) throws ValidatorException {
 101  
 
 102  
         // Validate our parameters
 103  15
         if ((context == null) || (component == null)) {
 104  
             throw new NullPointerException(message(context, "common.null"));
 105  
         }
 106  15
         if (value == null) {
 107  
             return;
 108  
         }
 109  
 
 110  
         // Conversion is expected to have occurred already, via an
 111  
         // explicit or implicit Converter having been applied.
 112  
         // Throw a validation error if this is not the case
 113  15
         if (!(value instanceof Integer)) {
 114  1
             throw new ValidatorException
 115  
               (new FacesMessage(FacesMessage.SEVERITY_ERROR,
 116  
                                 message(context, "Integer.unconverted"),
 117  
                                 null));
 118  
         }
 119  
 
 120  
         // Perform the requested range checks (if any)
 121  14
         Integer ivalue = (Integer) value;
 122  14
         if (minimumSet) {
 123  8
             if (maximumSet) {
 124  5
                 if (!INSTANCE.isInRange(ivalue, minimum, maximum)) {
 125  2
                     throw new ValidatorException
 126  
                       (new FacesMessage(FacesMessage.SEVERITY_ERROR,
 127  
                                         message(context, "Common.range",
 128  
                                                 new Object[] { ivalue,
 129  
                                                                new Integer(minimum),
 130  
                                                                new Integer(maximum) }),
 131  
                                         null));
 132  
                 }
 133  
             } else {
 134  3
                 if (!INSTANCE.minValue(ivalue, minimum)) {
 135  1
                     throw new ValidatorException
 136  
                       (new FacesMessage(FacesMessage.SEVERITY_ERROR,
 137  
                                         message(context, "Common.minimum",
 138  
                                                 new Object[] { ivalue,
 139  
                                                                new Integer(minimum) }),
 140  
                                         null));
 141  
                 }
 142  
             }
 143  6
         } else if (maximumSet) {
 144  3
             if (!INSTANCE.maxValue(ivalue, maximum)) {
 145  1
                 throw new ValidatorException
 146  
                   (new FacesMessage(FacesMessage.SEVERITY_ERROR,
 147  
                                     message(context, "Common.maximum",
 148  
                                             new Object[] { ivalue,
 149  
                                                            new Integer(maximum) }),
 150  
                                     null));
 151  
             }
 152  
         }
 153  
 
 154  10
     }
 155  
 
 156  
 
 157  
     // ----------------------------------------------------- StateHolder Methods
 158  
 
 159  
 
 160  
     /** {@inheritDoc} */
 161  
     public void restoreState(FacesContext context, Object state) {
 162  
 
 163  
         Object[] values = (Object[]) state;
 164  
         super.restoreState(context, values[0]);
 165  
         maximum = ((Integer) values[1]).intValue();
 166  
         maximumSet = ((Boolean) values[2]).booleanValue();
 167  
         minimum = ((Integer) values[3]).intValue();
 168  
         minimumSet = ((Boolean) values[4]).booleanValue();
 169  
 
 170  
     }
 171  
 
 172  
 
 173  
     /** {@inheritDoc} */
 174  
     public Object saveState(FacesContext context) {
 175  
 
 176  
         Object[] values = new Object[5];
 177  
         values[0] = super.saveState(context);
 178  
         values[1] = new Integer(maximum);
 179  
         values[2] = this.maximumSet ? Boolean.TRUE : Boolean.FALSE;
 180  
         values[3] = new Integer(minimum);
 181  
         values[4] = this.minimumSet ? Boolean.TRUE : Boolean.FALSE;
 182  
         return values;
 183  
 
 184  
     }
 185  
 
 186  
 
 187  
     // ---------------------------------------------------------- Object Methods
 188  
 
 189  
 
 190  
 }