1 /* 2 * Copyright (C) 2017 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; 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 class SparseArrayStream { keyStream(SparseArray<E> array)32 public static <E> IntStream keyStream(SparseArray<E> array) { 33 return IntStream.range(0, array.size()).map(array::keyAt); 34 } 35 valueStream(SparseArray<E> array)36 public static <E> Stream<E> valueStream(SparseArray<E> array) { 37 return IntStream.range(0, array.size()).mapToObj(array::valueAt); 38 } 39 pairStream(SparseArray<E> array)40 public static <E> Stream<Pair<Integer, E>> pairStream(SparseArray<E> array) { 41 return IntStream.range(0, array.size()).mapToObj( 42 i -> new Pair<>(array.keyAt(i), array.valueAt(i))); 43 } 44 45 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE, 46 details = "private constructor") SparseArrayStream()47 private SparseArrayStream() { 48 throw new UnsupportedOperationException("contains only static methods"); 49 } 50 } 51