All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sirix.axis.temporal.PreviousAxis 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 previous revision and try to move to the node with the given node key.
 *
 * @author Johannes Lichtenberger
 *
 */
public final class PreviousAxis
    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 PreviousAxis(final ResourceSession resourceSession, final R rtx) {
    this.resourceSession = requireNonNull(resourceSession);
    nodeKey = rtx.getNodeKey();
    revision = rtx.getRevisionNumber() - 1;
    first = true;
  }

  @Override
  protected R computeNext() {
    if (revision > 0 && first) {
      first = false;
      final R rtx = resourceSession.beginNodeReadOnlyTrx(revision);
      if (rtx.moveTo(nodeKey)) {
        return rtx;
      } else {
        rtx.close();
        return endOfData();
      }
    } else {
      return endOfData();
    }
  }

  @Override
  public ResourceSession getResourceManager() {
    return resourceSession;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy