1 /* MultiConnector.java */
2
3 package org.quilt.graph;
4
5 /***
6 * A Connector holding a array of edges, where the array has at least
7 * one member. The first element of the array is preferred.
8 *
9 * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
10 */
11
12 public class MultiConnector extends Connector {
13
14 /*** Fixed-size array of directed edges. */
15 private Edge [] edges = null;
16
17 /*** Source of all edges in this connector. */
18 private Vertex source = null;
19
20 /***
21 * Constructor for fixed-size array of edges. All edges in
22 * the new connector are copies of the seed edge.
23 */
24 public MultiConnector (Edge seed, int n) {
25 if ( seed == null || n < 1) {
26 throw new IllegalArgumentException(
27 "constructor arguments null or not in range");
28 }
29 Vertex source = seed.getSource();
30 source = seed.getSource();
31 Vertex target = seed.getTarget();
32 edges = new Edge[n];
33
34 edges[0] = new Edge (seed); // copy it
35 for (int i = 1; i < n; i++) {
36 edges[i] = new Edge (source, target);
37 }
38 }
39 /***
40 * Constructor initialized from an existing UnaryConnector.
41 * The unary connector is destroyed after constructing the
42 * new connector.
43 */
44 public MultiConnector( Connector conn, int n) {
45 // will throw NPE if conn == null
46 this( conn.getEdge(), n);
47 }
48 // INTERFACE CONNECTOR //////////////////////////////////////////
49 public Edge getEdge () {
50 return edges[0];
51 }
52 public Vertex getTarget () {
53 return edges[0].getTarget();
54 }
55 public void setTarget (Vertex v) {
56 checkTarget(v);
57 edges[0].setTarget(v);
58 }
59 // OTHER METHODS ////////////////////////////////////////////////
60 private void checkTarget (final Vertex target) {
61 if (target == null) {
62 throw new IllegalArgumentException("target may not be null");
63 }
64 if ( target.getGraph() != source.getGraph() ) {
65 throw new IllegalArgumentException(
66 "new target must be in same graph");
67 }
68 }
69 private void rangeCheck (int n) {
70 if ( n < 0 || n >= edges.length ) {
71 throw new IllegalArgumentException(
72 "MultiConnector index out of range");
73 }
74 }
75 public Edge getEdge (int n) {
76 rangeCheck(n);
77 return edges[n];
78 }
79 public Vertex getTarget (int n) {
80 rangeCheck(n);
81 return edges[n].getTarget();
82 }
83 public void setTarget (Vertex v, int n) {
84 checkTarget(v);
85 rangeCheck(n);
86 edges[n].setTarget(v);
87 }
88
89
90 /*** @return The number of edges in the Connector. */
91 public int size () {
92 return edges.length;
93 }
94 }
This page was automatically generated by Maven