View Javadoc

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