blob: 598f0be97485d20e0a38345dbc7b90db46a91403 [file] [log] [blame]
// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.utils.codeinspector;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Predicate;
class FilteredInstructionIterator<T extends InstructionSubject> implements Iterator<T> {
private final InstructionIterator iterator;
private final Predicate<InstructionSubject> predicate;
private InstructionSubject pendingNext = null;
FilteredInstructionIterator(
CodeInspector codeInspector, MethodSubject method, Predicate<InstructionSubject> predicate) {
this.iterator = codeInspector.createInstructionIterator(method);
this.predicate = predicate;
}
@Override
public boolean hasNext() {
if (pendingNext == null) {
while (iterator.hasNext()) {
pendingNext = iterator.next();
if (predicate.test(pendingNext)) {
break;
}
pendingNext = null;
}
}
return pendingNext != null;
}
@Override
public T next() {
hasNext();
if (pendingNext == null) {
throw new NoSuchElementException();
}
// We cannot tell if the provided predicate will only match instruction subjects of type T.
@SuppressWarnings("unchecked")
T result = (T) pendingNext;
pendingNext = null;
return result;
}
}