01: /*
02: * BEGIN_HEADER - DO NOT EDIT
03: *
04: * The contents of this file are subject to the terms
05: * of the Common Development and Distribution License
06: * (the "License"). You may not use this file except
07: * in compliance with the License.
08: *
09: * You can obtain a copy of the license at
10: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
11: * See the License for the specific language governing
12: * permissions and limitations under the License.
13: *
14: * When distributing Covered Code, include this CDDL
15: * HEADER in each file and include the License file at
16: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
17: * If applicable add the following below this CDDL HEADER,
18: * with the fields enclosed by brackets "[]" replaced with
19: * your own identifying information: Portions Copyright
20: * [year] [name of copyright owner]
21: */
22:
23: /*
24: * @(#)GenericsSupport.java
25: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
26: *
27: * END_HEADER - DO NOT EDIT
28: */
29: package com.sun.jbi.ui.runtime;
30:
31: import java.util.ArrayList;
32: import java.util.Collection;
33: import java.util.List;
34:
35: /**
36: * @author graj
37: *
38: */
39: public class GenericsSupport {
40:
41: /**
42: *
43: */
44: public GenericsSupport() {
45: // TODO Auto-generated constructor stub
46: }
47:
48: /**
49: * We can now use this to produce lists of ints or Strings:
50: * List<Integer> ints = Lists.toList(1, 2, 3);
51: * List<String> names = Lists.toList("Gopalan", "Suresh", "Raj");
52: *
53: * @param array
54: * @return
55: */
56: public static <Type> List<Type> toList(Type... array) {
57: List<Type> list = new ArrayList<Type>();
58: for (Type arrayElement : array) {
59: list.add(arrayElement);
60: }
61: return list;
62: }
63:
64: /**
65: *
66: * @param collection
67: * @param componentType
68: * @return
69: */
70: @SuppressWarnings("unchecked")
71: static public <Type> Type[] toArray(Collection<Type> collection,
72: Class<Type> componentType) {
73: // unchecked cast
74: Type[] array = (Type[]) java.lang.reflect.Array.newInstance(
75: componentType, collection.size());
76: int index = 0;
77: for (Type value : collection) {
78: array[index++] = value;
79: }
80: return array;
81: }
82:
83: /**
84: * @param args
85: */
86: public static void main(String[] args) {
87: // TODO Auto-generated method stub
88:
89: }
90:
91: }
|