| /* |
| * Copyright (C) 2017 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package androidx.slice; |
| |
| |
| import androidx.annotation.RequiresApi; |
| import androidx.annotation.RestrictTo; |
| import androidx.annotation.RestrictTo.Scope; |
| import androidx.core.util.ObjectsCompat; |
| |
| import java.lang.reflect.Array; |
| |
| /** |
| * @hide |
| */ |
| @RestrictTo(Scope.LIBRARY_GROUP) |
| @RequiresApi(19) |
| class ArrayUtils { |
| |
| public static <T> boolean contains(T[] array, T item) { |
| for (T t : array) { |
| if (ObjectsCompat.equals(t, item)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| @SuppressWarnings("unchecked") |
| public static <T> T[] appendElement(Class<T> kind, T[] array, T element) { |
| final T[] result; |
| final int end; |
| if (array != null) { |
| end = array.length; |
| result = (T[]) Array.newInstance(kind, end + 1); |
| System.arraycopy(array, 0, result, 0, end); |
| } else { |
| end = 0; |
| result = (T[]) Array.newInstance(kind, 1); |
| } |
| result[end] = element; |
| return result; |
| } |
| |
| @SuppressWarnings("unchecked") |
| public static <T> T[] removeElement(Class<T> kind, T[] array, T element) { |
| if (array != null) { |
| if (!contains(array, element)) { |
| return array; |
| } |
| final int length = array.length; |
| for (int i = 0; i < length; i++) { |
| if (ObjectsCompat.equals(array[i], element)) { |
| if (length == 1) { |
| return null; |
| } |
| T[] result = (T[]) Array.newInstance(kind, length - 1); |
| System.arraycopy(array, 0, result, 0, i); |
| System.arraycopy(array, i + 1, result, i, length - i - 1); |
| return result; |
| } |
| } |
| } |
| return array; |
| } |
| |
| private ArrayUtils() { |
| } |
| } |