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.  *      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. 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.      * Constructs a new instance.
  37.      */
  38.     public ValidatorResults() {
  39.         // empty
  40.     }

  41.     /**
  42.      * Add the result of a validator action.
  43.      *
  44.      * @param field The field validated.
  45.      * @param validatorName The name of the validator.
  46.      * @param result The result of the validation.
  47.      */
  48.     public void add(final Field field, final String validatorName, final boolean result) {
  49.         this.add(field, validatorName, result, null);
  50.     }

  51.     /**
  52.      * Add 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.      * @param value The value returned by the validator.
  58.      */
  59.     public void add(
  60.             final Field field,
  61.             final String validatorName,
  62.             final boolean result,
  63.             final Object value) {

  64.         ValidatorResult validatorResult = getValidatorResult(field.getKey());

  65.         if (validatorResult == null) {
  66.             validatorResult = new ValidatorResult(field);
  67.             hResults.put(field.getKey(), validatorResult);
  68.         }

  69.         validatorResult.add(validatorName, result, value);
  70.     }

  71.     /**
  72.      * Clear all results recorded by this object.
  73.      */
  74.     public void clear() {
  75.         hResults.clear();
  76.     }

  77.     /**
  78.      * Gets the set of property names for which at least one message has
  79.      * been recorded.
  80.      * @return An unmodifiable Set of the property names.
  81.      */
  82.     public Set<String> getPropertyNames() {
  83.         return Collections.unmodifiableSet(hResults.keySet());
  84.     }

  85.     /**
  86.      * Gets a {@link Map} of any {@code Object}s returned from
  87.      * validation routines.
  88.      *
  89.      * @return Map of objections returned by validators.
  90.      */
  91.     public Map<String, Object> getResultValueMap() {
  92.         final Map<String, Object> results = new HashMap<>();

  93.         for (final String propertyKey : hResults.keySet()) {
  94.             final ValidatorResult vr = getValidatorResult(propertyKey);

  95.             for (final Iterator<String> x = vr.getActions(); x.hasNext();) {
  96.                 final String actionKey = x.next();
  97.                 final Object result = vr.getResult(actionKey);

  98.                 if (result != null && !(result instanceof Boolean)) {
  99.                     results.put(propertyKey, result);
  100.                 }
  101.             }
  102.         }

  103.         return results;
  104.     }

  105.     /**
  106.      * Gets the {@code ValidatorResult} associated
  107.      * with the key passed in.  The key the {@code ValidatorResult}
  108.      * is stored under is the {@code Field}'s getKey method.
  109.      *
  110.      * @param key The key generated from {@code Field} (this is often just
  111.      * the field name).
  112.      *
  113.      * @return The result of a specified key.
  114.      */
  115.     public ValidatorResult getValidatorResult(final String key) {
  116.         return hResults.get(key);
  117.     }

  118.     /**
  119.      * Gets {@code true} if there are no messages recorded
  120.      * in this collection, or {@code false} otherwise.
  121.      *
  122.      * @return Whether these results are empty.
  123.      */
  124.     public boolean isEmpty() {
  125.         return hResults.isEmpty();
  126.     }

  127.     /**
  128.      * Merge another ValidatorResults into mine.
  129.      *
  130.      * @param results ValidatorResults to merge.
  131.      */
  132.     public void merge(final ValidatorResults results) {
  133.         hResults.putAll(results.hResults);
  134.     }

  135. }