KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > MapUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.misc;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import com.sun.appserv.management.util.stringifier.SmartStringifier;
33
34
35 public final class MapUtil
36 {
37         private
38     MapUtil( )
39     {
40         // disallow instantiation
41
}
42     
43         public static <K,V> V
44     getWithDefault( final Map JavaDoc<K,V> m, final K key, V defaultValue )
45     {
46         return m.containsKey( key ) ? m.get( key ) : defaultValue;
47     }
48     
49     
50         public static Object JavaDoc[]
51     getKeyObjects( final Map JavaDoc<?,?> m)
52     {
53         return( SetUtil.toArray( m.keySet() ) );
54     }
55     
56         public static String JavaDoc[]
57     getKeyStrings( final Map JavaDoc<?,?> m)
58     {
59         return( GSetUtil.toSortedStringArray( m.keySet() ) );
60     }
61     
62     /**
63         Create a new Map consisting of a single key/value pair.
64      */

65         public static <V> Map JavaDoc<String JavaDoc,V>
66     newMap(
67         final String JavaDoc key,
68         final V value )
69     {
70         final Map JavaDoc<String JavaDoc,V> m = new HashMap JavaDoc<String JavaDoc,V>();
71         
72         m.put( key, value );
73         
74         return( m );
75     }
76     
77     /**
78         Create a new Map consisting of a single key/value pair.
79      */

80         public static <K,V> Map JavaDoc<K,V>
81     newMap(
82         final Map JavaDoc<K,V> m1,
83         final Map JavaDoc<K,V> m2 )
84     {
85         final Map JavaDoc<K,V> m = new HashMap JavaDoc<K,V>();
86         
87         if ( m1 != null )
88         {
89             m.putAll( m1 );
90         }
91         if ( m2 != null )
92         {
93             m.putAll( m2 );
94         }
95         
96         return( m );
97     }
98     
99     /**
100         Create a new Map and insert the specified mappings as found in 'mappings'.
101         The even-numbered entries are the keys, and the odd-numbered entries are
102         the values.
103      */

104         public static <T> Map JavaDoc<T,T>
105     newMap( final T[] mappings )
106     {
107         if ( (mappings.length % 2) != 0 )
108         {
109             throw new IllegalArgumentException JavaDoc( "mappings must have even length" );
110         }
111         
112         final Map JavaDoc<T,T> m = new HashMap JavaDoc<T,T>();
113         
114         for( int i = 0; i < mappings.length; i += 2 )
115         {
116             m.put( mappings[ i ], mappings[ i + 1 ] );
117         }
118         
119         return( m );
120     }
121     
122     /**
123         Create a new Map and insert the specified mappings as found in 'mappings'.
124         The even-numbered entries are the keys, and the odd-numbered entries are
125         the values.
126      */

127         public static Map JavaDoc<String JavaDoc,String JavaDoc>
128     newMap( final String JavaDoc[] mappings )
129     {
130         if ( (mappings.length % 2) != 0 )
131         {
132             throw new IllegalArgumentException JavaDoc( "mappings must have even length" );
133         }
134         
135         final Map JavaDoc<String JavaDoc,String JavaDoc> m = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
136         
137         for( int i = 0; i < mappings.length; i += 2 )
138         {
139             m.put( mappings[ i ], mappings[ i + 1 ] );
140         }
141         
142         return( m );
143     }
144     
145     
146     /**
147         Remove all entries keyed by 'keys'
148      */

149         public static <T> void
150     removeAll(
151         final Map JavaDoc<T,?> m,
152         final T[] keys )
153     {
154         for( int i = 0; i < keys.length; ++i )
155         {
156             m.remove( keys[ i ] );
157         }
158     }
159     
160         public static boolean
161     mapsEqual(
162         final Map JavaDoc<?,?> m1,
163         final Map JavaDoc<?,?> m2 )
164     {
165         if ( m1 == m2 )
166         {
167             return( true );
168         }
169         
170         boolean equal = false;
171         
172         if ( m1.size() == m2.size() &&
173             m1.keySet().equals( m2.keySet() ) )
174         {
175             equal = true;
176             
177             for( final Object JavaDoc key : m1.keySet() )
178             {
179                 final Object JavaDoc value1 = m1.get( key );
180                 final Object JavaDoc value2 = m2.get( key );
181                 
182                 if ( ! CompareUtil.objectsEqual( value1, value2 ) )
183                 {
184                     equal = false;
185                     break;
186                 }
187             }
188         }
189         
190         return( equal );
191     }
192     
193     
194         public static <K,V> Map JavaDoc<K,V>
195     newMapNoNullValues( final Map JavaDoc<K,V> m )
196     {
197         final Map JavaDoc<K,V> result = new HashMap JavaDoc<K,V>();
198         
199         for ( final K key : m.keySet() )
200         {
201             final V value = m.get( key );
202             
203             if ( value != null )
204             {
205                 result.put( key, value );
206             }
207         }
208         
209         return( result );
210     }
211     
212         public static String JavaDoc
213     toString( final Map JavaDoc<?,?> m )
214     {
215         return( toString( m, "," ) );
216     }
217     
218         public static String JavaDoc
219     toString( final Map JavaDoc<?,?> m, final String JavaDoc separator )
220     {
221         if ( m == null )
222         {
223             return( "null" );
224         }
225         
226         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
227         
228         final String JavaDoc[] keyStrings = getKeyStrings( m );
229         for( final String JavaDoc key : keyStrings )
230         {
231             final Object JavaDoc value = m.get( key );
232             
233             buf.append( key );
234             buf.append( "=" );
235             buf.append( SmartStringifier.toString( value ) );
236             buf.append( separator );
237         }
238         if ( buf.length() != 0 )
239         {
240             // strip trailing separator
241
buf.setLength( buf.length() - separator.length() );
242         }
243         
244         return( buf.toString() );
245     }
246     
247         public static <K> Set JavaDoc<K>
248     getNullValueKeys( final Map JavaDoc<K,?> m)
249     {
250         final Set JavaDoc<K> s = new HashSet JavaDoc<K>();
251         
252         for( final K key : m.keySet() )
253         {
254             if ( m.get( key ) == null )
255             {
256                 s.add( key );
257             }
258         }
259         return( s );
260     }
261     
262     
263         public static <K,V> Map JavaDoc<K,V>
264     toMap( final Properties JavaDoc props, final Class JavaDoc<K> kClass, final Class JavaDoc<V> vClass)
265     {
266         return TypeCast.checkMap( props, kClass, vClass );
267     }
268     
269         public static <K> void
270     removeAll( final Map JavaDoc<K,?> m, final Set JavaDoc<K> s )
271     {
272         for( final K key : s )
273         {
274             m.remove( key );
275         }
276     }
277     
278     /**
279         @return true if non-null Map and all keys and values are of type java.lang.String
280      */

281         public static boolean
282     isAllStrings( final Map JavaDoc<?,?> m )
283     {
284         return m != null && CollectionUtil.isAllStrings( m.keySet() ) &&
285                 CollectionUtil.isAllStrings( m.values() );
286     }
287     
288     /**
289         Convert an arbitrary Map to one whose keys and values
290         are both of type String.
291      */

292         public static Map JavaDoc<String JavaDoc,String JavaDoc>
293     toStringStringMap( final Map JavaDoc<?,?> m )
294     {
295         if ( m == null )
296         {
297             return null;
298         }
299         
300         Map JavaDoc<String JavaDoc,String JavaDoc> result = null;
301         
302         if ( isAllStrings( m ) )
303         {
304             result = TypeCast.asMap( m );
305         }
306         else
307         {
308             result = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
309             
310             for( final Object JavaDoc key : m.keySet() )
311             {
312                 final Object JavaDoc value = m.get( key );
313                 
314                 if ( (key instanceof String JavaDoc) && (value instanceof String JavaDoc) )
315                 {
316                     result.put( (String JavaDoc)key, (String JavaDoc)value );
317                 }
318                 else
319                 {
320                     result.put( "" + key, "" + value );
321                 }
322             }
323         }
324         
325         return result;
326     }
327 }
328
329
330
331
332
333     
334
335
336
337
338
339
Popular Tags