Exemplo n.º 1
0
  private FigureMovableImpl createFigure(InputStream is) throws Exception {
    FigureType figureType = unmarshall(is);

    Map<String, Vertex> vertices = new HashMap<String, Vertex>();
    for (VertexType vertexType : figureType.getVertices()) {
      vertices.put(vertexType.getName(), createVertexFrom(vertexType));
    }

    Map<String, Edge> edges = new HashMap<String, Edge>();
    Map<Vertex, Set<Edge>> verticesEdges = new HashMap<Vertex, Set<Edge>>();
    for (EdgeType edgeType : figureType.getEdges()) {
      Edge edge = createEdgeFrom(edgeType, vertices);

      putEdgeIntoVerticesEdgesMap(verticesEdges, edge.getA(), edge);
      putEdgeIntoVerticesEdgesMap(verticesEdges, edge.getB(), edge);
      edges.put(edgeType.getName(), edge);
    }

    Map<String, Face> faces = new HashMap<String, Face>();
    for (FaceType faceType : figureType.getFaces()) {
      faces.put(faceType.getName(), createFaceFrom(faceType, edges, vertices, verticesEdges));
    }

    Double givenPrecision = figureType.getPrecision();
    double precision = (givenPrecision != null) ? givenPrecision : Pointable.PRECISION;

    return new FigureMovableImpl(
        new HashSet<Face>(faces.values()), new HashSet<Edge>(edges.values()), precision);
  }
Exemplo n.º 2
0
  private Face createFaceFrom(
      FaceType faceType,
      Map<String, Edge> edgesMap,
      Map<String, Vertex> vertices,
      Map<Vertex, Set<Edge>> verticesEdges) {
    Set<Edge> edges = new HashSet<Edge>();
    String edgesAttr = faceType.getEdges();
    String verticesAttr = faceType.getVertices();

    if (edgesAttr != null) {
      String[] arrEdges = edgesAttr.split(",");

      for (int i = 0; i < arrEdges.length; i++) {
        String adgeName = arrEdges[i].trim();
        Edge edge = edgesMap.get(adgeName);
        if (edge == null) {
          throw new IllegalArgumentException("No such edge defined - " + adgeName + ".");
        }
        edges.add(edge);
      }
    } else if (verticesAttr != null) {
      String[] arrVertexNames = verticesAttr.split(",");
      Vertex[] arrVertices = new Vertex[arrVertexNames.length];

      for (int i = 0; i < arrVertexNames.length; i++) {
        String vertexName = arrVertexNames[i].trim();
        Vertex vertex = vertices.get(vertexName);
        if (vertex == null) {
          throw new IllegalArgumentException("No such vertex defined - " + vertexName + ".");
        }
        arrVertices[i] = vertex;
      }

      for (int i = 0; i < arrVertices.length; i++) {
        Vertex v1 = arrVertices[i];

        for (int j = (i + 1); j < arrVertices.length; j++) {
          Vertex v2 = arrVertices[j];
          Set<Edge> edgeV1 = new HashSet<Edge>(verticesEdges.get(v1));
          edgeV1.retainAll(verticesEdges.get(v2));

          int size = edgeV1.size();
          if (size == 1) {
            edges.add(edgeV1.iterator().next());
          } else if (size > 1) {
            throw new IllegalArgumentException(
                "Between two vertices cannot be more than one edge.");
          }
        }
      }
    } else {
      throw new IllegalArgumentException(
          "Either 'edges' or 'vertices' attribute should be defined.");
    }
    return new Face(edges);
  }