Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Return a new Table whose columns are COLUMNNAMES, selected from pairs of
- * rows from this table and from TABLE2 that match on all columns with
- * identical names and satisfy CONDITIONS.
- */
- Table select(Table table2, List<String> columnNames,
- List<Condition> conditions) {
- Table result = new Table(columnNames);
- ArrayList<String> commonNames =
- new ArrayList<String>(this.columnTitles);
- commonNames.retainAll(table2.columnTitles);
- ArrayList<Column> common1 = new ArrayList<Column>();
- ArrayList<Column> common2 = new ArrayList<Column>();
- for (String name : commonNames) {
- common1.add(new Column(name, this));
- common2.add(new Column(name, table2));
- }
- ArrayList<Column> outCols = new ArrayList<Column>();
- for (String colName : columnNames) {
- outCols.add(new Column(colName, this, table2));
- }
- for (Row rowT1 : this._rows) {
- for (Row rowT2 : table2._rows) {
- if (equijoin(common1, common2, rowT1, rowT2)) {
- if (testConditions(conditions,
- new Row[] { rowT1, rowT2 })) {
- Row newRow = new Row(outCols, rowT1, rowT2);
- result.add(newRow);
- }
- }
- }
- }
- return result;
- }
- /**
- * Return true if the columns COMMON1 from ROW1 and COMMON2 from ROW2 all
- * have identical values. Assumes that COMMON1 and COMMON2 have the same
- * number of elements and the same names, that the columns in COMMON1 apply
- * to this table, those in COMMON2 to another, and that ROW1 and ROW2 come,
- * respectively, from those tables.
- */
- private static boolean equijoin(List<Column> common1, List<Column> common2,
- Row row1, Row row2) {
- for (int i = 0; i < common1.size(); i++) {
- String v1 = common1.get(i).getFrom(row1);
- String v2 = common2.get(i).getFrom(row2);
- if (!v1.equals(v2)) {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement