From 157d339906ff4b9fae59a04479e93022229ef778 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 20 Aug 2009 01:07:57 +0000 Subject: [PATCH] Provide a generic key element extraction higher-order function that works for any element type at any index. --- src/java/com/samskivert/depot/Key.java | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/java/com/samskivert/depot/Key.java b/src/java/com/samskivert/depot/Key.java index 818ced6..7da1ba4 100644 --- a/src/java/com/samskivert/depot/Key.java +++ b/src/java/com/samskivert/depot/Key.java @@ -100,14 +100,25 @@ public class Key extends WhereClause } /** - * Extracts an integer key from a record's {@link Key}. This should only be used on records - * whose primary key is a single integer. + * Returns a function that extracts an integer from a record's {@link Key}. This should only be + * used on records whose primary key is a single integer. */ - public static Function,Integer> toInt () + public static Function, Integer> toInt () { - return new Function,Integer>() { - public Integer apply (Key key) { - return (Integer)key.getValues()[0]; + return extract(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 Function, E> extract (final int index) + { + return new Function, E>() { + public E apply (Key key) { + @SuppressWarnings("unchecked") E value = (E)key.getValues()[index]; + return value; } }; }