blob: 669f66aefa584138a08361cdb55e391166137a96 [file] [log] [blame]
/*
* 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.app.slice;
import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;
import java.lang.reflect.Array;
import java.util.Objects;
/**
* @hide
*/
@RestrictTo(Scope.LIBRARY)
class ArrayUtils {
public static <T> boolean contains(T[] array, T item) {
for (T t : array) {
if (Objects.equals(t, item)) {
return true;
}
}
return false;
}
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;
}
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 (Objects.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;
}
}