001: /* MultiConnector.java */
002:
003: package org.quilt.graph;
004:
005: /**
006: * A Connector holding a array of edges, where the array has at least
007: * one member. The first element of the array is preferred.
008: *
009: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
010: */
011:
012: public class MultiConnector extends Connector {
013:
014: /** Fixed-size array of directed edges. */
015: private Edge[] edges = null;
016:
017: /** Source of all edges in this connector. */
018: private Vertex source = null;
019:
020: /**
021: * Constructor for fixed-size array of edges. All edges in
022: * the new connector are copies of the seed edge.
023: */
024: public MultiConnector(Edge seed, int n) {
025: if (seed == null || n < 1) {
026: throw new IllegalArgumentException(
027: "constructor arguments null or not in range");
028: }
029: Vertex source = seed.getSource();
030: source = seed.getSource();
031: Vertex target = seed.getTarget();
032: edges = new Edge[n];
033:
034: edges[0] = new Edge(seed); // copy it
035: for (int i = 1; i < n; i++) {
036: edges[i] = new Edge(source, target);
037: }
038: }
039:
040: /**
041: * Constructor initialized from an existing UnaryConnector.
042: * The unary connector is destroyed after constructing the
043: * new connector.
044: */
045: public MultiConnector(Connector conn, int n) {
046: // will throw NPE if conn == null
047: this (conn.getEdge(), n);
048: }
049:
050: // INTERFACE CONNECTOR //////////////////////////////////////////
051: public Edge getEdge() {
052: return edges[0];
053: }
054:
055: public Vertex getTarget() {
056: return edges[0].getTarget();
057: }
058:
059: public void setTarget(Vertex v) {
060: checkTarget(v);
061: edges[0].setTarget(v);
062: }
063:
064: // OTHER METHODS ////////////////////////////////////////////////
065: private void checkTarget(final Vertex target) {
066: if (target == null) {
067: throw new IllegalArgumentException("target may not be null");
068: }
069: if (target.getGraph() != source.getGraph()) {
070: throw new IllegalArgumentException(
071: "new target must be in same graph");
072: }
073: }
074:
075: private void rangeCheck(int n) {
076: if (n < 0 || n >= edges.length) {
077: throw new IllegalArgumentException(
078: "MultiConnector index out of range");
079: }
080: }
081:
082: public Edge getEdge(int n) {
083: rangeCheck(n);
084: return edges[n];
085: }
086:
087: public Vertex getTarget(int n) {
088: rangeCheck(n);
089: return edges[n].getTarget();
090: }
091:
092: public void setTarget(Vertex v, int n) {
093: checkTarget(v);
094: rangeCheck(n);
095: edges[n].setTarget(v);
096: }
097:
098: /** @return The number of edges in the Connector. */
099: public int size() {
100: return edges.length;
101: }
102: }
|