Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
jAgg is a Java 5.0 API that supports “group by” operations on Lists of Java objects: aggregate operations such as count, sum, max, min, avg, and many more. It also allows custom aggregate operations.
package net.sf.jagg.util;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jagg.exception.PropertyAccessException;
import net.sf.jagg.model.ChainedMethodCall;
import net.sf.jagg.model.MethodCall;
import net.sf.jagg.model.SelfMethodCall;
import net.sf.jagg.Aggregator;
import net.sf.jagg.util.PropertyParser;
/**
* Created as a wrapper around a HashMap that maps class/property
* combinations to MethodCalls. Method calls with best matches
* for argument types are found. Property names, e.g. "property", are mapped
* to "getProperty()" or "isProperty()" methods if found.
*
* @author Randy Gettman
* @since 0.1.0
*/
public class MethodCache
{
private static final boolean DEBUG = false;
private static MethodCache theMethodCache = null;
private final Map myMethods;
/**
* Private constructor for the singleton pattern.
*/
private MethodCache()
{
myMethods = new HashMap();
}
/**
* Returns the singleton MethodCache.
* @return The singleton MethodCache.
*/
public static MethodCache getMethodCache()
{
if (theMethodCache == null)
{
theMethodCache = new MethodCache();
}
return theMethodCache;
}
/**
* Clears the MethodCache.
* @since 0.8.0
*/
public void clear()
{
myMethods.clear();
}
/**
* Gets a specific MethodCall from the cache, or finds it using
* reflection if it does not exist. Invokes the MethodCall and
* returns the value.
*
* @param value The object on which to lookup a property value.
* @param property The property or method name plus signature to lookup.
* @return The object's property value.
* @throws PropertyAccessException If there was a problem accessing the
* property on the value data.
*/
public Object getValueFromProperty(Object value, String property)
throws PropertyAccessException
{
// Pseudo-property indicating to use the object itself, instead of a
// property of the object.
if (Aggregator.PROP_SELF.equals(property))
return value;
ChainedMethodCall methodCall = getMethodCallFromProperty(value, property);
return methodCall.invoke(value);
}
/**
* Gets a specific MethodCall from the cache, or creates it by
* finding the Method using reflection if it does not exist.
*
* @param value The object on which to lookup a property value.
* @param property The property or method name plus signature to lookup.
* @return A MethodCall.
* @throws PropertyAccessException If a suitable Method couldn't
* be found.
* @since 0.5.0
*/
public ChainedMethodCall getMethodCallFromProperty(Object value, String property)
throws PropertyAccessException
{
ChainedMethodCall methodCall;
MethodDescriptor lookup = new MethodDescriptor(value.getClass(), property);
Object[] parameterArray;
// Synchronize access to the cache so multiple Threads don't "find"
// the same Method.
synchronized (myMethods)
{
methodCall = myMethods.get(lookup);
// If not in cache...
if (methodCall == null)
{
if (Aggregator.PROP_SELF.equals(property))
{
// Get a SelfMethodCall.
methodCall = new SelfMethodCall(value);
}
else
{
// Get a MethodCall.
PropertyParser parser = new PropertyParser(property);
Method method;
methodCall = null;
ChainedMethodCall prevMethodCall = null;
ChainedMethodCall currMethodCall;
Class> currValueClass = value.getClass();
do
{
parser.parse();
if (parser.isMethod())
{
// Method with possible parameters.
String methodName = parser.getPropertyName();
if (DEBUG)
System.err.println("Method name: \"" + methodName + "\".");
List