001 /*
002 * Created on 25/6/2004
003 *
004 * Copyright (C) 2004 Denis Krukovsky. All rights reserved.
005 * ====================================================================
006 * The Software License (based on Apache Software License, Version 1.1)
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions
010 * are met:
011 *
012 * 1. Redistributions of source code must retain the above copyright
013 * notice, this list of conditions and the following disclaimer.
014 *
015 * 2. Redistributions in binary form must reproduce the above copyright
016 * notice, this list of conditions and the following disclaimer in
017 * the documentation and/or other materials provided with the
018 * distribution.
019 *
020 * 3. The end-user documentation included with the redistribution,
021 * if any, must include the following acknowledgment:
022 * "This product includes software developed by
023 * Denis Krukovsky (dkrukovsky at yahoo.com)."
024 * Alternately, this acknowledgment may appear in the software itself,
025 * if and wherever such third-party acknowledgments normally appear.
026 *
027 * 4. The names "dot useful" and "Denis Krukovsky" must not be used to
028 * endorse or promote products derived from this software without
029 * prior written permission. For written permission, please
030 * contact dkrukovsky at yahoo.com.
031 *
032 * 5. Products derived from this software may not be called "useful",
033 * nor may "useful" appear in their name, without prior written
034 * permission of Denis Krukovsky.
035 *
036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039 * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
040 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047 * SUCH DAMAGE.
048 * ====================================================================
049 */
050
051 package org.dotuseful.util;
052
053 /**
054 * This class contains various methods for manipulating arrays in addition to
055 * <code>java.util.Arrays</code> class.
056 *
057 * @author dkrukovsky
058 * @see java.util.Arrays
059 */
060 public class ArrayMgr {
061 // Suppresses default constructor, ensuring non-instantiability.
062 private ArrayMgr() {
063 }
064
065 /**
066 * Returns the index within source array of the first occurrence of the
067 * target array, starting at the specified index. The source is the boolean
068 * array being searched, and the target is the boolean array being searched
069 * for.
070 *
071 * @param source
072 * the array being searched.
073 * @param target
074 * the array being searched for.
075 * @param fromIndex
076 * the index to begin searching from.
077 * @return the index within source array of the first occurrence of the
078 * target array, starting at the specified index.
079 */
080 public static int indexOf(boolean[] source, boolean[] target, int fromIndex) {
081 int sourceLen = source.length;
082 int targetLen = target.length;
083 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
084 throw new IllegalArgumentException(fromIndex
085 + " is not between 0 and " + sourceLen);
086 } else {
087 if (targetLen == 0) {
088 return fromIndex;
089 } else {
090 int i = fromIndex;
091 int max = sourceLen - targetLen;
092 int j;
093 while (i <= max) {
094 j = 0;
095 while ((j < targetLen) && (source[i + j] == target[j])) {
096 j++;
097 }
098 if (j == targetLen) {
099 return i;
100 } else {
101 i++;
102 }
103 }
104 return -1;
105 }
106 }
107 }
108
109 /**
110 * Returns the index within source array of the first occurrence of the
111 * target array, starting at the specified index. The source is the byte
112 * array being searched, and the target is the byte array being searched
113 * for.
114 *
115 * @param source
116 * the bytes being searched.
117 * @param target
118 * the bytes being searched for.
119 * @param fromIndex
120 * the index to begin searching from.
121 * @return the index within source array of the first occurrence of the
122 * target array, starting at the specified index.
123 */
124 public static int indexOf(byte[] source, byte[] target, int fromIndex) {
125 int sourceLen = source.length;
126 int targetLen = target.length;
127 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
128 throw new IllegalArgumentException(fromIndex
129 + " is not between 0 and " + sourceLen);
130 } else {
131 if (targetLen == 0) {
132 return fromIndex;
133 } else {
134 int i = fromIndex;
135 int max = sourceLen - targetLen;
136 int j;
137 while (i <= max) {
138 j = 0;
139 while ((j < targetLen) && (source[i + j] == target[j])) {
140 j++;
141 }
142 if (j == targetLen) {
143 return i;
144 } else {
145 i++;
146 }
147 }
148 return -1;
149 }
150 }
151 }
152
153 /**
154 * Returns the index within source array of the first occurrence of the
155 * target array, starting at the specified index. The source is the char
156 * array being searched, and the target is the char array being searched
157 * for.
158 *
159 * @param source
160 * the chars being searched.
161 * @param target
162 * the chars being searched for.
163 * @param fromIndex
164 * the index to begin searching from.
165 * @return the index within source array of the first occurrence of the
166 * target array, starting at the specified index.
167 */
168 public static int indexOf(char[] source, char[] target, int fromIndex) {
169 int sourceLen = source.length;
170 int targetLen = target.length;
171 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
172 throw new IllegalArgumentException(fromIndex
173 + " is not between 0 and " + sourceLen);
174 } else {
175 if (targetLen == 0) {
176 return fromIndex;
177 } else {
178 int i = fromIndex;
179 int max = sourceLen - targetLen;
180 int j;
181 while (i <= max) {
182 j = 0;
183 while ((j < targetLen) && (source[i + j] == target[j])) {
184 j++;
185 }
186 if (j == targetLen) {
187 return i;
188 } else {
189 i++;
190 }
191 }
192 return -1;
193 }
194 }
195 }
196
197 /**
198 * Returns the index within source array of the first occurrence of the
199 * target array, starting at the specified index. The source is the short
200 * array being searched, and the target is the short array being searched
201 * for.
202 *
203 * @param source
204 * the array being searched.
205 * @param target
206 * the array being searched for.
207 * @param fromIndex
208 * the index to begin searching from.
209 * @return the index within source array of the first occurrence of the
210 * target array, starting at the specified index.
211 */
212 public static int indexOf(short[] source, short[] target, int fromIndex) {
213 int sourceLen = source.length;
214 int targetLen = target.length;
215 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
216 throw new IllegalArgumentException(fromIndex
217 + " is not between 0 and " + sourceLen);
218 } else {
219 if (targetLen == 0) {
220 return fromIndex;
221 } else {
222 int i = fromIndex;
223 int max = sourceLen - targetLen;
224 int j;
225 while (i <= max) {
226 j = 0;
227 while ((j < targetLen) && (source[i + j] == target[j])) {
228 j++;
229 }
230 if (j == targetLen) {
231 return i;
232 } else {
233 i++;
234 }
235 }
236 return -1;
237 }
238 }
239 }
240
241 /**
242 * Returns the index within source array of the first occurrence of the
243 * target array, starting at the specified index. The source is the int
244 * array being searched, and the target is the int array being searched for.
245 *
246 * @param source
247 * the array being searched.
248 * @param target
249 * the array being searched for.
250 * @param fromIndex
251 * the index to begin searching from.
252 * @return the index within source array of the first occurrence of the
253 * target array, starting at the specified index.
254 */
255 public static int indexOf(int[] source, int[] target, int fromIndex) {
256 int sourceLen = source.length;
257 int targetLen = target.length;
258 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
259 throw new IllegalArgumentException(fromIndex
260 + " is not between 0 and " + sourceLen);
261 } else {
262 if (targetLen == 0) {
263 return fromIndex;
264 } else {
265 int i = fromIndex;
266 int max = sourceLen - targetLen;
267 int j;
268 while (i <= max) {
269 j = 0;
270 while ((j < targetLen) && (source[i + j] == target[j])) {
271 j++;
272 }
273 if (j == targetLen) {
274 return i;
275 } else {
276 i++;
277 }
278 }
279 return -1;
280 }
281 }
282 }
283
284 /**
285 * Returns the index within source array of the first occurrence of the
286 * target array, starting at the specified index. The source is the array of
287 * long being searched, and the target is the array of long being searched
288 * for.
289 *
290 * @param source
291 * the array being searched.
292 * @param target
293 * the array being searched for.
294 * @param fromIndex
295 * the index to begin searching from.
296 * @return the index within source array of the first occurrence of the
297 * target array, starting at the specified index.
298 */
299 public static int indexOf(long[] source, long[] target, int fromIndex) {
300 int sourceLen = source.length;
301 int targetLen = target.length;
302 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
303 throw new IllegalArgumentException(fromIndex
304 + " is not between 0 and " + sourceLen);
305 } else {
306 if (targetLen == 0) {
307 return fromIndex;
308 } else {
309 int i = fromIndex;
310 int max = sourceLen - targetLen;
311 int j;
312 while (i <= max) {
313 j = 0;
314 while ((j < targetLen) && (source[i + j] == target[j])) {
315 j++;
316 }
317 if (j == targetLen) {
318 return i;
319 } else {
320 i++;
321 }
322 }
323 return -1;
324 }
325 }
326 }
327
328 /**
329 * Returns the index within source array of the first occurrence of the
330 * target array, starting at the specified index. The source is the double
331 * array being searched, and the target is the double array being searched
332 * for.
333 *
334 * @param source
335 * the array being searched.
336 * @param target
337 * the array being searched for.
338 * @param fromIndex
339 * the index to begin searching from.
340 * @return the index within source array of the first occurrence of the
341 * target array, starting at the specified index.
342 */
343 public static int indexOf(double[] source, double[] target, int fromIndex) {
344 int sourceLen = source.length;
345 int targetLen = target.length;
346 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
347 throw new IllegalArgumentException(fromIndex
348 + " is not between 0 and " + sourceLen);
349 } else {
350 if (targetLen == 0) {
351 return fromIndex;
352 } else {
353 int i = fromIndex;
354 int max = sourceLen - targetLen;
355 int j;
356 while (i <= max) {
357 j = 0;
358 while ((j < targetLen) && (source[i + j] == target[j])) {
359 j++;
360 }
361 if (j == targetLen) {
362 return i;
363 } else {
364 i++;
365 }
366 }
367 return -1;
368 }
369 }
370 }
371
372 /**
373 * Returns the index within source array of the first occurrence of the
374 * target array, starting at the specified index. The source is the double
375 * array being searched, and the target is the double array being searched
376 * for.
377 *
378 * @param source
379 * the array being searched.
380 * @param target
381 * the array being searched for.
382 * @param fromIndex
383 * the index to begin searching from.
384 * @return the index within source array of the first occurrence of the
385 * target array, starting at the specified index.
386 */
387 public static int indexOf(float[] source, float[] target, int fromIndex) {
388 int sourceLen = source.length;
389 int targetLen = target.length;
390 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
391 throw new IllegalArgumentException(fromIndex
392 + " is not between 0 and " + sourceLen);
393 } else {
394 if (targetLen == 0) {
395 return fromIndex;
396 } else {
397 int i = fromIndex;
398 int max = sourceLen - targetLen;
399 int j;
400 while (i <= max) {
401 j = 0;
402 while ((j < targetLen) && (source[i + j] == target[j])) {
403 j++;
404 }
405 if (j == targetLen) {
406 return i;
407 } else {
408 i++;
409 }
410 }
411 return -1;
412 }
413 }
414 }
415
416 /**
417 * Returns the index within source array of the first occurrence of the
418 * target array, starting at the specified index. The source is the array of
419 * Object being searched, and the target is the array of Object being
420 * searched for. Two objects <tt>e1</tt> and <tt>e2</tt> are considered
421 * <i>equal </i> if <tt>(e1==null ? e2==null
422 * : e1.equals(e2))</tt>.
423 *
424 * @param source
425 * the array being searched.
426 * @param target
427 * the array being searched for.
428 * @param fromIndex
429 * the index to begin searching from.
430 * @return the index within source array of the first occurrence of the
431 * target array, starting at the specified index.
432 */
433 public static int indexOf(Object[] source, Object[] target, int fromIndex) {
434 int sourceLen = source.length;
435 int targetLen = target.length;
436 if ((fromIndex > sourceLen) || (fromIndex < 0)) {
437 throw new IllegalArgumentException(fromIndex
438 + " is not between 0 and " + sourceLen);
439 } else {
440 if (targetLen == 0) {
441 return fromIndex;
442 } else {
443 int i = fromIndex;
444 int max = sourceLen - targetLen;
445 int j;
446 while (i <= max) {
447 j = 0;
448 while ((j < targetLen)
449 && (source[i + j] == null ? target[j] == null
450 : source[i + j].equals(target[j]))) {
451 j++;
452 }
453 if (j == targetLen) {
454 return i;
455 } else {
456 i++;
457 }
458 }
459 return -1;
460 }
461 }
462 }
463 }