Beispiel #1
0
  /**
   * This operation returns a clone of the Polygon using a deep copy.
   *
   * @return
   *     <p>The new clone.
   */
  @Override
  public Object clone() {

    // Initialize a new object.
    Polygon object = new Polygon();

    // Copy the contents from this one.
    object.copy(this);

    // Return the newly instantiated object.
    return object;
  }
Beispiel #2
0
  /**
   * This operation copies the contents of a Polygon into the current object using a deep copy.
   *
   * @param polygon
   *     <p>The Object from which the values should be copied.
   */
  public void copy(Polygon polygon) {

    // Check the parameters.
    if (polygon == null) {
      return;
    }
    // Copy the super's data.
    super.copy(polygon);

    // Deep copy the vertices.
    vertices.clear();
    for (Vertex vertex : polygon.getVertices()) {
      vertices.add((Vertex) vertex.clone());
    }

    // Deep copy the edges.
    edges.clear();
    ArrayList<Edge> otherEdges = polygon.getEdges();
    int size = otherEdges.size();
    for (int i = 0; i < size; i++) {
      // Clone the edge. This deep copies the other edge's vertices, so we
      // must hook the clone up to our vertex instances.
      Edge clone = (Edge) otherEdges.get(i).clone();

      // Add the clone to the list of edges.
      edges.add(clone);

      // Get the new start and end vertices for this edge.
      Vertex start = vertices.get(i);
      Vertex end = vertices.get((i + 1) % size);

      // Register the clone with the start and end.
      start.register(clone);
      end.register(clone);

      // Send the start and end vertices to the clone.
      clone.update(start);
      clone.update(end);
    }

    /* ---- Deep copy the edge properties. ---- */
    // Unregister from any of the current boundary conditions.
    for (Entry<Integer, EdgeProperties> entry : edgeProperties.entrySet()) {
      EdgeProperties properties = entry.getValue();
      // Unregister from the fluid and thermal boundary conditions.
      properties.getFluidBoundaryCondition().unregister(this);
      properties.getThermalBoundaryCondition().unregister(this);
      // Unregister from all passive scalar boundary conditions.
      for (BoundaryCondition condition : properties.getOtherBoundaryConditions()) {
        condition.unregister(this);
      }
    }

    // Clone all of the edge properties from the other polygon and register
    // with their boundary conditions.
    for (Entry<Integer, EdgeProperties> entry : polygon.edgeProperties.entrySet()) {
      // Clone and add the edge's properties.
      EdgeProperties properties = (EdgeProperties) entry.getValue().clone();
      edgeProperties.put(entry.getKey(), properties);

      // Unregister from the fluid and thermal boundary conditions.
      properties.getFluidBoundaryCondition().register(this);
      properties.getThermalBoundaryCondition().register(this);
      // Unregister from all passive scalar boundary conditions.
      for (BoundaryCondition condition : properties.getOtherBoundaryConditions()) {
        condition.register(this);
      }
    }
    /* ---------------------------------------- */

    // Copy the PolygonProperties
    polygonProperties = (PolygonProperties) polygon.getPolygonProperties().clone();

    return;
  }