litesoft
@ 939
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
package org.litesoft.commonfoundation.typeutils; import java.util.*; public class Maps { @SuppressWarnings("Convert2Diamond") public static <K, V> Map<K, V> newHashMap() { return new HashMap<K, V>(); } public static <K, V> Map<K, V> empty() { return Collections.emptyMap(); } public static <K, V> Map<K, V> copyHashMap( Map<K, V> pSource ) { if ( pSource == null || pSource.isEmpty() ) { return empty(); } Map<K, V> rv = newHashMap(); rv.putAll( pSource ); return rv; } public static <K, V> Map<K, V> deNull( Map<K, V> pToCheck ) { if ( pToCheck == null ) { pToCheck = empty(); } return pToCheck; } public static String[] stringMapToStringArray( Map<String, String> pMap ) { if ( pMap == null || pMap.isEmpty() ) { return Strings.EMPTY_ARRAY; } String[] rv = new String[pMap.size() * 2]; int i = 0; for ( Map.Entry<String, String> zEntry : pMap.entrySet() ) { rv[i++] = zEntry.getKey(); rv[i++] = zEntry.getValue(); } return rv; } public static Map<String, String> createHashMap( String... pKeyValuePairs ) throws IllegalArgumentException { Map<String, String> zMap = newHashMap(); return populate( zMap, pKeyValuePairs ); } public static Map<String, String> populate( Map<String, String> pToPopulate, String... pNameValuePairs ) throws IllegalArgumentException { if ( pNameValuePairs != null ) { if ( (pNameValuePairs.length & 1) == 1 ) { throw new IllegalArgumentException( "NameValuePairs not paired" ); } for ( int i = 0; i < pNameValuePairs.length; ) { String name = pNameValuePairs[i++]; String value = pNameValuePairs[i++]; // noinspection unchecked pToPopulate.put( name, value ); } } return pToPopulate; } public static void populate( Map pToPopulate, Map pNewContents ) { if ( pNewContents != null ) { //noinspection unchecked pToPopulate.putAll( pNewContents ); } } public static String[] getSortedKeysAsStrings( Map pMap ) { String[] names = Strings.toArray( pMap.keySet().toArray() ); Arrays.sort( names ); return names; } public static Map<String, String> addPropertiesTo( Map<String, String> pMap, String... pProperty_NameValues ) throws IllegalArgumentException { if ( Objects.isNotNullOrEmpty( pProperty_NameValues ) ) { if ( (pProperty_NameValues.length & 1) != 0 ) { throw new IllegalArgumentException( "Attempt to add Properties that were NOT Name/Value pairs" ); } for ( int i = 0; i < pProperty_NameValues.length; i += 2 ) { pMap.put( pProperty_NameValues[i], pProperty_NameValues[i + 1] ); } } return pMap; } } |