ValidatorResults.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. import java.util.Set;

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

  30.     private static final long serialVersionUID = -2709911078904924839L;

  31.     /**
  32.      * Map of validation results.
  33.      */
  34.     protected Map<String, ValidatorResult> hResults = new HashMap<>();

  35.     /**
  36.      * Add a the result of a validator action.
  37.      *
  38.      * @param field The field validated.
  39.      * @param validatorName The name of the validator.
  40.      * @param result The result of the validation.
  41.      */
  42.     public void add(final Field field, final String validatorName, final boolean result) {
  43.         this.add(field, validatorName, result, null);
  44.     }

  45.     /**
  46.      * Add a the result of a validator action.
  47.      *
  48.      * @param field The field validated.
  49.      * @param validatorName The name of the validator.
  50.      * @param result The result of the validation.
  51.      * @param value The value returned by the validator.
  52.      */
  53.     public void add(
  54.             final Field field,
  55.             final String validatorName,
  56.             final boolean result,
  57.             final Object value) {

  58.         ValidatorResult validatorResult = this.getValidatorResult(field.getKey());

  59.         if (validatorResult == null) {
  60.             validatorResult = new ValidatorResult(field);
  61.             this.hResults.put(field.getKey(), validatorResult);
  62.         }

  63.         validatorResult.add(validatorName, result, value);
  64.     }

  65.     /**
  66.      * Clear all results recorded by this object.
  67.      */
  68.     public void clear() {
  69.         this.hResults.clear();
  70.     }

  71.     /**
  72.      * Gets the set of property names for which at least one message has
  73.      * been recorded.
  74.      * @return An unmodifiable Set of the property names.
  75.      */
  76.     public Set<String> getPropertyNames() {
  77.         return Collections.unmodifiableSet(this.hResults.keySet());
  78.     }

  79.     /**
  80.      * Gets a <code>Map</code> of any <code>Object</code>s returned from
  81.      * validation routines.
  82.      *
  83.      * @return Map of objections returned by validators.
  84.      */
  85.     public Map<String, Object> getResultValueMap() {
  86.         final Map<String, Object> results = new HashMap<>();

  87.         for (final String propertyKey : hResults.keySet()) {
  88.             final ValidatorResult vr = this.getValidatorResult(propertyKey);

  89.             for (final Iterator<String> x = vr.getActions(); x.hasNext();) {
  90.                 final String actionKey = x.next();
  91.                 final Object result = vr.getResult(actionKey);

  92.                 if (result != null && !(result instanceof Boolean)) {
  93.                     results.put(propertyKey, result);
  94.                 }
  95.             }
  96.         }

  97.         return results;
  98.     }

  99.     /**
  100.      * Gets the <code>ValidatorResult</code> associated
  101.      * with the key passed in.  The key the <code>ValidatorResult</code>
  102.      * is stored under is the <code>Field</code>'s getKey method.
  103.      *
  104.      * @param key The key generated from <code>Field</code> (this is often just
  105.      * the field name).
  106.      *
  107.      * @return The result of a specified key.
  108.      */
  109.     public ValidatorResult getValidatorResult(final String key) {
  110.         return this.hResults.get(key);
  111.     }

  112.     /**
  113.      * Gets {@code true} if there are no messages recorded
  114.      * in this collection, or {@code false} otherwise.
  115.      *
  116.      * @return Whether these results are empty.
  117.      */
  118.     public boolean isEmpty() {
  119.         return this.hResults.isEmpty();
  120.     }

  121.     /**
  122.      * Merge another ValidatorResults into mine.
  123.      *
  124.      * @param results ValidatorResults to merge.
  125.      */
  126.     public void merge(final ValidatorResults results) {
  127.         this.hResults.putAll(results.hResults);
  128.     }

  129. }