    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 *      http://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.util;
    018
    019import java.util.Collection;
    020import java.util.HashMap;
    021import java.util.Map;
    022
    023import org.apache.commons.beanutils.PropertyUtils;
    024import org.apache.commons.collections.FastHashMap; // DEPRECATED
    025import org.apache.commons.logging.Log;
    026import org.apache.commons.logging.LogFactory;
    027import org.apache.commons.validator.Arg;
    028import org.apache.commons.validator.Msg;
    029import org.apache.commons.validator.Var;
    030
    031/**
    032 * Basic utility methods.
    033 * <p>
    034 * The use of FastHashMap is deprecated and will be replaced in a future
    035 * release.
    036 * </p>
    037 */
    038public class ValidatorUtils {
    039
    040    private static final Log LOG = LogFactory.getLog(ValidatorUtils.class);
    041
    042    /**
    043     * Makes a deep copy of a <code>FastHashMap</code> if the values
    044     * are <code>Msg</code>, <code>Arg</code>,
    045     * or <code>Var</code>.  Otherwise it is a shallow copy.
    046     *
    047     * @param fastHashMap <code>FastHashMap</code> to copy.
    048     * @return FastHashMap A copy of the <code>FastHashMap</code> that was
    049     * passed in.
    050     * @deprecated This method is not part of Validator's public API.  Validator
    051     * will use it internally until FastHashMap references are removed.  Use
    052     * copyMap() instead.
    053     */
    054    @Deprecated
    055    public static FastHashMap copyFastHashMap(final FastHashMap fastHashMap) {
    056        final FastHashMap results = new FastHashMap();
    057        @SuppressWarnings("unchecked") // FastHashMap is not generic
    058        final HashMap<String, ?> map = fastHashMap;
    059        map.forEach((key, value) -> {
    060            if (value instanceof Msg) {
    061                results.put(key, ((Msg) value).clone());
    062            } else if (value instanceof Arg) {
    063                results.put(key, ((Arg) value).clone());
    064            } else if (value instanceof Var) {
    065                results.put(key, ((Var) value).clone());
    066            } else {
    067                results.put(key, value);
    068            }
    069        });
    070        results.setFast(true);
    071        return results;
    072    }
    073
    074    /**
    075     * Makes a deep copy of a <code>Map</code> if the values are
    076     * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise,
    077     * it is a shallow copy.
    078     *
    079     * @param map The source Map to copy.
    080     *
    081     * @return A copy of the <code>Map</code> that was passed in.
    082     */
    083    public static Map<String, Object> copyMap(final Map<String, Object> map) {
    084        final Map<String, Object> results = new HashMap<>();
    085        map.forEach((key, value) -> {
    086            if (value instanceof Msg) {
    087                results.put(key, ((Msg) value).clone());
    088            } else if (value instanceof Arg) {
    089                results.put(key, ((Arg) value).clone());
    090            } else if (value instanceof Var) {
    091                results.put(key, ((Var) value).clone());
    092            } else {
    093                results.put(key, value);
    094            }
    095        });
    096        return results;
    097    }
    098
    099    /**
    100     * Convenience method for getting a value from a bean property as a
    101     * <code>String</code>.  If the property is a <code>String[]</code> or
    102     * <code>Collection</code> and it is empty, an empty <code>String</code>
    103     * "" is returned.  Otherwise, property.toString() is returned.  This method
    104     * may return <code>null</code> if there was an error retrieving the
    105     * property.
    106     *
    107     * @param bean The bean object.
    108     * @param property The name of the property to access.
    109     *
    110     * @return The value of the property.
    111     */
    112    public static String getValueAsString(final Object bean, final String property) {
    113        Object value = null;
    114
    115        try {
    116            value = PropertyUtils.getProperty(bean, property);
    117
    118        } catch (ReflectiveOperationException e) {
    119            LOG.error(e.getMessage(), e);
    120        }
    121
    122        if (value == null) {
    123            return null;
    124        }
    125
    126        if (value instanceof String[]) {
    127            return ((String[]) value).length > 0 ? value.toString() : "";
    128
    129        }
    130        if (value instanceof Collection) {
    131            return ((Collection<?>) value).isEmpty() ? "" : value.toString();
    132
    133        }
    134        return value.toString();
    135
    136    }
    137
    138    /**
    139     * <p>Replace part of a <code>String</code> with another value.</p>
    140     *
    141     * @param value <code>String</code> to perform the replacement on.
    142     * @param key The name of the constant.
    143     * @param replaceValue The value of the constant.
    144     *
    145     * @return The modified value.
    146     */
    147    public static String replace(String value, final String key, final String replaceValue) {
    148        if (value == null || key == null || replaceValue == null) {
    149            return value;
    150        }
    151        return value.replace(key, replaceValue);
    152    }
    153
    154}