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.Iterator;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  
25  import org.apache.commons.beanutils.PropertyUtils;
26  import org.apache.commons.collections.FastHashMap; // DEPRECATED
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.commons.validator.Arg;
30  import org.apache.commons.validator.Msg;
31  import org.apache.commons.validator.Var;
32  
33  /**
34   * Basic utility methods.
35   * <p>
36   * The use of FastHashMap is deprecated and will be replaced in a future
37   * release.
38   * </p>
39   */
40  public class ValidatorUtils {
41  
42      private static final Log LOG = LogFactory.getLog(ValidatorUtils.class);
43  
44      /**
45       * Makes a deep copy of a <code>FastHashMap</code> if the values
46       * are <code>Msg</code>, <code>Arg</code>,
47       * or <code>Var</code>.  Otherwise it is a shallow copy.
48       *
49       * @param fastHashMap <code>FastHashMap</code> to copy.
50       * @return FastHashMap A copy of the <code>FastHashMap</code> that was
51       * passed in.
52       * @deprecated This method is not part of Validator's public API.  Validator
53       * will use it internally until FastHashMap references are removed.  Use
54       * copyMap() instead.
55       */
56      @Deprecated
57      public static FastHashMap copyFastHashMap(final FastHashMap fastHashMap) {
58          final FastHashMap results = new FastHashMap();
59          @SuppressWarnings("unchecked") // FastHashMap is not generic
60          final Iterator<Entry<String, ?>> iterator = fastHashMap.entrySet().iterator();
61          while (iterator.hasNext()) {
62              final Entry<String, ?> entry = iterator.next();
63              final String key = entry.getKey();
64              final Object value = entry.getValue();
65              if (value instanceof Msg) {
66                  results.put(key, ((Msg) value).clone());
67              } else if (value instanceof Arg) {
68                  results.put(key, ((Arg) value).clone());
69              } else if (value instanceof Var) {
70                  results.put(key, ((Var) value).clone());
71              } else {
72                  results.put(key, value);
73              }
74          }
75          results.setFast(true);
76          return results;
77      }
78  
79      /**
80       * Makes a deep copy of a <code>Map</code> if the values are
81       * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise,
82       * it is a shallow copy.
83       *
84       * @param map The source Map to copy.
85       *
86       * @return A copy of the <code>Map</code> that was passed in.
87       */
88      public static Map<String, Object> copyMap(final Map<String, Object> map) {
89          final Map<String, Object> results = new HashMap<>(map.size());
90          map.forEach((key, value) -> {
91              if (value instanceof Msg) {
92                  results.put(key, ((Msg) value).clone());
93              } else if (value instanceof Arg) {
94                  results.put(key, ((Arg) value).clone());
95              } else if (value instanceof Var) {
96                  results.put(key, ((Var) value).clone());
97              } else {
98                  results.put(key, value);
99              }
100         });
101         return results;
102     }
103 
104     /**
105      * Convenience method for getting a value from a bean property as a
106      * <code>String</code>.  If the property is a <code>String[]</code> or
107      * <code>Collection</code> and it is empty, an empty <code>String</code>
108      * "" is returned.  Otherwise, property.toString() is returned.  This method
109      * may return {@code null} if there was an error retrieving the
110      * property.
111      *
112      * @param bean The bean object.
113      * @param property The name of the property to access.
114      *
115      * @return The value of the property.
116      */
117     public static String getValueAsString(final Object bean, final String property) {
118         Object value = null;
119 
120         try {
121             value = PropertyUtils.getProperty(bean, property);
122 
123         } catch (final ReflectiveOperationException e) {
124             LOG.error(e.getMessage(), e);
125         }
126 
127         if (value == null) {
128             return null;
129         }
130 
131         if (value instanceof String[]) {
132             return ((String[]) value).length > 0 ? value.toString() : "";
133 
134         }
135         if (value instanceof Collection) {
136             return ((Collection<?>) value).isEmpty() ? "" : value.toString();
137 
138         }
139         return value.toString();
140 
141     }
142 
143     /**
144      * <p>Replace part of a <code>String</code> with another value.</p>
145      *
146      * @param value <code>String</code> to perform the replacement on.
147      * @param key The name of the constant.
148      * @param replaceValue The value of the constant.
149      *
150      * @return The modified value.
151      */
152     public static String replace(final String value, final String key, final String replaceValue) {
153         if (value == null || key == null || replaceValue == null) {
154             return value;
155         }
156         return value.replace(key, replaceValue);
157     }
158 
159 }