KickJava   Java API By Example, From Geeks To Geeks.

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


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  
24 /*
25  */

26  
27 package com.sun.appserv.management.util.misc;
28
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34
35 public final class ListUtil
36 {
37         private
38     ListUtil( )
39     {
40         // disallow instantiation
41
}
42     
43     /**
44         Add all items in an array to a list.
45      */

46         public static <T> void
47     addArray(
48         final List JavaDoc<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     /**
58         Convert a List to a String[]
59      */

60         public static String JavaDoc[]
61     toStringArray( final List JavaDoc<?> list )
62     {
63         final String JavaDoc[] names = new String JavaDoc[ list.size() ];
64         
65         int i = 0;
66         for( final Object JavaDoc o : list )
67         {
68             names[ i ] = "" + o;
69             ++i;
70         }
71         
72         return( names );
73     }
74     
75     /**
76         Create a new List from a Collection
77      */

78         public static <T> List JavaDoc<T>
79     newListFromCollection( final Collection JavaDoc<T> c )
80     {
81         final List JavaDoc<T> list = new ArrayList JavaDoc<T>();
82         
83         list.addAll( c );
84         
85         return( list );
86     }
87     
88     /**
89         Create a new List from a Collection
90      */

91         public static <T> List JavaDoc<? extends T>
92     newListFromIterator( final Iterator JavaDoc<? extends T> iter )
93     {
94         final List JavaDoc<T> list = new ArrayList JavaDoc<T>();
95         
96         while ( iter.hasNext() )
97         {
98             list.add( iter.next() );
99         }
100         
101         return( list );
102     }
103     
104     /**
105         Create a new List with one member.
106      */

107         public static <T> List JavaDoc<T>
108     newList( T m1 )
109     {
110         final List JavaDoc<T> list = new ArrayList JavaDoc<T>();
111         
112         list.add( m1 );
113         
114         return( list );
115     }
116     
117     /**
118         Create a new List with two members.
119      */

120         public static <T> List JavaDoc<T>
121     newList(
122         final T m1,
123         final T m2 )
124     {
125         final List JavaDoc<T> list = new ArrayList JavaDoc<T>();
126         
127         list.add( m1 );
128         list.add( m2 );
129         
130         return( list );
131     }
132     
133     
134     /**
135         Create a new List with three members.
136      */

137         public static <T> List JavaDoc<T>
138     newList(
139         final T m1,
140         final T m2,
141         final T m3 )
142     {
143         final List JavaDoc<T> list = new ArrayList JavaDoc<T>();
144         
145         list.add( m1 );
146         list.add( m2 );
147         list.add( m3 );
148         
149         return( list );
150     }
151     
152     /**
153         Create a new List with four members.
154      */

155         public static <T> List JavaDoc<T>
156     newList(
157         final T m1,
158         final T m2,
159         final T m3,
160         final T m4 )
161     {
162         final List JavaDoc<T> list = new ArrayList JavaDoc<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     /**
173         Create a new List with four members.
174      */

175         public static <T> List JavaDoc<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 JavaDoc<T> list = new ArrayList JavaDoc<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 JavaDoc<T>
197     newListFromArray( final T [] items )
198     {
199         final List JavaDoc<T> list = new ArrayList JavaDoc<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     /**
210         Return a new List in reverse order. Because the List is new,
211         it works on any list, modifiable or not.
212      */

213         public static <T> List JavaDoc<T>
214     reverse( final List JavaDoc<T> list )
215     {
216         final int numItems = list.size();
217         final List JavaDoc<T> result = new ArrayList JavaDoc<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