litesoft
@ 943
litesoft / trunk / DeviceDesktopTest / src / org / litesoft / commonfoundation / typeutils / Lists.java
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
package org.litesoft.commonfoundation.typeutils; import sun.awt.util.*; import java.util.*; @SuppressWarnings("Convert2Diamond") public class Lists { public interface Provider<T> { List<T> get(); } public static <T> IdentityArrayList<T> newIdentityArrayList() { return new IdentityArrayList<T>(); } public static <T> ArrayList<T> newArrayList() { return new ArrayList<T>(); } @SuppressWarnings("unchecked") public static <T> ArrayList<T> newArrayList( T... pEntries ) { ArrayList<T> zList = newArrayList(); append( zList, pEntries ); return zList; } public static <T> ArrayList<T> newArrayList( Collection<T> pEntries ) { ArrayList<T> zList = newArrayList(); if ( pEntries != null ) { zList.addAll( pEntries ); } return zList; } public static <T> ArrayList<T> newArrayList( int zInitialSize ) { return new ArrayList<T>( Math.max( 1, zInitialSize ) ); } public static <T> List<T> newArrayList( List<T> pList1, List<T> pList2 ) { ArrayList<T> zMerged = newArrayList( deNull( pList1 ) ); zMerged.addAll( deNull( pList2 ) ); return zMerged; } public static <T> List<T> empty() { return Collections.emptyList(); } public static <T> List<T> deNull( List<T> pToCheck ) { if ( pToCheck == null ) { pToCheck = empty(); } return pToCheck; } public static <T> List<T> deNullMutable( List<T> pToCheck ) { if ( pToCheck == null ) { pToCheck = newArrayList(); } return pToCheck; } public static <T> List<T> deNullUnmodifiable( List<T> pToCheck ) { if ( pToCheck == null ) { return empty(); } return Collections.unmodifiableList( pToCheck ); } public static <T> List<T> deNullImmutable( List<T> pToCheck ) { if ( pToCheck == null ) { return empty(); } return Collections.unmodifiableList( newArrayList( pToCheck ) ); } public static boolean isNullOrEmpty( List<?> pToCheck ) { return (pToCheck == null || pToCheck.isEmpty()); } public static boolean isNotNullOrEmpty( List<?> pToCheck ) { return (pToCheck != null && !pToCheck.isEmpty()); } public static void assertNotNullNotEmpty( String pErrorMessage, List<?> pToAssert ) throws IllegalArgumentException { if ( isNullOrEmpty( pToAssert ) ) { Strings.errorNullOrEmpty( pErrorMessage, "Collection" ); } } public static <T> List<T> of( T pEntry ) { ArrayList<T> rv = newArrayList(); rv.add( pEntry ); return rv; } public static <T> List<T> from( Collection<T> pCollection ) { ArrayList<T> rv = newArrayList(); if ( pCollection != null ) { rv.addAll( pCollection ); } return rv; } public static <T> List<T> from( Enumeration<T> pEnumeration ) { ArrayList<T> rv = newArrayList(); while ( pEnumeration.hasMoreElements() ) { rv.add( pEnumeration.nextElement() ); } return rv; } public static <T> List<T> ofUnmodifiable( T pEntry ) { return deNullUnmodifiable( of( pEntry ) ); } public static <T> List<T> fromUnmodifiable( Collection<T> pCollection ) { return deNullUnmodifiable( from( pCollection ) ); } public static <T> List<T> fromUnmodifiable( Enumeration<T> pEnumeration ) { return deNullUnmodifiable( from( pEnumeration ) ); } @SuppressWarnings("unchecked") public static <T> List<T> append( List<T> pBase, T... pAdditionalEntries ) { if ( pAdditionalEntries != null ) { Collections.addAll( pBase, pAdditionalEntries ); } return pBase; } public static <T> List<T> append( List<T> pBase, List<T> pAdditionalEntries ) { pBase = deNullMutable( pBase ); pBase.addAll( deNullMutable( pAdditionalEntries ) ); return pBase; } public static String singleQuoteToString( List<String> pStrings ) { StringBuilder sb = new StringBuilder(); if ( pStrings != null ) { char prefix = '['; if ( pStrings.size() == 0 ) { sb.append( prefix ); } else { for ( String zString : pStrings ) { sb.append( prefix ); sb.append( '\'' ); sb.append( zString ); sb.append( '\'' ); prefix = ','; } } sb.append( ']' ); } return sb.toString(); } } |