| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.validator.ValidatorResults |
|
|
| 1 | /* |
|
| 2 | * $Id: ValidatorResults.java 376699 2006-02-10 14:27:03Z niallp $ |
|
| 3 | * $Rev: 376699 $ |
|
| 4 | * $Date: 2006-02-10 14:27:03 +0000 (Fri, 10 Feb 2006) $ |
|
| 5 | * |
|
| 6 | * ==================================================================== |
|
| 7 | * Copyright 2001-2006 The Apache Software Foundation |
|
| 8 | * |
|
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 10 | * you may not use this file except in compliance with the License. |
|
| 11 | * You may obtain a copy of the License at |
|
| 12 | * |
|
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 14 | * |
|
| 15 | * Unless required by applicable law or agreed to in writing, software |
|
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 18 | * See the License for the specific language governing permissions and |
|
| 19 | * limitations under the License. |
|
| 20 | */ |
|
| 21 | ||
| 22 | package org.apache.commons.validator; |
|
| 23 | ||
| 24 | import java.io.Serializable; |
|
| 25 | import java.util.Collections; |
|
| 26 | import java.util.HashMap; |
|
| 27 | import java.util.Iterator; |
|
| 28 | import java.util.Map; |
|
| 29 | import java.util.Set; |
|
| 30 | ||
| 31 | /** |
|
| 32 | * This contains the results of a set of validation rules processed |
|
| 33 | * on a JavaBean. |
|
| 34 | */ |
|
| 35 | 874 | public class ValidatorResults implements Serializable { |
| 36 | ||
| 37 | /** |
|
| 38 | * Map of validation results. |
|
| 39 | */ |
|
| 40 | 437 | protected Map hResults = new HashMap(); |
| 41 | ||
| 42 | /** |
|
| 43 | * Merge another ValidatorResults into mine. |
|
| 44 | * |
|
| 45 | * @param results ValidatorResults to merge. |
|
| 46 | */ |
|
| 47 | public void merge(ValidatorResults results) { |
|
| 48 | 322 | this.hResults.putAll(results.hResults); |
| 49 | 322 | } |
| 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 | 163 | ValidatorResult validatorResult = this.getValidatorResult(field.getKey()); |
| 77 | ||
| 78 | 163 | if (validatorResult == null) { |
| 79 | 156 | validatorResult = new ValidatorResult(field); |
| 80 | 156 | this.hResults.put(field.getKey(), validatorResult); |
| 81 | } |
|
| 82 | ||
| 83 | 163 | validatorResult.add(validatorName, result, value); |
| 84 | 163 | } |
| 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 | 495 | return (ValidatorResult) 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 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 getResultValueMap() { |
|
| 133 | 3 | Map results = new HashMap(); |
| 134 | ||
| 135 | 26 | for (Iterator i = hResults.keySet().iterator(); i.hasNext();) { |
| 136 | 20 | String propertyKey = (String) i.next(); |
| 137 | 20 | ValidatorResult vr = this.getValidatorResult(propertyKey); |
| 138 | ||
| 139 | 60 | for (Iterator x = vr.getActions(); x.hasNext();) { |
| 140 | 20 | String actionKey = (String)x.next(); |
| 141 | 20 | Object result = vr.getResult(actionKey); |
| 142 | ||
| 143 | 20 | if (result != null && !(result instanceof Boolean)) { |
| 144 | 18 | results.put(propertyKey, result); |
| 145 | } |
|
| 146 | } |
|
| 147 | } |
|
| 148 | ||
| 149 | 3 | return results; |
| 150 | } |
|
| 151 | ||
| 152 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |