KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > collection > 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.util.collection;
25
26 import java.util.AbstractCollection JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 //Bug 4677074 begin
29
import java.util.logging.Logger JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import com.sun.logging.LogDomains;
32 //Bug 4677074 end
33

34 public class ContainerHelper
35 {
36     ///////////////////////////////////////////////////////////////////////////////////////////////////////
37

38 //Bug 4677074 begin
39
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
40 //Bug 4677074 end
41
public static String JavaDoc[] toStringArray(AbstractCollection JavaDoc coll) throws ArrayStoreException JavaDoc
42     {
43         String JavaDoc ss[] = new String JavaDoc[0];
44
45         if(coll.size() > 0)
46             ss = (String JavaDoc[]) coll.toArray(ss);
47
48         return ss;
49     }
50
51     ///////////////////////////////////////////////////////////////////////////////////////////////////////
52

53     public static ArrayList JavaDoc toArrayList(String JavaDoc[] ss)
54     {
55         if(ss == null)
56             return new ArrayList JavaDoc();
57
58         ArrayList JavaDoc list = new ArrayList JavaDoc(ss.length);
59
60         for(int i = 0; i < ss.length; i++)
61             list.add(ss[i]);
62
63         return list;
64     }
65
66     ///////////////////////////////////////////////////////////////////////////////////////////////////////
67

68     public static String JavaDoc toOneString(String JavaDoc[] ss)
69     {
70         String JavaDoc s = new String JavaDoc();
71         
72         for(int i = 0; ss != null && i < ss.length; i++)
73         {
74             s += ss[i] + "\n";//NOI18N
75
}
76         return s;
77     }
78
79     ///////////////////////////////////////////////////////////////////////////////////////////////////////
80

81     public static void main(String JavaDoc[] args)
82     {
83         ArrayList JavaDoc L1 = new ArrayList JavaDoc();
84         ArrayList JavaDoc L2 = new ArrayList JavaDoc();
85
86         L1.add("Hello");//NOI18N
87
L1.add("World");//NOI18N
88
L1.add("!!!");//NOI18N
89
L2.add(new Integer JavaDoc(5));
90         L2.add(new Integer JavaDoc(7));
91
92         String JavaDoc ss[] = toStringArray(L1);
93
94         for(int i = 0; i < ss.length; i++)
95 //Bug 4677074 System.out.println("String #" + i + ": " + ss[i]);//NOI18N
96
//Bug 4677074 begin
97
_logger.log(Level.FINE,"String #" + i + ": " + ss[i]);
98 //Bug 4677074 end
99

100         // should throw
101
try
102         {
103             ss = toStringArray(L2);
104         }
105         catch(ArrayStoreException JavaDoc e)
106         {
107 //Bug 4677074 System.out.println("Caught an Exception, as expected: " + e);//NOI18N
108
//Bug 4677074 begin
109
_logger.log(Level.WARNING,"iplanet_util.arraystore_exception",e);
110         }
111     }
112 }
113
114
Popular Tags