package com.almworks.jira.structure.api.forest;
import com.almworks.integers.LongArray;
import com.almworks.integers.LongList;
import com.almworks.integers.util.LongListConcatenation;
import com.almworks.jira.structure.api.StructureManager;
import org.jetbrains.annotations.NotNull;
/**
* <p>A <code>ForestOp</code> represents a single operation on a forest, such as move or delete.
* A list of <code>ForestOp</code>s in the {@link ForestUpdate.Incremental} define a sequence
* of operations that bring a forest from one version to another, newer version.
* </p>
*
* <p>There are several types of the operations, all defined as the inner classes of <code>ForestOp:</code></p>
*
* <ul>
* <li>{@link ForestOp.Merge} - add and merge operation;</li>
* <li>{@link ForestOp.Delete} - delete operation;</li>
* <li>{@link ForestOp.Move} - move operation.</li>
* </ul>
*
* <p>Typically, a client code would inspect the exact type of each <code>ForestOp</code>
* and perform the required action to bring their forest to the up-to-date state.
* </p>
*
* @see ForestUpdate
* @see StructureManager#getForestUpdate
* @author Igor Sereda
*/
public abstract class ForestOp {
private final LongList myParents;
private final LongList myAncestors;
protected ForestOp(LongList parents, LongList ancestors) {
assert parents != null;
assert ancestors != null;
myParents = parents;
myAncestors = ancestors;
}
/**
* <p>This method returns the list of issues that were subject to the operation, or possibly
* the reduced set of these issues if other affected issues can be implied from the returned
* list and the current forest state.</p>
*
* <p>Namely, {@link ForestOp.Move} returns only the top
* issue in this list because all other issues moved can be figured
* from the state after the operation. If it wasn't the last operation then this
* holds true transitively via other operations.</p>
*
* @return a list of issues affected by this operation
*/
@NotNull
public abstract LongList getAffectedIssues();
/**
* @return a list of issues that are parents or used to be parents of the affected issues
*/
@NotNull
public LongList getParents() {
return myParents;
}
/**
* @return a list of all ancestors of the affected issues
*/
public LongList getAncestors() {
return myAncestors;
}
/**
* @return issues that are used to define this operation - used to check whether the operation
* is visible to a user
*/
@NotNull
public abstract LongList getAnchorIssues();
/**
* Represents move operation.
*
* @see ForestOp
* @see ForestAccessor#moveSubtree
*/
public static class Move extends ForestOp {
private final long myIssue;
private final long myUnder;
private final long myAfter;
private final LongArray myAnchor = new LongArray(3);
public Move(long issue, long under, long after, LongList parents, LongList ancestors) {
super(parents, ancestors);
myIssue = issue;
myUnder = under;
myAfter = after;
myAnchor.add(issue);
if (under > 0) myAnchor.add(under);
if (after > 0) myAnchor.add(after);
}
@NotNull
public LongList getAffectedIssues() {
return LongArray.create(myIssue);
}
@NotNull
public LongList getAnchorIssues() {
return myAnchor;
}
public long getUnder() {
return myUnder;
}
public long getAfter() {
return myAfter;
}
public long getIssue() {
return myIssue;
}
}
/**
* Represents merge or add operation (the latter is a primitive case of merge).
*
* @see ForestOp
* @see ForestAccessor#mergeForest
* @see ForestAccessor#addIssue
*/
public static class Merge extends ForestOp {
private final Forest myForest;
private final long myUnder;
private final long myAfter;
private final LongList myAnchor;
public Merge(Forest forest, long under, long after, LongList parents, LongList ancestors) {
super(parents, ancestors);
assert forest.size() > 0 : forest + " " + under + " " + after;
myForest = forest.makeImmutable();
myUnder = under;
myAfter = after;
if (myUnder > 0 || myAfter > 0) {
LongArray array = new LongArray();
if (myUnder > 0) array.add(myUnder);
if (myAfter > 0) array.add(myAfter);
myAnchor = new LongListConcatenation(array, myForest.getIssues());
} else {
myAnchor = myForest.getIssues();
}
}
@NotNull
public LongList getAffectedIssues() {
return myForest.getIssues();
}
@NotNull
public LongList getAnchorIssues() {
return myAnchor;
}
public Forest getMergedForest() {
return myForest;
}
public long getUnder() {
return myUnder;
}
public long getAfter() {
return myAfter;
}
}
/**
* Represents the delete operation.
*
* @see ForestOp
* @see ForestAccessor#removeSubtree
*/
public static class Delete extends ForestOp {
private final long myIssue;
private final Forest myRemoved;
public Delete(long issue, Forest removed, LongList parents, LongList ancestors) {
super(parents, ancestors);
myIssue = issue;
myRemoved = removed;
assert removed.isEmpty() || removed.getIssues().get(0) == issue : issue + " " + removed;
}
public long getIssue() {
return myIssue;
}
public Forest getRemovedForest() {
return myRemoved;
}
@NotNull
public LongList getAffectedIssues() {
return myRemoved.getIssues();
}
@NotNull
public LongList getAnchorIssues() {
return myRemoved.isEmpty() ? LongList.EMPTY : LongArray.create(myIssue);
}
}
}