View Javadoc

1   /*
2    * $Id: MapUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 Map mapWithKeysAndValues(Class mapClass, Object[] keys, Object[] values)
28      {
29          Collection keyCollection = (keys != null ? Arrays.asList(keys) : Collections.EMPTY_LIST);
30          Collection valuesCollection = (values != null ? Arrays.asList(values) : Collections.EMPTY_LIST);
31          return mapWithKeysAndValues(mapClass, keyCollection.iterator(), valuesCollection.iterator());
32      }
33  
34      /**
35       * Convenience method for CollectionUtil#mapWithKeysAndValues(Class, Iterator,
36       * Iterator); keys and values can be null or empty.
37       */
38      public static Map mapWithKeysAndValues(Class mapClass, Collection keys, Collection 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 Map mapWithKeysAndValues(Class mapClass, Iterator keys, Iterator values)
55      {
56          Map m = null;
57  
58          if (mapClass == null)
59          {
60              throw new IllegalArgumentException("Map class must not be null!");
61          }
62  
63          try
64          {
65              m = (Map) mapClass.newInstance();
66          }
67          catch (Exception ex)
68          {
69              throw new RuntimeException(ex);
70          }
71  
72          if (keys != null && values != null)
73          {
74              while (keys.hasNext() && values.hasNext())
75              {
76                  m.put(keys.next(), values.next());
77              }
78          }
79  
80          return m;
81      }
82  
83      /**
84       * Creates a String representation of the given Map, with optional newlines
85       * between elements.
86       * 
87       * @param props the map to format
88       * @param newline indicates whether elements are to be split across lines
89       * @return the formatted String
90       */
91      public static String toString(Map props, boolean newline)
92      {
93          if (props == null || props.isEmpty())
94          {
95              return "{}";
96          }
97  
98          StringBuffer buf = new StringBuffer(props.size() * 32);
99          buf.append('{');
100 
101         if (newline)
102         {
103             buf.append(SystemUtils.LINE_SEPARATOR);
104         }
105 
106         Object[] entries = props.entrySet().toArray();
107         int i;
108 
109         for (i = 0; i < entries.length - 1; i++)
110         {
111             Map.Entry property = (Map.Entry) entries[i];
112             buf.append(property.getKey());
113             buf.append('=');
114             buf.append(PropertiesUtils.maskedPropertyValue(property));
115 
116             if (newline)
117             {
118                 buf.append(SystemUtils.LINE_SEPARATOR);
119             }
120             else
121             {
122                 buf.append(',').append(' ');
123             }
124         }
125 
126         // don't forget the last one
127         Map.Entry lastProperty = (Map.Entry) entries[i];
128         buf.append(lastProperty.getKey().toString());
129         buf.append('=');
130         buf.append(PropertiesUtils.maskedPropertyValue(lastProperty));
131 
132         if (newline)
133         {
134             buf.append(SystemUtils.LINE_SEPARATOR);
135         }
136 
137         buf.append('}');
138         return buf.toString();
139     }
140 
141 }