001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.validator; 018 019import java.io.Serializable; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.Iterator; 023import java.util.Map; 024 025/** 026 * This contains the results of a set of validation rules processed 027 * on a JavaBean. 028 */ 029//TODO mutable non-private fields 030public class ValidatorResult implements Serializable { 031 032 /** 033 * Contains the status of the validation. 034 */ 035 protected static class ResultStatus implements Serializable { 036 037 private static final long serialVersionUID = 4076665918535320007L; 038 039 /** Whether or not the validation passed. */ 040 private boolean valid; 041 042 /** Result returned by a validation method. */ 043 private Object result; 044 045 /** 046 * Constructs a Result status. 047 * 048 * @param valid Whether the validator passed or failed. 049 * @param result Value returned by the validator. 050 */ 051 public ResultStatus(final boolean valid, final Object result) { 052 this.valid = valid; 053 this.result = result; 054 } 055 056 /** 057 * Provided for backwards binary compatibility only. 058 * 059 * @param ignored ignored by this method 060 * @param valid Whether the validator passed or failed. 061 * @param result Value returned by the validator. 062 * @deprecated Use {@code ResultStatus(boolean, Object)} instead 063 */ 064 @Deprecated 065 public ResultStatus(final ValidatorResult ignored, final boolean valid, final Object result) { 066 this(valid, result); 067 } 068 069 /** 070 * Gets the result returned by a validation method. 071 * This can be used to retrieve to the correctly 072 * typed value of a date validation for example. 073 * 074 * @return The value returned by the validation. 075 */ 076 public Object getResult() { 077 return result; 078 } 079 080 /** 081 * Tests whether or not the validation passed. 082 * 083 * @return true if the result was good. 084 */ 085 public boolean isValid() { 086 return valid; 087 } 088 089 /** 090 * Sets the result returned by a validation method. 091 * This can be used to retrieve to the correctly 092 * typed value of a date validation for example. 093 * 094 * @param result The value returned by the validation. 095 */ 096 public void setResult(final Object result) { 097 this.result = result; 098 } 099 100 /** 101 * Sets whether or not the validation passed. 102 * 103 * @param valid Whether the validation passed. 104 */ 105 public void setValid(final boolean valid) { 106 this.valid = valid; 107 } 108 109 } 110 111 private static final long serialVersionUID = -3713364681647250531L; 112 113 /** 114 * Map of results. The key is the name of the {@code ValidatorAction} 115 * and the value is whether or not this field passed or not. 116 */ 117 protected Map<String, ResultStatus> hAction = new HashMap<>(); 118 119 /** 120 * {@code Field} being validated. 121 * TODO This variable is not used. Need to investigate removing it. 122 */ 123 protected Field field; 124 125 /** 126 * Constructs a {@code ValidatorResult} with the associated field being 127 * validated. 128 * 129 * @param field Field that was validated. 130 */ 131 public ValidatorResult(final Field field) { 132 this.field = field; 133 } 134 135 /** 136 * Add the result of a validator action. 137 * 138 * @param validatorName Name of the validator. 139 * @param result Whether the validation passed or failed. 140 */ 141 public void add(final String validatorName, final boolean result) { 142 this.add(validatorName, result, null); 143 } 144 145 /** 146 * Add the result of a validator action. 147 * 148 * @param validatorName Name of the validator. 149 * @param result Whether the validation passed or failed. 150 * @param value Value returned by the validator. 151 */ 152 public void add(final String validatorName, final boolean result, final Object value) { 153 hAction.put(validatorName, new ResultStatus(result, value)); 154 } 155 156 /** 157 * Indicate whether a specified validator is in the Result. 158 * 159 * @param validatorName Name of the validator. 160 * @return true if the validator is in the result. 161 */ 162 public boolean containsAction(final String validatorName) { 163 return hAction.containsKey(validatorName); 164 } 165 166 /** 167 * Gets a Map of the validator actions in this Result. 168 * 169 * @return Map of validator actions. 170 * @deprecated Use getActions() to return the set of actions 171 * the isValid(name) and getResult(name) methods 172 * to determine the contents of ResultStatus. 173 */ 174 @Deprecated 175 public Map<String, ResultStatus> getActionMap() { 176 return Collections.unmodifiableMap(hAction); 177 } 178 179 /** 180 * Gets an Iterator of the action names contained in this Result. 181 * 182 * @return The set of action names. 183 */ 184 public Iterator<String> getActions() { 185 return Collections.unmodifiableMap(hAction).keySet().iterator(); 186 } 187 188 /** 189 * Returns the Field that was validated. 190 * 191 * @return The Field associated with this result. 192 */ 193 public Field getField() { 194 return field; 195 } 196 197 /** 198 * Gets the result of a validation. 199 * 200 * @param validatorName Name of the validator. 201 * @return The validation result. 202 */ 203 public Object getResult(final String validatorName) { 204 final ResultStatus status = hAction.get(validatorName); 205 return status == null ? null : status.getResult(); 206 } 207 208 /** 209 * Indicate whether a specified validation passed. 210 * 211 * @param validatorName Name of the validator. 212 * @return true if the validation passed. 213 */ 214 public boolean isValid(final String validatorName) { 215 final ResultStatus status = hAction.get(validatorName); 216 return status != null && status.isValid(); 217 } 218 219}