Coverage Report - org.apache.commons.validator.ValidatorResult
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidatorResult
76%
13/17
50%
2/4
1.133
ValidatorResult$ResultStatus
57%
8/14
N/A
1.133
 
 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  
 package org.apache.commons.validator;
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.util.Collections;
 21  
 import java.util.HashMap;
 22  
 import java.util.Map;
 23  
 import java.util.Iterator;
 24  
 
 25  
 /**
 26  
  * This contains the results of a set of validation rules processed
 27  
  * on a JavaBean.
 28  
  *
 29  
  * @version $Revision: 1739361 $
 30  
  */
 31  
 //TODO mutable non-private fields
 32  
 public class ValidatorResult implements Serializable {
 33  
 
 34  
     private static final long serialVersionUID = -3713364681647250531L;
 35  
 
 36  
     /**
 37  
      * Map of results.  The key is the name of the <code>ValidatorAction</code>
 38  
      * and the value is whether or not this field passed or not.
 39  
      */
 40  184
     protected Map<String, ResultStatus> hAction = new HashMap<String, ResultStatus>();
 41  
 
 42  
     /**
 43  
      * <code>Field</code> being validated.
 44  
      * TODO This variable is not used.  Need to investigate removing it.
 45  
      */
 46  184
     protected Field field = null;
 47  
 
 48  
     /**
 49  
      * Constructs a <code>ValidatorResult</code> with the associated field being
 50  
      * validated.
 51  
      * @param field Field that was validated.
 52  
      */
 53  184
     public ValidatorResult(Field field) {
 54  184
         this.field = field;
 55  184
     }
 56  
 
 57  
     /**
 58  
      * Add the result of a validator action.
 59  
      * @param validatorName Name of the validator.
 60  
      * @param result Whether the validation passed or failed.
 61  
      */
 62  
     public void add(String validatorName, boolean result) {
 63  0
         this.add(validatorName, result, null);
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Add the result of a validator action.
 68  
      * @param validatorName Name of the validator.
 69  
      * @param result Whether the validation passed or failed.
 70  
      * @param value Value returned by the validator.
 71  
      */
 72  
     public void add(String validatorName, boolean result, Object value) {
 73  199
         hAction.put(validatorName, new ResultStatus(result, value));
 74  199
     }
 75  
 
 76  
     /**
 77  
      * Indicate whether a specified validator is in the Result.
 78  
      * @param validatorName Name of the validator.
 79  
      * @return true if the validator is in the result.
 80  
      */
 81  
     public boolean containsAction(String validatorName) {
 82  168
         return hAction.containsKey(validatorName);
 83  
     }
 84  
 
 85  
     /**
 86  
      * Indicate whether a specified validation passed.
 87  
      * @param validatorName Name of the validator.
 88  
      * @return true if the validation passed.
 89  
      */
 90  
     public boolean isValid(String validatorName) {
 91  147
         ResultStatus status = hAction.get(validatorName);
 92  147
         return (status == null) ? false : status.isValid();
 93  
     }
 94  
 
 95  
     /**
 96  
      * Return the result of a validation.
 97  
      * @param validatorName Name of the validator.
 98  
      * @return The validation result.
 99  
      */
 100  
     public Object getResult(String validatorName) {
 101  20
         ResultStatus status = hAction.get(validatorName);
 102  20
         return (status == null) ? null : status.getResult();
 103  
     }
 104  
 
 105  
     /**
 106  
      * Return an Iterator of the action names contained in this Result.
 107  
      * @return The set of action names.
 108  
      */
 109  
     public Iterator<String> getActions() {
 110  20
         return Collections.unmodifiableMap(hAction).keySet().iterator();
 111  
     }
 112  
 
 113  
     /**
 114  
      * Return a Map of the validator actions in this Result.
 115  
      * @return Map of validator actions.
 116  
      * @deprecated Use getActions() to return the set of actions
 117  
      *             the isValid(name) and getResult(name) methods
 118  
      *             to determine the contents of ResultStatus.
 119  
      *
 120  
      */
 121  
     @Deprecated
 122  
     public Map<String, ResultStatus> getActionMap() {
 123  0
         return Collections.unmodifiableMap(hAction);
 124  
     }
 125  
 
 126  
     /**
 127  
      * Returns the Field that was validated.
 128  
      * @return The Field associated with this result.
 129  
      */
 130  
     public Field getField() {
 131  0
         return this.field;
 132  
     }
 133  
 
 134  
     /**
 135  
      * Contains the status of the validation.
 136  
      */
 137  
     protected static class ResultStatus implements Serializable {
 138  
 
 139  
         private static final long serialVersionUID = 4076665918535320007L;
 140  
 
 141  199
         private boolean valid = false;
 142  199
         private Object result = null;
 143  
 
 144  
        /**
 145  
         * Construct a Result status.
 146  
         * @param valid Whether the validator passed or failed.
 147  
         * @param result Value returned by the validator.
 148  
         */
 149  199
         public ResultStatus(boolean valid, Object result) {
 150  199
             this.valid = valid;
 151  199
             this.result = result;
 152  199
         }
 153  
         /**
 154  
          * Provided for backwards binary compatibility only.
 155  
          *
 156  
          * @param ignored ignored by this method
 157  
          * @param valid Whether the validator passed or failed.
 158  
          * @param result Value returned by the validator.
 159  
          *
 160  
          * @deprecated Use {@code ResultStatus(boolean, Object)} instead
 161  
          */
 162  
         @Deprecated
 163  
         public ResultStatus(ValidatorResult ignored, boolean valid, Object result) {
 164  0
             this(valid, result);
 165  0
         }
 166  
 
 167  
         /**
 168  
          * Tests whether or not the validation passed.
 169  
          * @return true if the result was good.
 170  
          */
 171  
         public boolean isValid() {
 172  147
             return valid;
 173  
         }
 174  
 
 175  
         /**
 176  
          * Sets whether or not the validation passed.
 177  
          * @param valid Whether the validation passed.
 178  
          */
 179  
         public void setValid(boolean valid) {
 180  0
             this.valid = valid;
 181  0
         }
 182  
 
 183  
         /**
 184  
          * Gets the result returned by a validation method.
 185  
          * This can be used to retrieve to the correctly
 186  
          * typed value of a date validation for example.
 187  
          * @return The value returned by the validation.
 188  
          */
 189  
         public Object getResult() {
 190  20
             return result;
 191  
         }
 192  
 
 193  
         /**
 194  
          * Sets the result returned by a validation method.
 195  
          * This can be used to retrieve to the correctly
 196  
          * typed value of a date validation for example.
 197  
          * @param result The value returned by the validation.
 198  
          */
 199  
         public void setResult(Object result) {
 200  0
             this.result = result;
 201  0
         }
 202  
 
 203  
     }
 204  
 
 205  
 }