ArrayUtils.java

  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. package org.apache.commons.collections4;

  18. /**
  19.  * <p>
  20.  * Operations on arrays, primitive arrays (like {@code int[]}) and primitive wrapper arrays (like {@code Integer[]}).
  21.  * </p>
  22.  * <p>
  23.  * 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
  24.  * contains a {@code null} element may throw an exception. Each method documents its behavior.
  25.  * </p>
  26.  * <p>
  27.  * Package private, might move to an internal package if this needs to be public.
  28.  * </p>
  29.  * <p>
  30.  * #ThreadSafe#
  31.  * </p>
  32.  *
  33.  * @since 4.2 (Copied from Apache Commons Lang.)
  34.  */
  35. final class ArrayUtils {

  36.     /**
  37.      * <p>
  38.      * Checks if the object is in the given array.
  39.      * </p>
  40.      * <p>
  41.      * The method returns {@code false} if a {@code null} array is passed in.
  42.      * </p>
  43.      *
  44.      * @param array        the array to search, may be {@code null}.
  45.      * @param objectToFind the object to find, may be {@code null}.
  46.      * @return {@code true} if the array contains the object
  47.      */
  48.     static boolean contains(final Object[] array, final Object objectToFind) {
  49.         return indexOf(array, objectToFind) != CollectionUtils.INDEX_NOT_FOUND;
  50.     }

  51.     /**
  52.      * <p>
  53.      * Finds the index of the given object in the array starting at the given index.
  54.      * </p>
  55.      * <p>
  56.      * This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
  57.      * </p>
  58.      * <p>
  59.      * A negative startIndex is treated as zero. A startIndex larger than the array length will return {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}).
  60.      * </p>
  61.      *
  62.      * @param array        the array to search for the object, may be {@code null}.
  63.      * @param objectToFind the object to find, may be {@code null}.
  64.      * @param startIndex   the index to start searching at
  65.      * @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}
  66.      *         array input
  67.      */
  68.     static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
  69.         if (array == null) {
  70.             return CollectionUtils.INDEX_NOT_FOUND;
  71.         }
  72.         if (startIndex < 0) {
  73.             startIndex = 0;
  74.         }
  75.         if (objectToFind == null) {
  76.             for (int i = startIndex; i < array.length; i++) {
  77.                 if (array[i] == null) {
  78.                     return i;
  79.                 }
  80.             }
  81.         } else {
  82.             for (int i = startIndex; i < array.length; i++) {
  83.                 if (objectToFind.equals(array[i])) {
  84.                     return i;
  85.                 }
  86.             }
  87.         }
  88.         return CollectionUtils.INDEX_NOT_FOUND;
  89.     }

  90.     /**
  91.      * <p>
  92.      * Finds the index of the given object in the array.
  93.      * </p>
  94.      * <p>
  95.      * This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
  96.      * </p>
  97.      *
  98.      * @param array        the array to search for the object, may be {@code null}.
  99.      * @param objectToFind the object to find, may be {@code null}.
  100.      * @return the index of the object within the array, {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
  101.      */
  102.     static <T> int indexOf(final T[] array, final Object objectToFind) {
  103.         return indexOf(array, objectToFind, 0);
  104.     }

  105.     /**
  106.      * Don't allow instances.
  107.      */
  108.     private ArrayUtils() {
  109.     }

  110. }