/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

  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. package com.bluemarsh.jswat.core.util;
  24. import java.lang.reflect.Array;
  25. /**
  26. * Utility methods for arrays.
  27. *
  28. * @author Nathan Fiedler
  29. */
  30. public class Arrays {
  31. /**
  32. * Creates a new instance of Arrays.
  33. */
  34. private Arrays() {
  35. }
  36. /**
  37. * Joins two arrays of objects of the same type. If one is null, the other
  38. * is returned. If both are null, null is returned. Otherwise, a new array
  39. * of size equal to the length of both arrays is returned, with the elements
  40. * of arr1 appearing before the elements of arr2.
  41. *
  42. * @param arr1 first array.
  43. * @param arr2 second array.
  44. * @return joined arrays, or null if both arrays are null.
  45. */
  46. public static Object[] join(Object[] arr1, Object[] arr2) {
  47. if (arr1 == null && arr2 != null) {
  48. return arr2;
  49. } else if (arr2 == null) {
  50. return arr1;
  51. } else {
  52. int size = arr1.length + arr2.length;
  53. Object[] arr = (Object[]) Array.newInstance(
  54. arr1.getClass().getComponentType(), size);
  55. System.arraycopy(arr1, 0, arr, 0, arr1.length);
  56. System.arraycopy(arr2, 0, arr, arr1.length, arr2.length);
  57. return arr;
  58. }
  59. }
  60. }