KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > helper > ListHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ListHelper.java 906 2006-07-21 12:31:35Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.helper;
26
27 import java.util.List JavaDoc;
28
29 /**
30  * This helper is used to do list modifications.
31  * @author Eduardo Studzinski Estima de Castro
32  * @author Gisele Pinheiro Souza
33  */

34 public final class ListHelper {
35
36     /**
37      * Helper should have a private constructor.
38      *
39      */

40     private ListHelper(){
41     }
42
43     /**
44      * Creates a new list with elements of a list, plus new values.
45      * @param <E> list type
46      * @param list current list
47      * @param values values to add
48      * @return new list with new values
49      * @throws InstantiationException reflection exception
50      * @throws IllegalAccessException reflection exception
51      */

52     @SuppressWarnings JavaDoc("unchecked")
53     public static <E> List JavaDoc<E> addValues(final List JavaDoc<E> list, final E[] values) throws InstantiationException JavaDoc,
54             IllegalAccessException JavaDoc {
55
56         if (list != null) {
57             //Creates a new instance using reflection
58
List JavaDoc<E> lstNew = list.getClass().newInstance();
59
60             // Adds values from current list
61
lstNew.addAll(list);
62
63             // Adds the new values
64
if (values != null){
65                 for (E eObj : values) {
66                     lstNew.add(eObj);
67                 }
68             }
69             return lstNew;
70         }
71         return null;
72     }
73
74     /**
75      * Converts an List that does not use generics to a List with generic type.
76      * @param <E> the list type;
77      * @param list the corrent list.
78      * @return the generic list.
79      * @throws InstantiationException reflection exception.
80      * @throws IllegalAccessException reflection exception.
81      */

82     @SuppressWarnings JavaDoc("unchecked")
83     public static <E> List JavaDoc<E> convertListType(final List JavaDoc list) throws InstantiationException JavaDoc,
84             IllegalAccessException JavaDoc {
85         //verifies if the list is null
86
if (list != null) {
87             //creates a new instance of the list
88
List JavaDoc<E> lstReturn = list.getClass().newInstance();
89             //copy to the list with defined type
90
if (list.size() > 0) {
91                 for (Object JavaDoc obj : list) {
92                    lstReturn.add((E) obj);
93                 }
94             }
95             return lstReturn;
96         }
97         return null;
98
99     }
100
101 }
102
Popular Tags