View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util;
8   
9   import java.util.Arrays;
10  import java.util.Collection;
11  import java.util.Collections;
12  import java.util.Iterator;
13  import java.util.Map;
14  
15  // @ThreadSafe
16  public class MapUtils extends org.apache.commons.collections.MapUtils
17  {
18  
19      /**
20       * Convenience method for CollectionUtil#mapWithKeysAndValues(Class, Iterator,
21       * Iterator); keys and values can be null or empty.
22       */
23      public static <K, V> Map<K, V> mapWithKeysAndValues(Class<? extends Map> mapClass,
24                                                          K[] keys,
25                                                          V[] values)
26      {
27          Collection<K> keyCollection = (keys != null ? Arrays.asList(keys) : Collections.EMPTY_LIST);
28          Collection<V> valuesCollection = (values != null ? Arrays.asList(values) : Collections.EMPTY_LIST);
29          return mapWithKeysAndValues(mapClass, keyCollection.iterator(), valuesCollection.iterator());
30      }
31  
32      /**
33       * Convenience method for CollectionUtil#mapWithKeysAndValues(Class, Iterator,
34       * Iterator); keys and values can be null or empty.
35       */
36      public static <K, V> Map<K, V> mapWithKeysAndValues(Class<? extends Map> mapClass,
37                                                          Collection<K> keys,
38                                                          Collection<V> values)
39      {
40          keys = (keys != null ? keys : Collections.EMPTY_LIST);
41          values = (values != null ? values : Collections.EMPTY_LIST);
42          return mapWithKeysAndValues(mapClass, keys.iterator(), values.iterator());
43      }
44  
45      /**
46       * Create & populate a Map of arbitrary class. Populating stops when either the
47       * keys or values iterator is null or exhausted.
48       * 
49       * @param mapClass the Class of the Map to instantiate
50       * @param keys iterator for Objects ued as keys
51       * @param values iterator for Objects used as values
52       * @return the instantiated Map
53       */
54      public static <K, V> Map<K, V> mapWithKeysAndValues(Class<? extends Map> mapClass,
55                                                          Iterator<K> keys,
56                                                          Iterator<V> values)
57      {
58          Map<K, V> m = null;
59  
60          if (mapClass == null)
61          {
62              throw new IllegalArgumentException("Map class must not be null!");
63          }
64  
65          try
66          {
67              m = mapClass.newInstance();
68          }
69          catch (Exception ex)
70          {
71              throw new RuntimeException(ex);
72          }
73  
74          if (keys != null && values != null)
75          {
76              while (keys.hasNext() && values.hasNext())
77              {
78                  m.put(keys.next(), values.next());
79              }
80          }
81  
82          return m;
83      }
84  
85      /**
86       * Creates a String representation of the given Map, with optional newlines
87       * between elements.
88       * 
89       * @param props the map to format
90       * @param newline indicates whether elements are to be split across lines
91       * @return the formatted String
92       */
93      public static String toString(Map props, boolean newline)
94      {
95          if (props == null || props.isEmpty())
96          {
97              return "{}";
98          }
99  
100         StringBuffer buf = new StringBuffer(props.size() * 32);
101         buf.append('{');
102 
103         if (newline)
104         {
105             buf.append(SystemUtils.LINE_SEPARATOR);
106         }
107 
108         Object[] entries = props.entrySet().toArray();
109         int i;
110 
111         for (i = 0; i < entries.length - 1; i++)
112         {
113             Map.Entry property = (Map.Entry) entries[i];
114             buf.append(property.getKey());
115             buf.append('=');
116             buf.append(PropertiesUtils.maskedPropertyValue(property));
117 
118             if (newline)
119             {
120                 buf.append(SystemUtils.LINE_SEPARATOR);
121             }
122             else
123             {
124                 buf.append(',').append(' ');
125             }
126         }
127 
128         // don't forget the last one
129         Map.Entry lastProperty = (Map.Entry) entries[i];
130         buf.append(lastProperty.getKey().toString());
131         buf.append('=');
132         buf.append(PropertiesUtils.maskedPropertyValue(lastProperty));
133 
134         if (newline)
135         {
136             buf.append(SystemUtils.LINE_SEPARATOR);
137         }
138 
139         buf.append('}');
140         return buf.toString();
141     }
142 
143 }