1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.car.util; 17 18 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE; 19 20 import android.util.Pair; 21 import android.util.SparseArray; 22 23 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 24 25 import java.util.stream.IntStream; 26 import java.util.stream.Stream; 27 28 /** 29 * Helper class that provides Stream abstractions for android.util.SparseArray 30 */ 31 public final class SparseArrayStream { 32 /** TODO: add javadoc */ keyStream(SparseArray<E> array)33 public static <E> IntStream keyStream(SparseArray<E> array) { 34 return IntStream.range(0, array.size()).map(array::keyAt); 35 } 36 37 /** TODO: add javadoc */ valueStream(SparseArray<E> array)38 public static <E> Stream<E> valueStream(SparseArray<E> array) { 39 return IntStream.range(0, array.size()).mapToObj(array::valueAt); 40 } 41 42 /** TODO: add javadoc */ pairStream(SparseArray<E> array)43 public static <E> Stream<Pair<Integer, E>> pairStream(SparseArray<E> array) { 44 return IntStream.range(0, array.size()).mapToObj( 45 i -> new Pair<>(array.keyAt(i), array.valueAt(i))); 46 } 47 48 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE, 49 details = "private constructor") SparseArrayStream()50 private SparseArrayStream() { 51 throw new UnsupportedOperationException("contains only static methods"); 52 } 53 } 54