1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 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.facebook.jni; 18 19 import com.facebook.jni.annotations.DoNotStrip; 20 import java.util.Iterator; 21 import javax.annotation.Nullable; 22 23 /** 24 * To iterate over an Iterator from C++ requires two calls per entry: hasNext() and next(). This 25 * helper reduces it to one call and one field get per entry. It does not use a generic argument, 26 * since in C++, the types will be erased, anyway. This is *not* a {@link java.util.Iterator}. 27 */ 28 @DoNotStrip 29 public class IteratorHelper { 30 private final Iterator mIterator; 31 32 // This is private, but accessed via JNI. 33 @DoNotStrip private @Nullable Object mElement; 34 35 @DoNotStrip IteratorHelper(Iterator iterator)36 public IteratorHelper(Iterator iterator) { 37 mIterator = iterator; 38 } 39 40 @DoNotStrip IteratorHelper(Iterable iterable)41 public IteratorHelper(Iterable iterable) { 42 mIterator = iterable.iterator(); 43 } 44 45 /** 46 * Moves the helper to the next entry in the map, if any. Returns true iff there is an entry to 47 * read. 48 */ 49 @DoNotStrip hasNext()50 boolean hasNext() { 51 if (mIterator.hasNext()) { 52 mElement = mIterator.next(); 53 return true; 54 } else { 55 mElement = null; 56 return false; 57 } 58 } 59 } 60