Ejemplo n.º 1
0
  public void cartesianProductOrUnionIfSameHeaders(Table t) {

    int t1Cols = this.getColumnsCount();
    int t2Cols = t.getColumnsCount();

    if (t2Cols == 0) return;
    else if (t1Cols == 0) {

      if (this.headers == null) this.headers = new ArrayList<Attribute>();

      for (Attribute att : t.getHeaders()) this.headers.add(new Attribute(att));

      if (this.values == null) this.values = new ArrayList<List<String>>();

      if (t.getValues() != null)
        for (List<String> v : t.getValues()) if (v != null) values.add(new ArrayList<String>(v));
    } else {

      if (sameHeaders(t)) {
        List<Table> tables = new ArrayList<Table>();
        tables.add(this);
        tables.add(t);
        Table result = union(tables);
        this.setHeaders(result.getHeaders());
        this.setValues(result.getValues());
        return;
      }

      for (Attribute att : t.getHeaders()) headers.add(new Attribute(att));

      int t1Rows = this.getRowsCount();
      int t2Rows = t.getRowsCount();
      int totalRows = t1Rows == 0 || t2Rows == 0 ? t1Rows + t2Rows : t1Rows * t2Rows;

      List<List<String>> values = new ArrayList<List<String>>();

      for (int i = 0; i < totalRows; i++) {

        List<String> row = new ArrayList<String>();

        for (int j = 0; j < t1Cols; j++) {
          int index = t1Rows == 0 ? -1 : i % t1Rows;
          if (index == -1 || this.values == null || this.values.get(index) == null) row.add(null);
          else row.add(this.values.get(index).get(j));
        }
        for (int j = 0; j < t2Cols; j++) {
          int index = t2Rows == 0 ? -1 : i % t2Rows;
          if (index == -1 || t.getValues() == null || t.getValues().get(index) == null)
            row.add(null);
          else row.add(t.getValues().get(index).get(j));
        }

        values.add(row);
      }

      this.values = values;
    }
  }