org.gridkit.jvmtool.event.ChainedEventReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sjk-stacktrace Show documentation
Show all versions of sjk-stacktrace Show documentation
Thread dumps: capture and encoding
package org.gridkit.jvmtool.event;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.Callable;
public abstract class ChainedEventReader implements EventReader {
public static EventReader chain(final EventReader... readers) {
return new ChainedEventReader() {
int next;
@Override
protected EventReader produceNext() {
return next < readers.length ? readers[next++] : null;
}
};
}
public static EventReader chain(final EventReader reader, final Callable producer) {
return new ChainedEventReader() {
boolean first;
boolean done;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected EventReader produceNext() {
if (!first) {
first = true;
return reader;
}
if (done) {
return null;
}
else {
done = true;
try {
return (EventReader) producer.call();
} catch (Exception e) {
return new SingleEventReader(new SimpleErrorEvent(e));
}
}
}
};
}
private boolean init;
private EventReader current;
public ChainedEventReader() {
}
@Override
public EventReader morph(EventMorpher morpher) {
return MorphingEventReader.morph(this, morpher);
}
@Override
public Iterator iterator() {
return this;
}
@Override
public boolean hasNext() {
if (!init) {
init = true;
current = produceNext();
}
if (current == null) {
return false;
}
while(true) {
if (current.hasNext()) {
return true;
}
else {
current.dispose();
current = produceNext();
if (current == null) {
return false;
}
}
}
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return current.next();
}
@Override
public T peekNext() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return current.peekNext();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
// @Override
// @SuppressWarnings({ "unchecked", "rawtypes" })
// public EventReader filterByClass(Class typeFilter) {
// this.typeFilter = (Class)typeFilter;
// if (current != null) {
// current = (EventReader)current.filterByClass(typeFilter);
// }
// return (EventReader)this;
// }
//
@Override
public void dispose() {
if (current != null) {
current.dispose();
}
}
protected abstract EventReader produceNext();
}