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 17 package com.android.car.vehiclehal; 18 19 import android.util.SparseArray; 20 import java.util.Iterator; 21 22 class Utils { Utils()23 private Utils() {} 24 25 static class SparseArrayIterator<T> 26 implements Iterable<SparseArrayIterator.SparseArrayEntry<T>>, 27 Iterator<SparseArrayIterator.SparseArrayEntry<T>> { 28 static class SparseArrayEntry<U> { 29 public final int key; 30 public final U value; 31 SparseArrayEntry(SparseArray<U> array, int index)32 SparseArrayEntry(SparseArray<U> array, int index) { 33 key = array.keyAt(index); 34 value = array.valueAt(index); 35 } 36 } 37 38 private final SparseArray<T> mArray; 39 private int mIndex = 0; 40 SparseArrayIterator(SparseArray<T> array)41 SparseArrayIterator(SparseArray<T> array) { 42 mArray = array; 43 } 44 45 @Override iterator()46 public Iterator<SparseArrayEntry<T>> iterator() { 47 return this; 48 } 49 50 @Override hasNext()51 public boolean hasNext() { 52 return mIndex < mArray.size(); 53 } 54 55 @Override next()56 public SparseArrayEntry<T> next() { 57 return new SparseArrayEntry<>(mArray, mIndex++); 58 } 59 } 60 } 61