1 23 package com.sun.appserv.management.util.misc; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Properties ; 31 32 import com.sun.appserv.management.util.stringifier.SmartStringifier; 33 34 35 public final class MapUtil 36 { 37 private 38 MapUtil( ) 39 { 40 } 42 43 public static <K,V> V 44 getWithDefault( final Map <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 [] 51 getKeyObjects( final Map <?,?> m) 52 { 53 return( SetUtil.toArray( m.keySet() ) ); 54 } 55 56 public static String [] 57 getKeyStrings( final Map <?,?> m) 58 { 59 return( GSetUtil.toSortedStringArray( m.keySet() ) ); 60 } 61 62 65 public static <V> Map <String ,V> 66 newMap( 67 final String key, 68 final V value ) 69 { 70 final Map <String ,V> m = new HashMap <String ,V>(); 71 72 m.put( key, value ); 73 74 return( m ); 75 } 76 77 80 public static <K,V> Map <K,V> 81 newMap( 82 final Map <K,V> m1, 83 final Map <K,V> m2 ) 84 { 85 final Map <K,V> m = new HashMap <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 104 public static <T> Map <T,T> 105 newMap( final T[] mappings ) 106 { 107 if ( (mappings.length % 2) != 0 ) 108 { 109 throw new IllegalArgumentException ( "mappings must have even length" ); 110 } 111 112 final Map <T,T> m = new HashMap <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 127 public static Map <String ,String > 128 newMap( final String [] mappings ) 129 { 130 if ( (mappings.length % 2) != 0 ) 131 { 132 throw new IllegalArgumentException ( "mappings must have even length" ); 133 } 134 135 final Map <String ,String > m = new HashMap <String ,String >(); 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 149 public static <T> void 150 removeAll( 151 final Map <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 <?,?> m1, 163 final Map <?,?> 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 key : m1.keySet() ) 178 { 179 final Object value1 = m1.get( key ); 180 final Object 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 <K,V> 195 newMapNoNullValues( final Map <K,V> m ) 196 { 197 final Map <K,V> result = new HashMap <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 213 toString( final Map <?,?> m ) 214 { 215 return( toString( m, "," ) ); 216 } 217 218 public static String 219 toString( final Map <?,?> m, final String separator ) 220 { 221 if ( m == null ) 222 { 223 return( "null" ); 224 } 225 226 final StringBuffer buf = new StringBuffer (); 227 228 final String [] keyStrings = getKeyStrings( m ); 229 for( final String key : keyStrings ) 230 { 231 final Object 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 buf.setLength( buf.length() - separator.length() ); 242 } 243 244 return( buf.toString() ); 245 } 246 247 public static <K> Set <K> 248 getNullValueKeys( final Map <K,?> m) 249 { 250 final Set <K> s = new HashSet <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 <K,V> 264 toMap( final Properties props, final Class <K> kClass, final Class <V> vClass) 265 { 266 return TypeCast.checkMap( props, kClass, vClass ); 267 } 268 269 public static <K> void 270 removeAll( final Map <K,?> m, final Set <K> s ) 271 { 272 for( final K key : s ) 273 { 274 m.remove( key ); 275 } 276 } 277 278 281 public static boolean 282 isAllStrings( final Map <?,?> m ) 283 { 284 return m != null && CollectionUtil.isAllStrings( m.keySet() ) && 285 CollectionUtil.isAllStrings( m.values() ); 286 } 287 288 292 public static Map <String ,String > 293 toStringStringMap( final Map <?,?> m ) 294 { 295 if ( m == null ) 296 { 297 return null; 298 } 299 300 Map <String ,String > result = null; 301 302 if ( isAllStrings( m ) ) 303 { 304 result = TypeCast.asMap( m ); 305 } 306 else 307 { 308 result = new HashMap <String ,String >(); 309 310 for( final Object key : m.keySet() ) 311 { 312 final Object value = m.get( key ); 313 314 if ( (key instanceof String ) && (value instanceof String ) ) 315 { 316 result.put( (String )key, (String )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 |