ValidatorResult.java

  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. import java.io.Serializable;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.Map;

  23. /**
  24.  * This contains the results of a set of validation rules processed
  25.  * on a JavaBean.
  26.  */
  27. //TODO mutable non-private fields
  28. public class ValidatorResult implements Serializable {

  29.     /**
  30.      * Contains the status of the validation.
  31.      */
  32.     protected static class ResultStatus implements Serializable {

  33.         private static final long serialVersionUID = 4076665918535320007L;

  34.         /** Whether or not the validation passed. */
  35.         private boolean valid;

  36.         /** Result returned by a validation method. */
  37.         private Object result;

  38.        /**
  39.         * Constructs a Result status.
  40.         * @param valid Whether the validator passed or failed.
  41.         * @param result Value returned by the validator.
  42.         */
  43.         public ResultStatus(final boolean valid, final Object result) {
  44.             this.valid = valid;
  45.             this.result = result;
  46.         }
  47.         /**
  48.          * Provided for backwards binary compatibility only.
  49.          *
  50.          * @param ignored ignored by this method
  51.          * @param valid Whether the validator passed or failed.
  52.          * @param result Value returned by the validator.
  53.          *
  54.          * @deprecated Use {@code ResultStatus(boolean, Object)} instead
  55.          */
  56.         @Deprecated
  57.         public ResultStatus(final ValidatorResult ignored, final boolean valid, final Object result) {
  58.             this(valid, result);
  59.         }

  60.         /**
  61.          * Gets the result returned by a validation method.
  62.          * This can be used to retrieve to the correctly
  63.          * typed value of a date validation for example.
  64.          * @return The value returned by the validation.
  65.          */
  66.         public Object getResult() {
  67.             return result;
  68.         }

  69.         /**
  70.          * Tests whether or not the validation passed.
  71.          * @return true if the result was good.
  72.          */
  73.         public boolean isValid() {
  74.             return valid;
  75.         }

  76.         /**
  77.          * Sets the result returned by a validation method.
  78.          * This can be used to retrieve to the correctly
  79.          * typed value of a date validation for example.
  80.          * @param result The value returned by the validation.
  81.          */
  82.         public void setResult(final Object result) {
  83.             this.result = result;
  84.         }

  85.         /**
  86.          * Sets whether or not the validation passed.
  87.          * @param valid Whether the validation passed.
  88.          */
  89.         public void setValid(final boolean valid) {
  90.             this.valid = valid;
  91.         }

  92.     }

  93.     private static final long serialVersionUID = -3713364681647250531L;

  94.     /**
  95.      * Map of results.  The key is the name of the <code>ValidatorAction</code>
  96.      * and the value is whether or not this field passed or not.
  97.      */
  98.     protected Map<String, ResultStatus> hAction = new HashMap<>();

  99.     /**
  100.      * <code>Field</code> being validated.
  101.      * TODO This variable is not used.  Need to investigate removing it.
  102.      */
  103.     protected Field field;

  104.     /**
  105.      * Constructs a <code>ValidatorResult</code> with the associated field being
  106.      * validated.
  107.      * @param field Field that was validated.
  108.      */
  109.     public ValidatorResult(final Field field) {
  110.         this.field = field;
  111.     }

  112.     /**
  113.      * Add the result of a validator action.
  114.      * @param validatorName Name of the validator.
  115.      * @param result Whether the validation passed or failed.
  116.      */
  117.     public void add(final String validatorName, final boolean result) {
  118.         this.add(validatorName, result, null);
  119.     }

  120.     /**
  121.      * Add the result of a validator action.
  122.      * @param validatorName Name of the validator.
  123.      * @param result Whether the validation passed or failed.
  124.      * @param value Value returned by the validator.
  125.      */
  126.     public void add(final String validatorName, final boolean result, final Object value) {
  127.         hAction.put(validatorName, new ResultStatus(result, value));
  128.     }

  129.     /**
  130.      * Indicate whether a specified validator is in the Result.
  131.      * @param validatorName Name of the validator.
  132.      * @return true if the validator is in the result.
  133.      */
  134.     public boolean containsAction(final String validatorName) {
  135.         return hAction.containsKey(validatorName);
  136.     }

  137.     /**
  138.      * Gets a Map of the validator actions in this Result.
  139.      * @return Map of validator actions.
  140.      * @deprecated Use getActions() to return the set of actions
  141.      *             the isValid(name) and getResult(name) methods
  142.      *             to determine the contents of ResultStatus.
  143.      */
  144.     @Deprecated
  145.     public Map<String, ResultStatus> getActionMap() {
  146.         return Collections.unmodifiableMap(hAction);
  147.     }

  148.     /**
  149.      * Gets an Iterator of the action names contained in this Result.
  150.      * @return The set of action names.
  151.      */
  152.     public Iterator<String> getActions() {
  153.         return Collections.unmodifiableMap(hAction).keySet().iterator();
  154.     }

  155.     /**
  156.      * Returns the Field that was validated.
  157.      * @return The Field associated with this result.
  158.      */
  159.     public Field getField() {
  160.         return this.field;
  161.     }

  162.     /**
  163.      * Gets the result of a validation.
  164.      * @param validatorName Name of the validator.
  165.      * @return The validation result.
  166.      */
  167.     public Object getResult(final String validatorName) {
  168.         final ResultStatus status = hAction.get(validatorName);
  169.         return status == null ? null : status.getResult();
  170.     }

  171.     /**
  172.      * Indicate whether a specified validation passed.
  173.      * @param validatorName Name of the validator.
  174.      * @return true if the validation passed.
  175.      */
  176.     public boolean isValid(final String validatorName) {
  177.         final ResultStatus status = hAction.get(validatorName);
  178.         return status != null && status.isValid();
  179.     }

  180. }