001: /* BinaryConnector.java */
002:
003: package org.quilt.graph;
004:
005: /**
006: * A Connector holding two edges. Implementation detail: we assume
007: * that in laying out bytecode the 'else' code appears immediately
008: * after the if but the 'then' code is accessed using a goto.
009: *
010: * @author <a href="mailto:jddixon@users.sourceforge.net">Jim Dixon</a>
011: */
012: public class BinaryConnector extends Connector {
013: /** The preferred edge, the 'else' edge. */
014: private Edge edge;
015: /** The other edge, the 'then' edge. */
016: private Edge otherEdge;
017: /** Source of both edges. */
018: private Vertex source = null;
019:
020: private void doSetUp(Edge e, Edge otherE) {
021: if (e == null || otherE == null) {
022: throw new IllegalArgumentException(
023: "arguments cannot be null");
024: }
025: edge = e;
026: source = edge.getSource();
027: otherEdge = otherE;
028: if (source != otherEdge.getSource()) {
029: throw new IllegalArgumentException(
030: "edges in a BinaryConnector must have the same source");
031: }
032: }
033:
034: public BinaryConnector(Edge e, Edge otherE) {
035: doSetUp(e, otherE);
036: }
037:
038: public BinaryConnector(Connector conn, Edge otherE) {
039: if (conn == null) {
040: throw new IllegalArgumentException(
041: "connector cannot be null");
042: }
043: doSetUp(conn.getEdge(), otherE);
044: }
045:
046: // INTERFACE CONNECTOR //////////////////////////////////////////
047: /** Get the preferred edge. */
048: public Edge getEdge() {
049: return edge;
050: }
051:
052: /** Get the target of the preferred edge. */
053: public Vertex getTarget() {
054: return edge.getTarget();
055: }
056:
057: /** Change the target of the preferred edge. */
058: public void setTarget(Vertex v) {
059: checkTarget(v);
060: edge.setTarget(v);
061: }
062:
063: public int size() {
064: return 2;
065: }
066:
067: // OTHER METHODS ////////////////////////////////////////////////
068: private void checkTarget(final Vertex target) {
069: if (target == null) {
070: throw new IllegalArgumentException("target may not be null");
071: }
072: // DEBUG
073: if (source == null)
074: System.out
075: .println("BinaryConnector.checkTarget INTERNAL ERROR: "
076: + " source is null");
077: else if (source.getGraph() == null)
078: System.out
079: .println("BinaryConnector.checkTarget: source has no graph!");
080: if (target.getGraph() == null)
081: System.out
082: .println("BinaryConnector.checkTarget: target has no graph!");
083: // END
084: if (target.getGraph() != source.getGraph()) {
085: throw new IllegalArgumentException(
086: "new target must be in same graph");
087: }
088: }
089:
090: /** Get the other edge. */
091: public Edge getOtherEdge() {
092: return otherEdge;
093: }
094:
095: /** Get the target of the other edge */
096: public Vertex getOtherTarget() {
097: return otherEdge.getTarget();
098: }
099:
100: /** Set the target of the other edge. */
101: public void setOtherTarget(Vertex v) {
102: checkTarget(v);
103: otherEdge.setTarget(v);
104: }
105: }
|