package jetbrains.mps.debugger.java.api.evaluation.proxies;
/*Generated by MPS */
import com.sun.jdi.ThreadReference;
import java.util.Iterator;
import jetbrains.mps.debugger.java.api.evaluation.EvaluationException;
import jetbrains.mps.debugger.java.api.evaluation.EvaluationRuntimeException;
public class IterableProxy<T extends IValueProxy> implements Iterable<T> {
private final IObjectValueProxy myValueProxy;
private final ThreadReference myThreadReference;
public IterableProxy(IObjectValueProxy valueProxy, ThreadReference threadReference) {
myValueProxy = valueProxy;
myThreadReference = threadReference;
}
@Override
public Iterator<T> iterator() {
try {
IObjectValueProxy iteratorProxy = (IObjectValueProxy) myValueProxy.invokeMethod("iterator", "()Ljava/util/Iterator;", myThreadReference);
return new IterableProxy.MyIterator(iteratorProxy);
} catch (EvaluationException e) {
throw new EvaluationRuntimeException(e);
}
}
private class MyIterator implements Iterator<T> {
private final IObjectValueProxy myIteratorProxy;
public MyIterator(IObjectValueProxy iteratorProxy) {
myIteratorProxy = iteratorProxy;
}
@Override
public boolean hasNext() {
try {
return (Boolean) ((PrimitiveValueProxy) myIteratorProxy.invokeMethod("hasNext", "()Z", myThreadReference)).getJavaValue();
} catch (EvaluationException e) {
throw new EvaluationRuntimeException(e);
}
}
@Override
public T next() {
try {
return (T) myIteratorProxy.invokeMethod("next", "()Ljava/lang/Object;", myThreadReference);
} catch (EvaluationException e) {
throw new EvaluationRuntimeException(e);
}
}
@Override
public void remove() {
try {
myIteratorProxy.invokeMethod("remove", "()V", myThreadReference);
} catch (EvaluationException e) {
throw new EvaluationRuntimeException(e);
}
}
}
}