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 android.util.Pair;
19 import android.util.SparseArray;
20 import java.util.stream.IntStream;
21 import java.util.stream.Stream;
22 
23 /**
24  * Helper class that provides Stream abstractions for android.util.SparseArray
25  */
26 public class SparseArrayStream {
keyStream(SparseArray<E> array)27     public static <E> IntStream keyStream(SparseArray<E> array) {
28         return IntStream.range(0, array.size()).map(array::keyAt);
29     }
30 
valueStream(SparseArray<E> array)31     public static <E> Stream<E> valueStream(SparseArray<E> array) {
32         return IntStream.range(0, array.size()).mapToObj(array::valueAt);
33     }
34 
pairStream(SparseArray<E> array)35     public static <E> Stream<Pair<Integer, E>> pairStream(SparseArray<E> array) {
36         return IntStream.range(0, array.size()).mapToObj(
37             i -> new Pair<>(array.keyAt(i), array.valueAt(i)));
38     }
39 }
40