KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > tools > ArrayUtils


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.tools;
20
21 import java.lang.reflect.Array JavaDoc;
22
23
24 /**
25  * Collection of tools for the server
26  *
27  * @author Stefano Fornari
28  * @version $Id: ArrayUtils.java,v 1.5 2005/03/02 20:57:38 harrie Exp $
29  */

30 public class ArrayUtils {
31     
32     /**
33      * Merges the two given arrays in a single array, putting the elements of
34      * the former array first and the elements of the latter array. The returned
35      * array will be of the type specified by <i>elementClass</i>
36      *
37      * @param a the former array - NULL
38      * @param b the latter array - NULL
39      * @param elementType the class of the elements
40      *
41      * @return an array containing the elements of <i>a</i> and <i>b</i>
42      */

43     public static Object JavaDoc mergeArrays(Object JavaDoc[] a, Object JavaDoc[] b, Class JavaDoc elementType) {
44         
45         int size = 0;
46         
47         if (a != null) size += a.length;
48         if (b != null) size += b.length;
49
50         Object JavaDoc newArray = Array.newInstance(elementType, size);
51         
52         int newArrayPos = 0;
53         if (a != null) {
54             System.arraycopy(a, 0, newArray, newArrayPos, a.length);
55             newArrayPos = a.length;
56         }
57          
58         if (b != null) {
59             System.arraycopy(b, 0, newArray, newArrayPos, b.length);
60         }
61         
62         return newArray;
63
64     }
65     
66 }
Popular Tags