00001 /* 00002 * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien 00003 * 00004 * This file is part of jdiagengine. 00005 * 00006 * jdiagengine is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * jdiagengine is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * You should have received a copy of the GNU General Public License 00016 * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>. 00017 * 00018 * Authors: Joerg Weber, Franz Wotawa 00019 * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at 00020 * 00021 */ 00022 00023 00024 package utils; 00025 00026 import java.util.*; 00027 00036 public class DoubleLinkedDAGNode implements Comparable, Cloneable { 00037 00038 /* 00039 * Set by DoubleLinkedDAG only. 00040 */ 00041 public int id = -1; 00042 00043 /* 00044 * Set by DoubleLinkedDAG only. 00045 */ 00046 public ArrayList allNodes; 00047 00048 /* 00049 * The integers in the list are indexes to allNodes. 00050 */ 00051 protected SortedIntList parents; 00052 00053 /* 00054 * The integers in the list are indexes to allNodes. 00055 */ 00056 protected SortedIntList children; 00057 00058 00059 /* 00060 * Creates an "empty" node. 00061 */ 00062 public DoubleLinkedDAGNode() { 00063 parents = new SortedIntList(); 00064 children = new SortedIntList(); 00065 } 00066 00067 /* 00068 * The new node is a (deep) clone of n. 00069 */ 00070 public DoubleLinkedDAGNode(DoubleLinkedDAGNode n) { 00071 id = n.id; 00072 allNodes = n.allNodes; 00073 parents = (SortedIntList)n.parents.clone(); 00074 children = (SortedIntList)n.children.clone(); 00075 } 00076 00077 /* 00078 * Returns a (deep) clone of the node. 00079 */ 00080 public Object clone() { 00081 DoubleLinkedDAGNode clonedN = new DoubleLinkedDAGNode(this); 00082 return clonedN; 00083 } 00084 00085 /* 00086 * Compares the id fields. 00087 */ 00088 public int compareTo(Object other) { 00089 00090 DoubleLinkedDAGNode on = (DoubleLinkedDAGNode)other; 00091 if (id < on.id) return -1; 00092 else if (id == on.id) return 0; 00093 else return +1; 00094 } 00095 00096 public final int getID() { 00097 return id; 00098 } 00099 00100 /*public void setID(int id) { 00101 this.id = id; 00102 } 00103 00104 public void setAllNodes(ArrayList allNodes) { 00105 this.allNodes = allNodes; 00106 }*/ 00107 00108 00109 public final SortedIntList getParents() { 00110 return parents; 00111 } 00112 00113 public final SortedIntList getChildren() { 00114 return children; 00115 } 00116 00117 public final boolean hasParents() { 00118 return (parents.size() > 0); 00119 } 00120 00121 public final boolean hasChildren() { 00122 return (children.size() > 0); 00123 } 00124 00125 /* 00126 * Returns an iterator whose elements are nodes. 00127 */ 00128 public final Iterator getParentsIterator() { 00129 return new NodeIterator(parents); 00130 } 00131 00132 /* 00133 * Returns an iterator whose elements are nodes. 00134 */ 00135 public final Iterator getChildrenIterator() { 00136 return new NodeIterator(children); 00137 } 00138 00139 public DoubleLinkedDAGNode getFirstParent() { 00140 assert(hasParents()); 00141 00142 return (DoubleLinkedDAGNode)allNodes.get(parents.getFirstInt()); 00143 } 00144 00145 public String toString() { 00146 return Integer.toString(id); 00147 } 00148 00149 /* 00150 * Iterates through node objects. 00151 */ 00152 protected class NodeIterator implements Iterator { 00153 00154 Iterator silIterator; 00155 00156 NodeIterator(SortedIntList nodeIDs) { 00157 silIterator = nodeIDs.iterator(); 00158 } 00159 00160 public boolean hasNext() { 00161 return (silIterator.hasNext()); 00162 } 00163 00164 /* 00165 * Returns a DoubleLinkedDAGNode instance. 00166 */ 00167 public Object next() { 00168 Integer iobj = (Integer)silIterator.next(); 00169 return allNodes.get(iobj.intValue()); 00170 } 00171 00172 public void remove() { 00173 throw new UnsupportedOperationException(); 00174 } 00175 00176 } 00177 }