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 <code>java.util.Arrays</code> class.
055     * @author dkrukovsky
056     * @see     java.util.Arrays
057     */
058    public class ArrayMgr {
059        // Suppresses default constructor, ensuring non-instantiability.
060        private ArrayMgr() {
061        }
062    
063        /**
064         * Returns the index within source array of the first occurrence of the
065         * target array, starting at the specified index.
066         * The source is the boolean array being searched, and the target is
067         * the boolean array being searched for.
068         *
069         * @param   source       the array being searched.
070         * @param   target       the array being searched for.
071         * @param   fromIndex    the index to begin searching from.
072         * @return  the index within source array of the first occurrence of the
073         *          target array, starting at the specified index.
074         */
075        public static int indexOf(boolean[] source, boolean[] target, int fromIndex) {
076            int sourceLen = source.length;
077            int targetLen = target.length;
078            if ((fromIndex > sourceLen) || (fromIndex < 0)) {
079                throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
080            }
081            else {
082                if (targetLen == 0) {
083                    return fromIndex;
084                }
085                else {
086                    int i = fromIndex;
087                    int max = sourceLen - targetLen;
088                    int j;
089                    while (i <= max) {
090                        j = 0;
091                        while ((j < targetLen) && (source[i + j] == target[j])) {
092                            j++;
093                        }
094                        if (j == targetLen) {
095                            return i;
096                        }
097                        else {
098                            i++;
099                        }
100                    }
101                    return -1;
102                }
103            }
104        }
105    
106            /**
107             * Returns the index within source array of the first occurrence of the
108             * target array, starting at the specified index.
109             * The source is the byte array being searched, and the target is
110             * the byte array being searched for.
111             *
112             * @param   source       the bytes being searched.
113             * @param   target       the bytes being searched for.
114             * @param   fromIndex    the index to begin searching from.
115             * @return  the index within source array of the first occurrence of the
116             *          target array, starting at the specified index.
117             */
118            public static int indexOf(byte[] source, byte[] target, int fromIndex) {
119                int sourceLen = source.length;
120                int targetLen = target.length;
121                if ((fromIndex > sourceLen) || (fromIndex < 0)) {
122                    throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
123                }
124                else {
125                    if (targetLen == 0) {
126                        return fromIndex;
127                    }
128                    else {
129                        int i = fromIndex;
130                        int max = sourceLen - targetLen;
131                        int j;
132                        while (i <= max) {
133                            j = 0;
134                            while ((j < targetLen) && (source[i + j] == target[j])) {
135                                j++;
136                            }
137                            if (j == targetLen) {
138                                return i;
139                            }
140                            else {
141                                i++;
142                            }
143                        }
144                        return -1;
145                    }
146                }
147            }
148    
149            /**
150             * Returns the index within source array of the first occurrence of the
151             * target array, starting at the specified index.
152             * The source is the char array being searched, and the target is
153             * the char array being searched for.
154             *
155             * @param   source       the chars being searched.
156             * @param   target       the chars being searched for.
157             * @param   fromIndex    the index to begin searching from.
158             * @return  the index within source array of the first occurrence of the
159             *          target array, starting at the specified index.
160             */
161            public static int indexOf(char[] source, char[] target, int fromIndex) {
162                int sourceLen = source.length;
163                int targetLen = target.length;
164                if ((fromIndex > sourceLen) || (fromIndex < 0)) {
165                    throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
166                }
167                else {
168                    if (targetLen == 0) {
169                        return fromIndex;
170                    }
171                    else {
172                        int i = fromIndex;
173                        int max = sourceLen - targetLen;
174                        int j;
175                        while (i <= max) {
176                            j = 0;
177                            while ((j < targetLen) && (source[i + j] == target[j])) {
178                                j++;
179                            }
180                            if (j == targetLen) {
181                                return i;
182                            }
183                            else {
184                                i++;
185                            }
186                        }
187                        return -1;
188                    }
189                }
190            }
191    
192            /**
193             * Returns the index within source array of the first occurrence of the
194             * target array, starting at the specified index.
195             * The source is the short array being searched, and the target is
196             * the short array being searched for.
197             *
198             * @param   source       the array being searched.
199             * @param   target       the array being searched for.
200             * @param   fromIndex    the index to begin searching from.
201             * @return  the index within source array of the first occurrence of the
202             *          target array, starting at the specified index.
203             */
204            public static int indexOf(short[] source, short[] target, int fromIndex) {
205                int sourceLen = source.length;
206                int targetLen = target.length;
207                if ((fromIndex > sourceLen) || (fromIndex < 0)) {
208                    throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
209                }
210                else {
211                    if (targetLen == 0) {
212                        return fromIndex;
213                    }
214                    else {
215                        int i = fromIndex;
216                        int max = sourceLen - targetLen;
217                        int j;
218                        while (i <= max) {
219                            j = 0;
220                            while ((j < targetLen) && (source[i + j] == target[j])) {
221                                j++;
222                            }
223                            if (j == targetLen) {
224                                return i;
225                            }
226                            else {
227                                i++;
228                            }
229                        }
230                        return -1;
231                    }
232                }
233            }
234    
235        /**
236         * Returns the index within source array of the first occurrence of the
237         * target array, starting at the specified index.
238         * The source is the int array being searched, and the target is
239         * the int array being searched for.
240         *
241         * @param   source       the array being searched.
242         * @param   target       the array being searched for.
243         * @param   fromIndex    the index to begin searching from.
244         * @return  the index within source array of the first occurrence of the
245         *          target array, starting at the specified index.
246         */
247        public static int indexOf(int[] source, int[] target, int fromIndex) {
248                    int sourceLen = source.length;
249                    int targetLen = target.length;
250                    if ((fromIndex > sourceLen) || (fromIndex < 0)) {
251                            throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
252                    }
253                    else {
254                            if (targetLen == 0) {
255                                    return fromIndex;
256                            }
257                            else {
258                                    int i = fromIndex;
259                                    int max = sourceLen - targetLen;
260                                    int j;
261                                    while (i <= max) {
262                                            j = 0;
263                                            while ((j < targetLen) && (source[i + j] == target[j])) {
264                                                    j++;
265                                            }
266                                            if (j == targetLen) {
267                                                    return i;
268                                            }
269                                            else {
270                                                    i++;
271                                            }
272                                    }
273                                    return -1;
274                            }
275                    }
276        }
277    
278            /**
279             * Returns the index within source array of the first occurrence of the
280             * target array, starting at the specified index.
281             * The source is the array of long being searched, and the target is
282             * the array of long being searched for.
283             *
284             * @param   source       the array being searched.
285             * @param   target       the array being searched for.
286             * @param   fromIndex    the index to begin searching from.
287             * @return  the index within source array of the first occurrence of the
288             *          target array, starting at the specified index.
289             */
290            public static int indexOf(long[] source, long[] target, int fromIndex) {
291                    int sourceLen = source.length;
292                    int targetLen = target.length;
293                    if ((fromIndex > sourceLen) || (fromIndex < 0)) {
294                            throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
295                    }
296                    else {
297                            if (targetLen == 0) {
298                                    return fromIndex;
299                            }
300                            else {
301                                    int i = fromIndex;
302                                    int max = sourceLen - targetLen;
303                                    int j;
304                                    while (i <= max) {
305                                            j = 0;
306                                            while ((j < targetLen) && (source[i + j] == target[j])) {
307                                                    j++;
308                                            }
309                                            if (j == targetLen) {
310                                                    return i;
311                                            }
312                                            else {
313                                                    i++;
314                                            }
315                                    }
316                                    return -1;
317                            }
318                    }
319            }
320    
321            /**
322             * Returns the index within source array of the first occurrence of the
323             * target array, starting at the specified index.
324             * The source is the double array being searched, and the target is
325             * the double array being searched for.
326             *
327             * @param   source       the array being searched.
328             * @param   target       the array being searched for.
329             * @param   fromIndex    the index to begin searching from.
330             * @return  the index within source array of the first occurrence of the
331             *          target array, starting at the specified index.
332             */
333            public static int indexOf(double[] source, double[] target, int fromIndex) {
334                    int sourceLen = source.length;
335                    int targetLen = target.length;
336                    if ((fromIndex > sourceLen) || (fromIndex < 0)) {
337                            throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
338                    }
339                    else {
340                            if (targetLen == 0) {
341                                    return fromIndex;
342                            }
343                            else {
344                                    int i = fromIndex;
345                                    int max = sourceLen - targetLen;
346                                    int j;
347                                    while (i <= max) {
348                                            j = 0;
349                                            while ((j < targetLen) && (source[i + j] == target[j])) {
350                                                    j++;
351                                            }
352                                            if (j == targetLen) {
353                                                    return i;
354                                            }
355                                            else {
356                                                    i++;
357                                            }
358                                    }
359                                    return -1;
360                            }
361                    }
362            }
363    
364            /**
365             * Returns the index within source array of the first occurrence of the
366             * target array, starting at the specified index.
367             * The source is the double array being searched, and the target is
368             * the double array being searched for.
369             *
370             * @param   source       the array being searched.
371             * @param   target       the array being searched for.
372             * @param   fromIndex    the index to begin searching from.
373             * @return  the index within source array of the first occurrence of the
374             *          target array, starting at the specified index.
375             */
376            public static int indexOf(float[] source, float[] target, int fromIndex) {
377                    int sourceLen = source.length;
378                    int targetLen = target.length;
379                    if ((fromIndex > sourceLen) || (fromIndex < 0)) {
380                            throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
381                    }
382                    else {
383                            if (targetLen == 0) {
384                                    return fromIndex;
385                            }
386                            else {
387                                    int i = fromIndex;
388                                    int max = sourceLen - targetLen;
389                                    int j;
390                                    while (i <= max) {
391                                            j = 0;
392                                            while ((j < targetLen) && (source[i + j] == target[j])) {
393                                                    j++;
394                                            }
395                                            if (j == targetLen) {
396                                                    return i;
397                                            }
398                                            else {
399                                                    i++;
400                                            }
401                                    }
402                                    return -1;
403                            }
404                    }
405            }
406    
407            /**
408             * Returns the index within source array of the first occurrence of the
409             * target array, starting at the specified index.
410             * The source is the array of Object being searched, and the target is
411             * the array of Object being searched for.
412             * Two objects <tt>e1</tt>
413         * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
414         * : e1.equals(e2))</tt>.
415             *
416             * @param   source       the array being searched.
417             * @param   target       the array being searched for.
418             * @param   fromIndex    the index to begin searching from.
419             * @return  the index within source array of the first occurrence of the
420             *          target array, starting at the specified index.
421             */
422            public static int indexOf(Object[] source, Object[] target, int fromIndex) {
423                    int sourceLen = source.length;
424                    int targetLen = target.length;
425                    if ((fromIndex > sourceLen) || (fromIndex < 0)) {
426                            throw new IllegalArgumentException(fromIndex + " is not between 0 and " + sourceLen);
427                    }
428                    else {
429                            if (targetLen == 0) {
430                                    return fromIndex;
431                            }
432                            else {
433                                    int i = fromIndex;
434                                    int max = sourceLen - targetLen;
435                                    int j;
436                                    while (i <= max) {
437                                            j = 0;
438                                            while ((j < targetLen) && (source[i + j] == null ? target[j] == null :
439                                            source[i + j].equals(target[j]))) {
440                                                    j++;
441                                            }
442                                            if (j == targetLen) {
443                                                    return i;
444                                            }
445                                            else {
446                                                    i++;
447                                            }
448                                    }
449                                    return -1;
450                            }
451                    }
452            }
453    }