001: /* ComplexConnector.java */
002:
003: package org.quilt.graph;
004:
005: /**
006: * A Connector holding a single edge plus a fixed size array of edges.
007: * This is a combination of the UnaryConnector and MultiConnector.
008: *
009: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
010: */
011: public class ComplexConnector extends Connector {
012:
013: /** The single edge */
014: private Edge edge;
015:
016: /** The array of edges. */
017: private Edge[] edges = null;
018:
019: /** Source of all edges in this connector. */
020: private Vertex source = null;
021:
022: /**
023: * Constructor for a Connector with a single edge plus a fixed-size
024: * array of edges. The source of the single edge becomes the source
025: * of the array of edges. All edges in the array are set to point
026: * to the graph exit.
027: *
028: * @param e Becomes preferred edge of the connector.
029: * @param n Number of edges in the array
030: * @param graph Graph this appears in.
031: */
032: public ComplexConnector(final Edge e, int n
033: //, final Exit exit
034: ) {
035: if (e == null || n < 1
036: // || exit == null
037: ) {
038: throw new IllegalArgumentException(
039: "constructor arguments must be in range and not null");
040: }
041: edge = new Edge(e); // the preferred edge
042: edges = new Edge[n]; // fixed-size array of edges
043: source = edge.getSource();
044: Vertex target = edge.getTarget();
045: for (int i = 0; i < n; i++) {
046: edges[i] = new Edge(source, target);
047: }
048: }
049:
050: public ComplexConnector(Connector conn, int n
051: //, final Exit exit
052: ) {
053: // will throw NPE if conn is null
054: this (conn.getEdge(), n
055: // , exit
056: );
057: }
058:
059: // INTERFACE CONNECTOR //////////////////////////////////////////
060: /** Get the single edge. */
061: public Edge getEdge() {
062: return edge;
063: }
064:
065: /** Get the target of the single edge. */
066: public Vertex getTarget() {
067: return edge.getTarget();
068: }
069:
070: /** Change the target of the single edge. */
071: public void setTarget(Vertex v) {
072: checkTarget(v);
073: edge.setTarget(v);
074: }
075:
076: // OTHER METHODS ////////////////////////////////////////////////
077: private void checkTarget(final Vertex target) {
078: if (target == null) {
079: throw new IllegalArgumentException("target may not be null");
080: }
081: if (source.getGraph() != target.getGraph()) {
082: throw new IllegalArgumentException(
083: "ComplexConnector's target must be in the same graph");
084: }
085: }
086:
087: private void rangeCheck(int n) {
088: if (n < 0 || n >= edges.length) {
089: throw new IllegalArgumentException(
090: "ComplexConnector index " + n + " out of range 0.."
091: + (edges.length - 1));
092: }
093: }
094:
095: /** Get the Nth edge from the array. */
096: public Edge getEdge(int n) {
097: rangeCheck(n);
098: return edges[n];
099: }
100:
101: /** Get the target of the Nth edge. */
102: public Vertex getTarget(int n) {
103: rangeCheck(n);
104: return edges[n].getTarget();
105: }
106:
107: /** Change the target of the Nth edge. */
108: public void setTarget(Vertex v, int n) {
109: checkTarget(v);
110: rangeCheck(n);
111: edges[n].setTarget(v);
112: }
113:
114: /**
115: * Returns the number of edges in the connector EXCLUDING the
116: * preferred edge. This is the same number used in the
117: * constructor as the size of the connector.
118: *
119: * @return Total number of edges in the multiway part of the connector.
120: */
121: public int size() {
122: return edges.length;
123: }
124: }
|