Node.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.gecode.gist;
00026
00027 import java.util.*;
00028
00029
00030 public class Node {
00031
00032 public static final NodeStatus Undetermined = NodeStatus.UNDETERMINED;
00033
00034 private static final ArrayList emptyList = new ArrayList(0);
00035
00036 private Node parent;
00037 private Node nextSibling;
00038 private ArrayList children;
00039 private int nextChildIndex;
00040
00041 public Node() {
00042 this.parent = null;
00043 this.nextSibling = null;
00044 this.children = null;
00045 this.nextChildIndex = 0;
00046 }
00047
00048 public Node getParent() {
00049 return parent;
00050 }
00051
00052 public Node getNextSibling() {
00053 return nextSibling;
00054 }
00055
00056 public boolean isLastChild() {
00057 return (nextSibling == null);
00058 }
00059
00060 public void setNumberOfChildren(int numberOfChildren) {
00061 children = new ArrayList(numberOfChildren);
00062 for (int i = 0; i < numberOfChildren; i++) {
00063 children.add(null);
00064 }
00065 nextChildIndex = 0;
00066 }
00067
00068 public void setChildren(Collection theChildren) {
00069 children = new ArrayList(theChildren.size());
00070 nextChildIndex = 0;
00071 Node previousChild = null;
00072 Iterator theChildrenIterator = theChildren.iterator();
00073 while (theChildrenIterator.hasNext()) {
00074 Node nextChild = (Node) theChildrenIterator.next();
00075 nextChild.parent = this;
00076 if (previousChild != null) {
00077 previousChild.nextSibling = nextChild;
00078 }
00079 children.add(nextChild);
00080 previousChild = nextChild;
00081 nextChildIndex++;
00082 }
00083 }
00084
00085 public void setNextChild(int index, Node childNode) {
00086 children.set(index, childNode);
00087 childNode.parent = this;
00088 if (index > 0) {
00089 ((Node) children.get(index - 1)).nextSibling = childNode;
00090 }
00091
00092 }
00093
00094 public int getNumberOfChildren() {
00095 if (children == null) {
00096 return -1;
00097 } else {
00098 return children.size();
00099 }
00100 }
00101
00102 public Node getChild(int index) {
00103 return (Node) children.get(index);
00104 }
00105
00106 public Node getFirstChild() {
00107 return (Node) children.get(0);
00108 }
00109
00110 public Iterator childrenIterator() {
00111 if (children == null) {
00112 return emptyList.iterator();
00113 } else {
00114 return children.iterator();
00115 }
00116 }
00117
00118 private class ReversedChildrenIterator implements Iterator {
00119
00120 int currentIndex;
00121
00122 public ReversedChildrenIterator () {
00123 this.currentIndex = children.size() - 1;
00124 }
00125
00126 public boolean hasNext() {
00127 return (currentIndex >= 0);
00128 }
00129
00130 public Object next() {
00131 try {
00132 currentIndex--;
00133 return children.get(currentIndex + 1);
00134 } catch (IndexOutOfBoundsException e) {
00135 throw new NoSuchElementException();
00136 }
00137 }
00138
00139 public void remove() {
00140 throw new UnsupportedOperationException();
00141 }
00142
00143 }
00144
00145 public Iterator reversedChildrenIterator() {
00146 if (children == null) {
00147 return emptyList.iterator();
00148 } else {
00149 return new ReversedChildrenIterator();
00150 }
00151 }
00152
00153 public boolean isRoot() {
00154 return (parent == null);
00155 }
00156
00157 public int getDepth() {
00158 return isRoot() ? 1 : (parent.getDepth() + 1);
00159 }
00160 }