1 23 24 26 27 package com.sun.appserv.management.util.misc; 28 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 34 35 public final class ListUtil 36 { 37 private 38 ListUtil( ) 39 { 40 } 42 43 46 public static <T> void 47 addArray( 48 final List <T> list, 49 final T[] array ) 50 { 51 for( int i = 0; i < array.length; ++i ) 52 { 53 list.add( array[ i ] ); 54 } 55 } 56 57 60 public static String [] 61 toStringArray( final List <?> list ) 62 { 63 final String [] names = new String [ list.size() ]; 64 65 int i = 0; 66 for( final Object o : list ) 67 { 68 names[ i ] = "" + o; 69 ++i; 70 } 71 72 return( names ); 73 } 74 75 78 public static <T> List <T> 79 newListFromCollection( final Collection <T> c ) 80 { 81 final List <T> list = new ArrayList <T>(); 82 83 list.addAll( c ); 84 85 return( list ); 86 } 87 88 91 public static <T> List <? extends T> 92 newListFromIterator( final Iterator <? extends T> iter ) 93 { 94 final List <T> list = new ArrayList <T>(); 95 96 while ( iter.hasNext() ) 97 { 98 list.add( iter.next() ); 99 } 100 101 return( list ); 102 } 103 104 107 public static <T> List <T> 108 newList( T m1 ) 109 { 110 final List <T> list = new ArrayList <T>(); 111 112 list.add( m1 ); 113 114 return( list ); 115 } 116 117 120 public static <T> List <T> 121 newList( 122 final T m1, 123 final T m2 ) 124 { 125 final List <T> list = new ArrayList <T>(); 126 127 list.add( m1 ); 128 list.add( m2 ); 129 130 return( list ); 131 } 132 133 134 137 public static <T> List <T> 138 newList( 139 final T m1, 140 final T m2, 141 final T m3 ) 142 { 143 final List <T> list = new ArrayList <T>(); 144 145 list.add( m1 ); 146 list.add( m2 ); 147 list.add( m3 ); 148 149 return( list ); 150 } 151 152 155 public static <T> List <T> 156 newList( 157 final T m1, 158 final T m2, 159 final T m3, 160 final T m4 ) 161 { 162 final List <T> list = new ArrayList <T> (); 163 164 list.add( m1 ); 165 list.add( m2 ); 166 list.add( m3 ); 167 list.add( m4 ); 168 169 return( list ); 170 } 171 172 175 public static <T> List <T> 176 newList( 177 final T m1, 178 final T m2, 179 final T m3, 180 final T m4, 181 final T m5 ) 182 { 183 final List <T> list = new ArrayList <T> (); 184 185 list.add( m1 ); 186 list.add( m2 ); 187 list.add( m3 ); 188 list.add( m4 ); 189 list.add( m5 ); 190 191 return( list ); 192 } 193 194 195 196 public static <T> List <T> 197 newListFromArray( final T [] items ) 198 { 199 final List <T> list = new ArrayList <T>(); 200 201 for( int i = 0; i < items.length; ++i ) 202 { 203 list.add( items[ i ] ); 204 } 205 206 return( list ); 207 } 208 209 213 public static <T> List <T> 214 reverse( final List <T> list ) 215 { 216 final int numItems = list.size(); 217 final List <T> result = new ArrayList <T>( numItems ); 218 219 for( int i = 0; i < numItems; ++i ) 220 { 221 result.add( list.get( numItems - i -1 ) ); 222 } 223 224 return( result ); 225 } 226 227 } 228 229 | Popular Tags |