Provide a generic key element extraction higher-order function that works for

any element type at any index.
This commit is contained in:
Michael Bayne
2009-08-20 01:07:57 +00:00
parent b43ca1e203
commit 157d339906
+17 -6
View File
@@ -100,14 +100,25 @@ public class Key<T extends PersistentRecord> extends WhereClause
} }
/** /**
* Extracts an integer key from a record's {@link Key}. This should only be used on records * Returns a function that extracts an integer from a record's {@link Key}. This should only be
* whose primary key is a single integer. * used on records whose primary key is a single integer.
*/ */
public static <T extends PersistentRecord> Function<Key<T>,Integer> toInt () public static <T extends PersistentRecord> Function<Key<T>, Integer> toInt ()
{ {
return new Function<Key<T>,Integer>() { return extract(0);
public Integer apply (Key<T> key) { }
return (Integer)key.getValues()[0];
/**
* Returns a function that extracts an element key from a record's {@link Key}.
*
* @param index the index in the key of the element to be extracted.
*/
public static <T extends PersistentRecord, E> Function<Key<T>, E> extract (final int index)
{
return new Function<Key<T>, E>() {
public E apply (Key<T> key) {
@SuppressWarnings("unchecked") E value = (E)key.getValues()[index];
return value;
} }
}; };
} }