KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > MapUtils


1 /*
2  * $Id: MapUtils.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.apache.commons.lang.SystemUtils;
20
21 // @ThreadSafe
22
public class MapUtils extends org.apache.commons.collections.MapUtils
23 {
24
25     /**
26      * Convenience method for CollectionUtil#mapWithKeysAndValues(Class, Iterator,
27      * Iterator); keys and values can be null or empty.
28      */

29     public static Map JavaDoc mapWithKeysAndValues(Class JavaDoc mapClass, Object JavaDoc[] keys, Object JavaDoc[] values)
30     {
31         Collection JavaDoc keyCollection = (keys != null ? Arrays.asList(keys) : Collections.EMPTY_LIST);
32         Collection JavaDoc 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 Map JavaDoc mapWithKeysAndValues(Class JavaDoc mapClass, Collection JavaDoc keys, Collection JavaDoc values)
41     {
42         keys = (keys != null ? keys : Collections.EMPTY_LIST);
43         values = (values != null ? values : Collections.EMPTY_LIST);
44         return mapWithKeysAndValues(mapClass, keys.iterator(), values.iterator());
45     }
46
47     /**
48      * Create & populate a Map of arbitrary class. Populating stops when either the
49      * keys or values iterator is null or exhausted.
50      *
51      * @param mapClass the Class of the Map to instantiate
52      * @param keys iterator for Objects ued as keys
53      * @param values iterator for Objects used as values
54      * @return the instantiated Map
55      */

56     public static Map JavaDoc mapWithKeysAndValues(Class JavaDoc mapClass, Iterator JavaDoc keys, Iterator JavaDoc values)
57     {
58         Map JavaDoc m = null;
59
60         if (mapClass == null)
61         {
62             throw new IllegalArgumentException JavaDoc("Map class must not be null!");
63         }
64
65         try
66         {
67             m = (Map JavaDoc)mapClass.newInstance();
68         }
69         catch (Exception JavaDoc ex)
70         {
71             throw new RuntimeException JavaDoc(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 JavaDoc toString(Map JavaDoc props, boolean newline)
94     {
95         if (props == null || props.isEmpty())
96         {
97             return "{}";
98         }
99
100         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(props.size() * 32);
101         buf.append('{');
102
103         if (newline)
104         {
105             buf.append(SystemUtils.LINE_SEPARATOR);
106         }
107
108         Object JavaDoc[] entries = props.entrySet().toArray();
109         int i;
110
111         for (i = 0; i < entries.length - 1; i++)
112         {
113             Map.Entry JavaDoc property = (Map.Entry JavaDoc)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 JavaDoc lastProperty = (Map.Entry JavaDoc)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 }
144
Popular Tags