PageRenderTime 19ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/com/bluemarsh/jswat/core/util/Arrays.java

http://jswat.googlecode.com/
Java | 65 lines | 20 code | 5 blank | 40 comment | 8 complexity | 5565d16fa6537cdcb1c312a5184a2d1b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
 1/*
 2 * The contents of this file are subject to the terms of the Common Development
 3 * and Distribution License (the License). You may not use this file except in
 4 * compliance with the License.
 5 *
 6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
 7 * or http://www.netbeans.org/cddl.txt.
 8 *
 9 * When distributing Covered Code, include this CDDL Header Notice in each file
10 * and include the License file at http://www.netbeans.org/cddl.txt.
11 * If applicable, add the following below the CDDL Header, with the fields
12 * enclosed by brackets [] replaced by your own identifying information:
13 * "Portions Copyrighted [year] [name of copyright owner]"
14 *
15 * The Original Software is JSwat. The Initial Developer of the Original
16 * Software is Nathan L. Fiedler. Portions created by Nathan L. Fiedler
17 * are Copyright (C) 2004-2005. All Rights Reserved.
18 *
19 * Contributor(s): Nathan L. Fiedler.
20 *
21 * $Id: Arrays.java 40 2009-01-09 07:35:28Z nathanfiedler $
22 */
23
24package com.bluemarsh.jswat.core.util;
25
26import java.lang.reflect.Array;
27
28/**
29 * Utility methods for arrays.
30 *
31 * @author Nathan Fiedler
32 */
33public class Arrays {
34
35    /**
36     * Creates a new instance of Arrays.
37     */
38    private Arrays() {
39    }
40
41    /**
42     * Joins two arrays of objects of the same type. If one is null, the other
43     * is returned. If both are null, null is returned. Otherwise, a new array
44     * of size equal to the length of both arrays is returned, with the elements
45     * of arr1 appearing before the elements of arr2.
46     *
47     * @param  arr1  first array.
48     * @param  arr2  second array.
49     * @return  joined arrays, or null if both arrays are null.
50     */
51    public static Object[] join(Object[] arr1, Object[] arr2) {
52        if (arr1 == null && arr2 != null) {
53            return arr2;
54        } else if (arr2 == null) {
55            return arr1;
56        } else {
57            int size = arr1.length + arr2.length;
58            Object[] arr = (Object[]) Array.newInstance(
59                    arr1.getClass().getComponentType(), size);
60            System.arraycopy(arr1, 0, arr, 0, arr1.length);
61            System.arraycopy(arr2, 0, arr, arr1.length, arr2.length);
62            return arr;
63        }
64    }
65}