io.sirix.axis.temporal.NextAxis Maven / Gradle / Ivy
package io.sirix.axis.temporal;
import io.sirix.api.NodeCursor;
import io.sirix.api.NodeReadOnlyTrx;
import io.sirix.api.NodeTrx;
import io.sirix.api.ResourceSession;
import io.sirix.axis.AbstractTemporalAxis;
import static java.util.Objects.requireNonNull;
/**
* Open the next revision and try to move to the node with the given node key.
*
* @author Johannes Lichtenberger
*
*/
public final class NextAxis
extends AbstractTemporalAxis {
/** Sirix {@link ResourceSession}. */
private final ResourceSession resourceSession;
/** Determines if it's the first call. */
private boolean first;
/** The revision number. */
private int revision;
/** Node key to lookup and retrieve. */
private final long nodeKey;
/**
* Constructor.
*
* @param rtx Sirix {@link NodeReadOnlyTrx}
*/
public NextAxis(final ResourceSession resourceSession, final R rtx) {
this.resourceSession = requireNonNull(resourceSession);
revision = 0;
nodeKey = rtx.getNodeKey();
revision = rtx.getRevisionNumber() + 1;
first = true;
}
@Override
protected R computeNext() {
if (revision <= resourceSession.getMostRecentRevisionNumber() && first) {
first = false;
final R rtx = resourceSession.beginNodeReadOnlyTrx(revision);
revision++;
if (rtx.moveTo(nodeKey)) {
return rtx;
} else {
rtx.close();
return endOfData();
}
} else {
return endOfData();
}
}
@Override
public ResourceSession getResourceManager() {
return resourceSession;
}
}