ValidatorUtils.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.util;

  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.Map.Entry;

  23. import org.apache.commons.beanutils.PropertyUtils;
  24. import org.apache.commons.collections.FastHashMap; // DEPRECATED
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.commons.validator.Arg;
  28. import org.apache.commons.validator.Msg;
  29. import org.apache.commons.validator.Var;

  30. /**
  31.  * Basic utility methods.
  32.  * <p>
  33.  * The use of FastHashMap is deprecated and will be replaced in a future
  34.  * release.
  35.  * </p>
  36.  */
  37. public class ValidatorUtils {

  38.     private static final Log LOG = LogFactory.getLog(ValidatorUtils.class);

  39.     /**
  40.      * Makes a deep copy of a <code>FastHashMap</code> if the values
  41.      * are <code>Msg</code>, <code>Arg</code>,
  42.      * or <code>Var</code>.  Otherwise it is a shallow copy.
  43.      *
  44.      * @param fastHashMap <code>FastHashMap</code> to copy.
  45.      * @return FastHashMap A copy of the <code>FastHashMap</code> that was
  46.      * passed in.
  47.      * @deprecated This method is not part of Validator's public API.  Validator
  48.      * will use it internally until FastHashMap references are removed.  Use
  49.      * copyMap() instead.
  50.      */
  51.     @Deprecated
  52.     public static FastHashMap copyFastHashMap(final FastHashMap fastHashMap) {
  53.         final FastHashMap results = new FastHashMap();
  54.         @SuppressWarnings("unchecked") // FastHashMap is not generic
  55.         final Iterator<Entry<String, ?>> iterator = fastHashMap.entrySet().iterator();
  56.         while (iterator.hasNext()) {
  57.             final Entry<String, ?> entry = iterator.next();
  58.             final String key = entry.getKey();
  59.             final Object value = entry.getValue();
  60.             if (value instanceof Msg) {
  61.                 results.put(key, ((Msg) value).clone());
  62.             } else if (value instanceof Arg) {
  63.                 results.put(key, ((Arg) value).clone());
  64.             } else if (value instanceof Var) {
  65.                 results.put(key, ((Var) value).clone());
  66.             } else {
  67.                 results.put(key, value);
  68.             }
  69.         }
  70.         results.setFast(true);
  71.         return results;
  72.     }

  73.     /**
  74.      * Makes a deep copy of a <code>Map</code> if the values are
  75.      * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise,
  76.      * it is a shallow copy.
  77.      *
  78.      * @param map The source Map to copy.
  79.      *
  80.      * @return A copy of the <code>Map</code> that was passed in.
  81.      */
  82.     public static Map<String, Object> copyMap(final Map<String, Object> map) {
  83.         final Map<String, Object> results = new HashMap<>(map.size());
  84.         map.forEach((key, value) -> {
  85.             if (value instanceof Msg) {
  86.                 results.put(key, ((Msg) value).clone());
  87.             } else if (value instanceof Arg) {
  88.                 results.put(key, ((Arg) value).clone());
  89.             } else if (value instanceof Var) {
  90.                 results.put(key, ((Var) value).clone());
  91.             } else {
  92.                 results.put(key, value);
  93.             }
  94.         });
  95.         return results;
  96.     }

  97.     /**
  98.      * Convenience method for getting a value from a bean property as a
  99.      * <code>String</code>.  If the property is a <code>String[]</code> or
  100.      * <code>Collection</code> and it is empty, an empty <code>String</code>
  101.      * "" is returned.  Otherwise, property.toString() is returned.  This method
  102.      * may return {@code null} if there was an error retrieving the
  103.      * property.
  104.      *
  105.      * @param bean The bean object.
  106.      * @param property The name of the property to access.
  107.      *
  108.      * @return The value of the property.
  109.      */
  110.     public static String getValueAsString(final Object bean, final String property) {
  111.         Object value = null;

  112.         try {
  113.             value = PropertyUtils.getProperty(bean, property);

  114.         } catch (final ReflectiveOperationException e) {
  115.             LOG.error(e.getMessage(), e);
  116.         }

  117.         if (value == null) {
  118.             return null;
  119.         }

  120.         if (value instanceof String[]) {
  121.             return ((String[]) value).length > 0 ? value.toString() : "";

  122.         }
  123.         if (value instanceof Collection) {
  124.             return ((Collection<?>) value).isEmpty() ? "" : value.toString();

  125.         }
  126.         return value.toString();

  127.     }

  128.     /**
  129.      * <p>Replace part of a <code>String</code> with another value.</p>
  130.      *
  131.      * @param value <code>String</code> to perform the replacement on.
  132.      * @param key The name of the constant.
  133.      * @param replaceValue The value of the constant.
  134.      *
  135.      * @return The modified value.
  136.      */
  137.     public static String replace(final String value, final String key, final String replaceValue) {
  138.         if (value == null || key == null || replaceValue == null) {
  139.             return value;
  140.         }
  141.         return value.replace(key, replaceValue);
  142.     }

  143. }