Пример #1
0
  /**
   * The default constructor. It sets the edge's vertices based on the supplied starting and ending
   * vertices. The vertices should be different and must have their IDs set to a non-negative
   * integer. It registers the edge as a listener with the provided vertices.
   *
   * @param start
   *     <p>The first Vertex in this Edge.
   * @param end
   *     <p>The second Vertex in this Edge.
   */
  public Edge(Vertex start, Vertex end) {

    // The expected behavior is that if the supplied Vertices are invalid,
    // then the edge's default vertices become an empty list and its length
    // becomes -1.

    // The vertices are valid if:
    // they are non-null Vertex instances
    // they are not the same reference
    // there are exactly 2 Vertex instances

    // Initialize the defaults.
    this();

    // The supplied Vertices must not be null and must not be the same.
    if (start == null || end == null || start.getId() == end.getId()) {
      throw new IllegalArgumentException(
          "Edge error: start and end vertices must not be null and must not have the same ID.");
    }
    // Set the vertex IDs.
    vertices[0] = start;
    vertices[1] = end;

    // Update the length
    updateLength();

    // Register with the vertices.
    start.register(this);
    end.register(this);

    return;
  }
Пример #2
0
  /**
   * This constructor takes a collection of vertices. The collection should contain only two
   * different vertices. They must have their IDs set to a non-negative integer. It registers the
   * edge as a listener with the provided vertices.
   *
   * @param vertices
   *     <p>The two Vertices this Edge connects.
   */
  public Edge(ArrayList<Vertex> vertices) {

    // The expected behavior is that if the supplied ArrayList of Vertices
    // is invalid, then the edge's default vertices become an empty list and
    // its length becomes -1.

    // The vertices are valid if:
    // they are non-null Vertex instances
    // they are not the same reference
    // there are exactly 2 Vertex instances

    // Initialize the defaults.
    this();

    // The supplied ArrayList must not be null and must have two elements.
    if (vertices == null || vertices.size() != 2) {
      throw new IllegalArgumentException(
          "Edge error: The vertex ArrayList must not be null and must have 2 elements.");
    }
    // Make sure the vertices are not null and are not the same
    // reference.
    Vertex start = vertices.get(0);
    Vertex end = vertices.get(1);

    // The supplied Vertices must not be null and must not be the same.
    if (start == null || end == null || start.getId() == end.getId()) {
      throw new IllegalArgumentException(
          "Edge error: start and end vertices must not be null and must not have the same ID.");
    }
    // Set the vertex IDs.
    this.vertices[0] = start;
    this.vertices[1] = end;

    // Update the length
    updateLength();

    // Register with the vertices.
    start.register(this);
    end.register(this);

    return;
  }
Пример #3
0
  /** Updates the Edge if the Vertex's ID matches one of its stored Vertices. */
  @Override
  public void update(IVizUpdateable component) {

    // Check for null.
    if (component != null) {
      Vertex vertex = (Vertex) component;

      // Make sure the ID is valid.
      int id = vertex.getId();

      // See which vertex the component matches. For whichever one
      // matches, we need to update the reference, reset the length, and
      // notify edge listeners.
      for (int i = 0; i < 2; i++) {
        if (id == vertices[i].getId()) {

          updateLock.writeLock().lock();
          try {
            // Update the stored reference to the Vertex if its
            // ID matches.
            vertices[i] = vertex;
          } finally {
            updateLock.writeLock().unlock();
          }

          // Recompute the length.
          updateLength();

          // Notify listeners of the change.
          notifyListeners();

          // Break from the loop.
          i = 1;
        }
      }
    }

    return;
  }