1 /*
2 * #%L
3 * Nuiton Utils
4 * %%
5 * Copyright (C) 2004 - 2010 CodeLutin
6 * %%
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Lesser Public License for more details.
16 *
17 * You should have received a copy of the GNU General Lesser Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20 * #L%
21 */
22
23
24 /**
25 * ArrayUtil.java
26 * <p>
27 * Created: 31 oct. 2004
28 *
29 * @author Benjamin Poussin - poussin@codelutin.com
30 */
31
32 package org.nuiton.util;
33
34 import java.lang.reflect.Array;
35 import java.util.Collection;
36
37 public class ArrayUtil { // ArrayUtil
38
39 public static int[] asIntArray(String[] a) {
40 int[] result = new int[a.length];
41 for (int i = 0; i < a.length; i++) {
42 result[i] = StringUtil.toInt(a[i]);
43 }
44 return result;
45 }
46
47
48 /**
49 * Fait la somme des 2 tableaux et retourne un nouveau tableau, les
50 * 2 tableaux passés en argument ne sont pas modifiés. Les deux tableaux
51 * doivent être non null et avoir la même taille.
52 *
53 * @param a le premier tableau
54 * @param b le second tableau
55 * @return le tableau des sommes
56 */
57 public static int[] sum(int[] a, int[] b) {
58 if (a == null || b == null || a.length != b.length) {
59 throw new IllegalArgumentException("Au moins des tableaux est null ou les tableaux ne font pas la même taille");
60 }
61 int[] result = new int[a.length];
62 for (int i = 0; i < a.length; i++) {
63 result[i] = a[i] + b[i];
64 }
65 return result;
66 }
67
68 public static int[] concat(int[]... tabs) {
69 int length = 0;
70 for (int[] tab : tabs) {
71 if (tab != null) {
72 length += tab.length;
73 }
74 }
75 int[] result = new int[length];
76 length = 0;
77 for (int[] tab : tabs) {
78 if (tab != null) {
79 System.arraycopy(tab, 0, result, length, tab.length);
80 length += tab.length;
81 }
82 }
83 return result;
84 }
85
86 /**
87 * Retourne un nouveau tableau qui est la concatenation des deux autres.
88 * Essai de garder pour le tableau resultat le type des tableaux en entré
89 * si possible. [Double], [Number] → [Number]; [Double], [Long] → [Object]
90 *
91 * @param tabs les tableaux
92 * @return le nouveau tableau ou null, si les deux tableaux sont null
93 * todo essayer de retourner le meilleur type de tableau possible
94 * [Double], [Long] → [Number]
95 */
96 public static Object[] concat(Object[]... tabs) {
97 Object[] result = null;
98 Class<?> clazz = null;
99 int length = 0;
100 for (Object[] tab : tabs) {
101 if (tab != null) {
102 length += tab.length;
103 Class<?> tmp = tab.getClass().getComponentType();
104 if (clazz == null) {
105 clazz = tmp;
106 } else if (tmp.isAssignableFrom(clazz)) {
107 clazz = tmp;
108 } else if (clazz.isAssignableFrom(tmp)) {
109 // do nothing, because clazz can't be better
110 } else {
111 clazz = Object.class;
112 }
113 }
114 }
115
116 if (clazz != null) {
117 result = (Object[]) Array.newInstance(clazz, length);
118 length = 0;
119 for (Object[] tab : tabs) {
120 if (tab != null) {
121 System.arraycopy(tab, 0, result, length, tab.length);
122 length += tab.length;
123 }
124 }
125 }
126 return result;
127 }
128
129 /**
130 * Ajoute a un tableau un ensemble d'element. Le type du tableau retourné
131 * est le meilleur possible.
132 *
133 * @param <E> FIXME
134 * @param <F> FIXME
135 * @param tab les valeurs initiales du tableau
136 * @param elems les elemements a ajouter
137 * @return un nouveau tableau contenant a la fin les elements souhaites
138 */
139 @SuppressWarnings("unchecked")
140 public static <E, F extends E> E[] concatElems(E[] tab, F... elems) {
141 E[] result;
142 result = (E[]) concat(tab, elems);
143 return result;
144 }
145
146 /**
147 * Recherche dans le table le 1er element qui correspond a la classe
148 * passée en argument.
149 *
150 * @param <A> FIXME
151 * @param tab le tableau dans lequel il faut chercher
152 * @param clazz la classe de l'objet souhaité
153 * @return un objet de la classe demandé, ou null si aucun ne correspond
154 */
155 public static <A> A search(Object[] tab, Class<A> clazz) {
156 A result = null;
157 for (Object o : tab) {
158 if (clazz.isInstance(o)) {
159 result = clazz.cast(o);
160 }
161 }
162 return result;
163 }
164
165 @SuppressWarnings({"unchecked"})
166 public static <T> T[] toArray(Collection list, Class<T> clazz) {
167 T[] result = (T[]) Array.newInstance(clazz, list == null ? 0 : list.size());
168 int i = 0;
169 for (Object o : list) {
170 result[i++] = (T) o;
171 }
172 return result;
173 }
174 } // ArrayUtil
175