1 /* -------------------------------------------------------------------
2 * Java source file for the class ArrayUtil
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:47 $
11 *
12 * $Id: ArrayUtil.java,v 1.1.1.1 2003/07/25 04:51:47 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.util;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.lang.reflect.*;
27 import java.util.*;
28
29 /*====================================================================
30 Implementation of class ArrayUtil
31 ====================================================================*/
32 /***
33 * ArrayUtil provides various utility methods.
34 *
35 * @version $Revision: 1.1.1.1 $
36 * @author Masahiro Takatsuka (masa@jbeans.net)
37 */
38
39 public final class ArrayUtil {
40 private ArrayUtil (){
41 super();
42 }
43
44 public final static Object createArray(Class type, int length) {
45 return java.lang.reflect.Array.newInstance(type, length);
46 }
47
48 public final static Object[] copyArray(Object[] from) {
49 Object[] to = (Object[]) from.clone();
50 System.arraycopy(from, 0, to, 0, from.length);
51
52 return to;
53 }
54
55 public final static boolean isArray(Object obj) {
56 return obj.getClass().isArray();
57 }
58
59 public final static Object copyArray(Object from) {
60 Class arrayClass = from.getClass();
61 if (!arrayClass.isArray()) {
62 throw new IllegalArgumentException("The argument must be an array.");
63 }
64
65 /*
66 * I cannot use .clone() of Object.class becase it's protected.
67 * Therefore, I have to create array from scrach.
68 * Masahiro Takatsuka/2000-Nov-09
69 */
70 Class type = arrayClass.getComponentType();
71 int size = Array.getLength(from);
72 Object to = createArray(type, size);
73 System.arraycopy(from, 0, to, 0, size);
74
75 return to;
76 }
77
78 public final static boolean equals(Object array1, Object array2) {
79 Class array1Class = array1.getClass();
80 Class array2Class = array2.getClass();
81 if (!array1Class.isArray() || !array2Class.isArray()) {
82 throw new IllegalArgumentException("The argument must be an array.");
83 }
84 Class type1 = array1Class.getComponentType();
85 Class type2 = array2Class.getComponentType();
86 if (type1 != type2) {
87 throw new IllegalArgumentException("The component type must be the same.");
88 }
89 int size1 = Array.getLength(array1);
90 int size2 = Array.getLength(array2);
91 if (size1 != size2) {
92 throw new IllegalArgumentException("The array sizes must be the same.");
93 }
94 Object comp1;
95 Object comp2;
96 boolean result = true;
97 for (int i = 0; i < size1; i++) {
98 comp1 = Array.get(array1, i);
99 comp2 = Array.get(array2, i);
100 if (( isArray(comp1) && !isArray(comp2)) ||
101 (!isArray(comp1) && isArray(comp2))) {
102 throw new IllegalArgumentException("The array structures must be the same.");
103 }
104 if (isArray(comp1)) {
105 result = equals(comp1, comp2);
106 } else {
107 result = comp1.equals(comp2);
108 }
109 }
110 return result;
111 }
112
113 // keep it with package scope for the inner class' sake
114 public static String addWithUniqueName(java.util.List list, String s) {
115 if (! list.contains(s)) {
116 list.add(s);
117 return s;
118 }
119 int count = 2;
120 String name = s;
121 while (list.contains(name)) {
122 count += 1;
123 name = s + "_" + count;
124 }
125 list.add(name);
126 return name;
127 }
128
129 // keep it with package scope for the inner class' sake
130 public static String addWithUniqueName(Map map, Object obj, String s) {
131 if (! map.containsKey(s)) {
132 map.put(s, obj);
133 return s;
134 }
135 int count = 1;
136 String name = s;
137 while (map.containsKey(name)) {
138 count += 1;
139 name = s + "_" + count;
140 }
141 map.put(name, obj);
142 return name;
143 }
144 }// ArrayUtil
This page was automatically generated by Maven