Coverage Report - org.apache.commons.validator.ValidatorResults
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidatorResults
82%
24/29
80%
8/10
1.625
 
 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.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * This contains the results of a set of validation rules processed
 28  
  * on a JavaBean.
 29  
  *
 30  
  * @version $Revision: 1739361 $
 31  
  */
 32  
 //TODO mutable non-private fields
 33  505
 public class ValidatorResults implements Serializable {
 34  
 
 35  
     private static final long serialVersionUID = -2709911078904924839L;
 36  
 
 37  
     /**
 38  
      * Map of validation results.
 39  
      */
 40  505
     protected Map<String, ValidatorResult> hResults = new HashMap<String, ValidatorResult>();
 41  
 
 42  
     /**
 43  
      * Merge another ValidatorResults into mine.
 44  
      *
 45  
      * @param results ValidatorResults to merge.
 46  
      */
 47  
     public void merge(ValidatorResults results) {
 48  378
         this.hResults.putAll(results.hResults);
 49  378
     }
 50  
 
 51  
     /**
 52  
      * Add a the result of a validator action.
 53  
      *
 54  
      * @param field The field validated.
 55  
      * @param validatorName The name of the validator.
 56  
      * @param result The result of the validation.
 57  
      */
 58  
     public void add(Field field, String validatorName, boolean result) {
 59  0
         this.add(field, validatorName, result, null);
 60  0
     }
 61  
 
 62  
     /**
 63  
      * Add a the result of a validator action.
 64  
      *
 65  
      * @param field The field validated.
 66  
      * @param validatorName The name of the validator.
 67  
      * @param result The result of the validation.
 68  
      * @param value The value returned by the validator.
 69  
      */
 70  
     public void add(
 71  
             Field field,
 72  
             String validatorName,
 73  
             boolean result,
 74  
             Object value) {
 75  
 
 76  199
         ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
 77  
 
 78  199
         if (validatorResult == null) {
 79  184
             validatorResult = new ValidatorResult(field);
 80  184
             this.hResults.put(field.getKey(), validatorResult);
 81  
         }
 82  
 
 83  199
         validatorResult.add(validatorName, result, value);
 84  199
     }
 85  
 
 86  
     /**
 87  
      * Clear all results recorded by this object.
 88  
      */
 89  
     public void clear() {
 90  0
         this.hResults.clear();
 91  0
     }
 92  
 
 93  
     /**
 94  
      * Return <code>true</code> if there are no messages recorded
 95  
      * in this collection, or <code>false</code> otherwise.
 96  
      *
 97  
      * @return Whether these results are empty.
 98  
      */
 99  
     public boolean isEmpty() {
 100  0
         return this.hResults.isEmpty();
 101  
     }
 102  
 
 103  
     /**
 104  
      * Gets the <code>ValidatorResult</code> associated
 105  
      * with the key passed in.  The key the <code>ValidatorResult</code>
 106  
      * is stored under is the <code>Field</code>'s getKey method.
 107  
      *
 108  
      * @param key The key generated from <code>Field</code> (this is often just
 109  
      * the field name).
 110  
      *
 111  
      * @return The result of a specified key.
 112  
      */
 113  
     public ValidatorResult getValidatorResult(String key) {
 114  588
         return this.hResults.get(key);
 115  
     }
 116  
 
 117  
     /**
 118  
      * Return the set of property names for which at least one message has
 119  
      * been recorded.
 120  
      * @return An unmodifiable Set of the property names.
 121  
      */
 122  
     public Set<String> getPropertyNames() {
 123  3
         return Collections.unmodifiableSet(this.hResults.keySet());
 124  
     }
 125  
 
 126  
     /**
 127  
      * Get a <code>Map</code> of any <code>Object</code>s returned from
 128  
      * validation routines.
 129  
      *
 130  
      * @return Map of objections returned by validators.
 131  
      */
 132  
     public Map<String, Object> getResultValueMap() {
 133  3
         Map<String, Object> results = new HashMap<String, Object>();
 134  
 
 135  3
         for (Iterator<String> i = hResults.keySet().iterator(); i.hasNext();) {
 136  20
             String propertyKey = i.next();
 137  20
             ValidatorResult vr = this.getValidatorResult(propertyKey);
 138  
 
 139  20
             for (Iterator<String> x = vr.getActions(); x.hasNext();) {
 140  20
                 String actionKey = x.next();
 141  20
                 Object result = vr.getResult(actionKey);
 142  
 
 143  20
                 if (result != null && !(result instanceof Boolean)) {
 144  20
                     results.put(propertyKey, result);
 145  
                 }
 146  20
             }
 147  20
         }
 148  
 
 149  3
         return results;
 150  
     }
 151  
 
 152  
 }