Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. /**
  2.      * Return a new Table whose columns are COLUMNNAMES, selected from pairs of
  3.      * rows from this table and from TABLE2 that match on all columns with
  4.      * identical names and satisfy CONDITIONS.
  5.      */
  6.     Table select(Table table2, List<String> columnNames,
  7.             List<Condition> conditions) {
  8.         Table result = new Table(columnNames);
  9.  
  10.         ArrayList<String> commonNames =
  11.             new ArrayList<String>(this.columnTitles);
  12.         commonNames.retainAll(table2.columnTitles);
  13.  
  14.         ArrayList<Column> common1 = new ArrayList<Column>();
  15.         ArrayList<Column> common2 = new ArrayList<Column>();
  16.         for (String name : commonNames) {
  17.             common1.add(new Column(name, this));
  18.             common2.add(new Column(name, table2));
  19.  
  20.         }
  21.  
  22.         ArrayList<Column> outCols = new ArrayList<Column>();
  23.         for (String colName : columnNames) {
  24.             outCols.add(new Column(colName, this, table2));
  25.         }
  26.  
  27.         for (Row rowT1 : this._rows) {
  28.             for (Row rowT2 : table2._rows) {
  29.                 if (equijoin(common1, common2, rowT1, rowT2)) {
  30.                     if (testConditions(conditions,
  31.                         new Row[] { rowT1, rowT2 })) {
  32.                         Row newRow = new Row(outCols, rowT1, rowT2);
  33.                         result.add(newRow);
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.  
  39.         return result;
  40.     }
  41.  
  42.     /**
  43.      * Return true if the columns COMMON1 from ROW1 and COMMON2 from ROW2 all
  44.      * have identical values. Assumes that COMMON1 and COMMON2 have the same
  45.      * number of elements and the same names, that the columns in COMMON1 apply
  46.      * to this table, those in COMMON2 to another, and that ROW1 and ROW2 come,
  47.      * respectively, from those tables.
  48.      */
  49.     private static boolean equijoin(List<Column> common1, List<Column> common2,
  50.             Row row1, Row row2) {
  51.  
  52.         for (int i = 0; i < common1.size(); i++) {
  53.             String v1 = common1.get(i).getFrom(row1);
  54.             String v2 = common2.get(i).getFrom(row2);
  55.             if (!v1.equals(v2)) {
  56.                 return false;
  57.             }
  58.         }
  59.         return true;
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement