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