/** * Adds the given entity with the given priority. * * @param priority the priority to use for this element. * @param entity the entity added to the collector. */ public void add(int priority, T entity) { data.add(new ComparableTuple<Integer, T>(priority, entity)); }
@SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (!(obj instanceof ComparableTuple<?, ?>)) { return false; } return compareTo((ComparableTuple<F, S>) obj) == 0; }
/** * Adds a column to the key * * @param pos the position to add at * @param field the field or column to add */ public void addColumn(int pos, String field) { keyFields.add(ComparableTuple.create(pos, field)); Collections.sort(keyFields); }
/** * Returns the columns that make up the key. * * @return the columns of the key */ public List<String> getColumns() { List<String> columns = Lists.newArrayList(); for (ComparableTuple<Integer, String> field : keyFields) { columns.add(field.getSecond()); } return columns; }
/** * Returns the columns matched in the referenced table. * * @return the columns matched in the referenced table */ public List<String> getForeignColumns() { List<String> columns = Lists.newArrayList(); for (ComparableTuple<Integer, String> field : foreignKeyFields) { columns.add(field.getSecond()); } return columns; }
/** * Creates a new tuple without any values. * * @param <F> the type for the first value * @param <S> the type for the second value * @return the newly created tuple */ @Nonnull public static <F extends Comparable<F>, S> ComparableTuple<F, S> createTuple() { return new ComparableTuple<>(null, null); }
/** * Adds a foreign column which has to be matched be the local ones. * * @param pos the position to add at * @param field the field or column to add */ public void addForeignColumn(int pos, String field) { foreignKeyFields.add(ComparableTuple.create(pos, field)); Collections.sort(foreignKeyFields); } }
/** * Returns the columns that make up the key. * * @return the columns of the key */ public List<String> getColumns() { List<String> columns = new ArrayList<>(); for (ComparableTuple<Integer, String> field : keyFields) { columns.add(field.getSecond()); } return columns; }
/** * Adds the given entity with the default priority. * * @param entity the entity added to the collector. * @see #DEFAULT_PRIORITY */ public void addDefault(T entity) { data.add(new ComparableTuple<Integer, T>(DEFAULT_PRIORITY, entity)); }
/** * Adds a column to the key * * @param pos the position to add at * @param field the field or column to add */ public void addColumn(int pos, String field) { keyFields.add(ComparableTuple.create(pos, field)); Collections.sort(keyFields); }
/** * Creates a new tuple by only specifying the first value of the tuple. * <p> * The second value will remain <tt>null</tt>. * * @param first defines the first value of the tuple * @param <F> the type for the first value * @param <S> the type for the second value * @return the newly created tuple */ @Nonnull public static <F extends Comparable<F>, S> ComparableTuple<F, S> create(@Nullable F first) { return new ComparableTuple<>(first, null); }
private void fillPK(Connection c, Table table) throws SQLException { ResultSet rs = c.getMetaData().getPrimaryKeys(c.getSchema(), null, table.getName()); List<ComparableTuple<Integer, String>> keyFields = new ArrayList<>(); while (rs.next()) { keyFields.add(ComparableTuple.create(rs.getInt(COLUMN_KEY_SEQ), rs.getString(COLUMN_COLUMN_NAME))); } Collections.sort(keyFields); for (Tuple<Integer, String> key : keyFields) { table.getPrimaryKey().add(key.getSecond()); } rs.close(); }
/** * Creates a new tuple with the given values. * * @param first defines the first value of the tuple * @param second defines the second value of the tuple * @param <F> the type for the first value * @param <S> the type for the second value * @return the newly created tuple */ @Nonnull public static <F extends Comparable<F>, S> ComparableTuple<F, S> create(@Nullable F first, @Nullable S second) { return new ComparableTuple<>(first, second); }
/** * Converts a map into a list of tuples. * * @param map the map to be converted * @param <K> the key type of the map and therefore the type of the first component of the tuples * @param <V> the value type of the map and therefore the type of the second component of the tuples * @return a list of tuples, containing one tuple per map entry where the first component is the key, * and the second component is the value of the map entry. */ public static <K extends Comparable<K>, V> List<ComparableTuple<K, V>> fromComparableMap(@Nonnull Map<K, V> map) { List<ComparableTuple<K, V>> result = new ArrayList<>(map.size()); for (Map.Entry<K, V> e : map.entrySet()) { result.add(new ComparableTuple<K, V>(e.getKey(), e.getValue())); } return result; }