io.sirix.axis.temporal.LastAxis 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 last revision and try to move to the node with the given node key.
*
* @author Johannes Lichtenberger
*
*/
public final class LastAxis
extends AbstractTemporalAxis {
/** Sirix {@link ResourceSession}. */
private final ResourceSession resourceSession;
/** Node key to lookup and retrieve. */
private final long nodeKey;
/** Determines if it's the first call. */
private boolean first;
/**
* Constructor.
*
* @param rtx Sirix {@link NodeReadOnlyTrx}
*/
public LastAxis(final ResourceSession resourceSession, final R rtx) {
this.resourceSession = requireNonNull(resourceSession);
nodeKey = rtx.getNodeKey();
first = true;
}
@Override
protected R computeNext() {
if (first) {
first = false;
final R rtx = resourceSession.beginNodeReadOnlyTrx(resourceSession.getMostRecentRevisionNumber());
if (rtx.moveTo(nodeKey)) {
return rtx;
} else {
rtx.close();
return endOfData();
}
} else {
return endOfData();
}
}
@Override
public ResourceSession getResourceManager() {
return resourceSession;
}
}