View Javadoc
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  
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Map;
22  
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  /**
32   * Basic utility methods.
33   * <p>
34   * The use of FastHashMap is deprecated and will be replaced in a future
35   * release.
36   * </p>
37   */
38  public class ValidatorUtils {
39  
40      private static final Log LOG = LogFactory.getLog(ValidatorUtils.class);
41  
42      /**
43       * Makes a deep copy of a <code>FastHashMap</code> if the values
44       * are <code>Msg</code>, <code>Arg</code>,
45       * or <code>Var</code>.  Otherwise it is a shallow copy.
46       *
47       * @param fastHashMap <code>FastHashMap</code> to copy.
48       * @return FastHashMap A copy of the <code>FastHashMap</code> that was
49       * passed in.
50       * @deprecated This method is not part of Validator's public API.  Validator
51       * will use it internally until FastHashMap references are removed.  Use
52       * copyMap() instead.
53       */
54      @Deprecated
55      public static FastHashMap copyFastHashMap(final FastHashMap fastHashMap) {
56          final FastHashMap results = new FastHashMap();
57          @SuppressWarnings("unchecked") // FastHashMap is not generic
58          final HashMap<String, ?> map = fastHashMap;
59          map.forEach((key, value) -> {
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      /**
75       * Makes a deep copy of a <code>Map</code> if the values are
76       * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise,
77       * it is a shallow copy.
78       *
79       * @param map The source Map to copy.
80       *
81       * @return A copy of the <code>Map</code> that was passed in.
82       */
83      public static Map<String, Object> copyMap(final Map<String, Object> map) {
84          final Map<String, Object> results = new HashMap<>();
85          map.forEach((key, value) -> {
86              if (value instanceof Msg) {
87                  results.put(key, ((Msg) value).clone());
88              } else if (value instanceof Arg) {
89                  results.put(key, ((Arg) value).clone());
90              } else if (value instanceof Var) {
91                  results.put(key, ((Var) value).clone());
92              } else {
93                  results.put(key, value);
94              }
95          });
96          return results;
97      }
98  
99      /**
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 }