1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.collections4;
19
20 /**
21 * <p>
22 * Operations on arrays, primitive arrays (like {@code int[]}) and primitive wrapper arrays (like {@code Integer[]}).
23 * </p>
24 * <p>
25 * This class tries to handle {@code null} input gracefully. An exception will not be thrown for a {@code null} array input. However, an Object array that
26 * contains a {@code null} element may throw an exception. Each method documents its behavior.
27 * </p>
28 * <p>
29 * Package private, might move to an internal package if this needs to be public.
30 * </p>
31 * <p>
32 * #ThreadSafe#
33 * </p>
34 *
35 * @since 4.2 (Copied from Apache Commons Lang.)
36 */
37 final class ArrayUtils {
38
39 /**
40 * <p>
41 * Checks if the object is in the given array.
42 * </p>
43 * <p>
44 * The method returns {@code false} if a {@code null} array is passed in.
45 * </p>
46 *
47 * @param array the array to search, may be {@code null}.
48 * @param objectToFind the object to find, may be {@code null}.
49 * @return {@code true} if the array contains the object
50 */
51 static boolean contains(final Object[] array, final Object objectToFind) {
52 return indexOf(array, objectToFind) != CollectionUtils.INDEX_NOT_FOUND;
53 }
54
55 /**
56 * <p>
57 * Finds the index of the given object in the array starting at the given index.
58 * </p>
59 * <p>
60 * This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
61 * </p>
62 * <p>
63 * A negative startIndex is treated as zero. A startIndex larger than the array length will return {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}).
64 * </p>
65 *
66 * @param array the array to search for the object, may be {@code null}.
67 * @param objectToFind the object to find, may be {@code null}.
68 * @param startIndex the index to start searching at
69 * @return the index of the object within the array starting at the index, {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null}
70 * array input
71 */
72 static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
73 if (array == null) {
74 return CollectionUtils.INDEX_NOT_FOUND;
75 }
76 if (startIndex < 0) {
77 startIndex = 0;
78 }
79 if (objectToFind == null) {
80 for (int i = startIndex; i < array.length; i++) {
81 if (array[i] == null) {
82 return i;
83 }
84 }
85 } else {
86 for (int i = startIndex; i < array.length; i++) {
87 if (objectToFind.equals(array[i])) {
88 return i;
89 }
90 }
91 }
92 return CollectionUtils.INDEX_NOT_FOUND;
93 }
94
95 /**
96 * <p>
97 * Finds the index of the given object in the array.
98 * </p>
99 * <p>
100 * This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
101 * </p>
102 *
103 * @param array the array to search for the object, may be {@code null}.
104 * @param objectToFind the object to find, may be {@code null}.
105 * @return the index of the object within the array, {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
106 */
107 static <T> int indexOf(final T[] array, final Object objectToFind) {
108 return indexOf(array, objectToFind, 0);
109 }
110
111 /**
112 * Don't allow instances.
113 */
114 private ArrayUtils() {
115 }
116
117 }