src.com.android.server.utils.Snapshots Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android-all Show documentation
Show all versions of android-all Show documentation
A library jar that provides APIs for Applications written for the Google Android Platform.
/*
* Copyright (C) 2020 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 com.android.server.utils;
import android.annotation.NonNull;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.util.SparseSetArray;
/**
* A collection of useful methods for manipulating Snapshot classes. This is similar to
* java.util.Objects or java.util.Arrays.
*/
public class Snapshots {
/**
* Return the snapshot of an object, if the object extends {@link Snapper}, or the object
* itself.
* @param o The object to be copied
* @return A snapshot of the object, if the object extends {@link Snapper}
*/
public static T maybeSnapshot(T o) {
if (o instanceof Snappable) {
return ((Snappable) o).snapshot();
} else {
return o;
}
}
/**
* Copy a SparseArray in a manner suitable for a snapshot. The destination must be
* empty. This is not a snapshot because the elements are copied by reference even if
* they are {@link Snappable}.
* @param dst The destination array. It must be empty.
* @param src The source array
*/
public static void copy(@NonNull SparseArray dst, @NonNull SparseArray src) {
if (dst.size() != 0) {
throw new IllegalArgumentException("copy destination is not empty");
}
final int end = src.size();
for (int i = 0; i < end; i++) {
dst.put(src.keyAt(i), src.valueAt(i));
}
}
/**
* Copy a SparseSetArray in a manner suitable for a snapshot. The destination must be
* empty. This is not a snapshot because the elements are copied by reference even if
* they are {@link Snappable}.
* @param dst The destination array. It must be empty.
* @param src The source array
*/
public static void copy(@NonNull SparseSetArray dst, @NonNull SparseSetArray src) {
if (dst.size() != 0) {
throw new IllegalArgumentException("copy destination is not empty");
}
final int end = src.size();
for (int i = 0; i < end; i++) {
final int size = src.sizeAt(i);
for (int j = 0; j < size; j++) {
dst.add(src.keyAt(i), src.valueAt(i, j));
}
}
}
/**
* Make a snapshot of .
* @param dst The destination array. It must be empty.
* @param src The source array
*/
public static void snapshot(@NonNull SparseIntArray dst, @NonNull SparseIntArray src) {
if (dst.size() != 0) {
throw new IllegalArgumentException("snapshot destination is not empty");
}
final int end = src.size();
for (int i = 0; i < end; i++) {
dst.put(src.keyAt(i), src.valueAt(i));
}
}
/**
* Make a "snapshot" of . mst be empty. The destination is just a
* copy of the source except that if the source elements implement Snappable, then
* the elements in the destination will be snapshots of elements from the source.
* @param dst The destination array. It must be empty.
* @param src The source array
*/
public static > void snapshot(@NonNull SparseArray dst,
@NonNull SparseArray src) {
if (dst.size() != 0) {
throw new IllegalArgumentException("snapshot destination is not empty");
}
final int end = src.size();
for (int i = 0; i < end; i++) {
dst.put(src.keyAt(i), src.valueAt(i).snapshot());
}
}
/**
* Make a "snapshot" of . mst be empty. The destination is a
* copy of the source except that snapshots are taken of the elements.
* @param dst The destination array. It must be empty.
* @param src The source array
*/
public static > void snapshot(@NonNull SparseSetArray dst,
@NonNull SparseSetArray src) {
if (dst.size() != 0) {
throw new IllegalArgumentException("snapshot destination is not empty");
}
final int end = src.size();
for (int i = 0; i < end; i++) {
final int size = src.sizeAt(i);
for (int j = 0; j < size; j++) {
dst.add(src.keyAt(i), src.valueAt(i, j).snapshot());
}
}
}
}