[flaptor-util] r204 committed - Added documentation to FunctionUtils...

0 views
Skip to first unread message

codesite...@google.com

unread,
Oct 14, 2009, 2:18:16 PM10/14/09
to flaptor-o...@googlegroups.com
Revision: 204
Author: san...@santip.com.ar
Date: Wed Oct 14 11:17:36 2009
Log: Added documentation to FunctionUtils

Added a method to wrap functions caching its return value.
http://code.google.com/p/flaptor-util/source/detail?r=204

Modified:
/trunk/src/com/flaptor/util/FunctionUtils.java

=======================================
--- /trunk/src/com/flaptor/util/FunctionUtils.java Thu May 14 10:32:36 2009
+++ /trunk/src/com/flaptor/util/FunctionUtils.java Wed Oct 14 11:17:36 2009
@@ -2,11 +2,21 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.Map;

import com.google.common.base.Function;
-
+import com.google.common.collect.Maps;
+
+/**
+ * Utility methods for the {@link Function} class.
+ *
+ * @author santip
+ */
public class FunctionUtils {

+ /**
+ * @return a function that will return the toString of its parameter
+ */
public static Function<Object, String> getToString() {
return new Function<Object, String>() {
public String apply(Object from) {
@@ -15,10 +25,18 @@
};
}

+ /**
+ * @return a function that will invoke the static valueOf method of
the given type on
+ * its String parameter and will cast the return value to the given
type.
+ */
public static <T> Function<String, T> getValueOf(final Class<T> type) {
return getStaticMethod(type, String.class, type, "valueOf");
}

+ /**
+ * @return a function that will invoke a static method using the
function argument as
+ * a parameter and will cast the return value to the given toType
+ */
public static <F, T> Function<F, T> getStaticMethod(Class<?> type,
Class<F> fromType, final Class<T> toType, String methodName) {
try {
final Method method = type.getMethod(methodName, fromType);
@@ -42,14 +60,25 @@
}
}

+ /**
+ * invokes the given method to the given target using reflection.
+
+ * @return the value returned by the invoked method.
+ */
public static Object invokeMethod(Object target, String method) {
return getMethodInvoker(method).apply(target);
}

+ /**
+ * @return a function that invokes the given method using reflection.
+ */
public static Function<Object, Object> getMethodInvoker(final String
methodName) {
return getMethodInvoker(Object.class, methodName);
}

+ /**
+ * @return a function that invokes the given method using reflection
and will cast it to the given return type.
+ */
public static <T> Function<Object, T> getMethodInvoker(final Class<T>
returnType, final String methodName) {
return new Function<Object, T>() {
public T apply(Object from) {
@@ -71,6 +100,25 @@
}
};
}
+
+ /**
+ * Wraps the given function in such a way that it will call the
function only
+ * once for each different key and cache the return value for future
requests.
+ *
+ * @param function the function to be cached.
+ * @return a wrapper function that will cache the inner function's
return values
+ */
+ public static <K,V> Function<K,V> cachedFunction(final Function<K, V>
function) {
+ return new Function<K, V>() {
+ Map<K, V> cache = Maps.newHashMap();
+ public V apply(K key) {
+ if (!cache.containsKey(key)) {
+ cache.put(key, function.apply(key));
+ }
+ return cache.get(key);
+ }
+ };
+ }


}

Reply all
Reply to author
Forward
0 new messages