KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > util > ContainerHelper


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 package com.sun.enterprise.tools.common.util;
25
26 import java.util.AbstractCollection JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 public class ContainerHelper
30 {
31     ///////////////////////////////////////////////////////////////////////////////////////////////////////
32

33     public static String JavaDoc[] toStringArray(AbstractCollection JavaDoc coll) throws ArrayStoreException JavaDoc
34     {
35         String JavaDoc ss[] = new String JavaDoc[0];
36
37         if(coll.size() > 0)
38             ss = (String JavaDoc[]) coll.toArray(ss);
39
40         return ss;
41     }
42
43     ///////////////////////////////////////////////////////////////////////////////////////////////////////
44

45     public static ArrayList JavaDoc toArrayList(String JavaDoc[] ss)
46     {
47         if(ss == null)
48             return new ArrayList JavaDoc();
49
50         ArrayList JavaDoc list = new ArrayList JavaDoc(ss.length);
51
52         for(int i = 0; i < ss.length; i++)
53             list.add(ss[i]);
54
55         return list;
56     }
57
58     ///////////////////////////////////////////////////////////////////////////////////////////////////////
59

60     public static String JavaDoc toOneString(String JavaDoc[] ss)
61     {
62         String JavaDoc s = new String JavaDoc();
63         
64         for(int i = 0; ss != null && i < ss.length; i++)
65         {
66             s += ss[i] + "\n";//NOI18N
67
}
68         return s;
69     }
70
71     ///////////////////////////////////////////////////////////////////////////////////////////////////////
72

73     public static void main(String JavaDoc[] args)
74     {
75         ArrayList JavaDoc L1 = new ArrayList JavaDoc();
76         ArrayList JavaDoc L2 = new ArrayList JavaDoc();
77
78         L1.add("Hello");//NOI18N
79
L1.add("World");//NOI18N
80
L1.add("!!!");//NOI18N
81
L2.add(new Integer JavaDoc(5));
82         L2.add(new Integer JavaDoc(7));
83
84         String JavaDoc ss[] = toStringArray(L1);
85
86         for(int i = 0; i < ss.length; i++)
87             System.out.println("String #" + i + ": " + ss[i]);//NOI18N
88

89         // should throw
90
try
91         {
92             ss = toStringArray(L2);
93         }
94         catch(ArrayStoreException JavaDoc e)
95         {
96             System.out.println("Caught an Exception, as expected: " + e);//NOI18N
97
}
98     }
99 }
100
101
Popular Tags