001package daikon.diff;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005import java.util.List;
006import org.checkerframework.checker.nullness.qual.Nullable;
007import org.checkerframework.dataflow.qual.Pure;
008import org.plumelib.util.IPair;
009
010/**
011 * All nodes must subclass this class.
012 *
013 * @param <CONTENT> half of the type of the objects stored in this node, which are {@code
014 *     IPair<CONTENT,CONTENT>}
015 * @param <CHILD> the type of the children; it is is ignored if there are no children
016 */
017public abstract class Node<CONTENT extends @Nullable Object, CHILD> {
018
019  /** The children of this node. */
020  private List<CHILD> children = new ArrayList<>();
021
022  /** Nonsensical for RootNode. */
023  private IPair<CONTENT, CONTENT> userObject;
024
025  /**
026   * Creates a new Node.
027   *
028   * @param userObject the user object
029   */
030  protected Node(IPair<CONTENT, CONTENT> userObject) {
031    this.userObject = userObject;
032  }
033
034  protected Node(CONTENT left, CONTENT right) {
035    this.userObject = IPair.of(left, right);
036  }
037
038  public void add(CHILD newChild) {
039    children.add(newChild);
040  }
041
042  public Iterator<CHILD> children() {
043    return children.iterator();
044  }
045
046  /**
047   * Returns the user object pair.
048   *
049   * @return the user object pair
050   */
051  public IPair<CONTENT, CONTENT> getUserObject() {
052    return userObject;
053  }
054
055  /**
056   * Returns the first element of the user object pair.
057   *
058   * @return the first element of the user object pair
059   */
060  @Pure
061  public CONTENT getUserLeft() {
062    return userObject.first;
063  }
064
065  /**
066   * Returns the second element of the user object pair.
067   *
068   * @return the second element of the user object pair
069   */
070  @Pure
071  public CONTENT getUserRight() {
072    return userObject.second;
073  }
074
075  public abstract void accept(Visitor v);
076}