ch.lambdaj.function.argument.ArgumentsFactory Maven / Gradle / Ivy
// Modified or written by Ex Machina SAGL for inclusion with lambdaj.
// Copyright (c) 2009 Mario Fusco.
// Licensed under the Apache License, Version 2.0 (the "License")
package ch.lambdaj.function.argument;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.atomic.*;
import static ch.lambdaj.proxy.ProxyUtil.*;
/**
* An utility class of static factory methods that creates arguments and binds them with their placeholders
* @author Mario Fusco
*/
public final class ArgumentsFactory {
private ArgumentsFactory() { }
// ////////////////////////////////////////////////////////////////////////
// /// Factory
// ////////////////////////////////////////////////////////////////////////
/**
* Constructs a proxy object that mocks the given Class registering all the subsequent invocations on the object.
* @param clazz The class of the object to be mocked
* @return An object of the given class that register all the invocations made on it
*/
public static T createArgument(Class clazz) {
return createArgument(clazz, new InvocationSequence(clazz));
}
private static final Map PLACEHOLDER_BY_INVOCATION = new WeakHashMap();
@SuppressWarnings("unchecked")
static T createArgument(Class clazz, InvocationSequence invocationSequence) {
T placeholder = (T) PLACEHOLDER_BY_INVOCATION.get(invocationSequence);
boolean isNewPlaceholder = placeholder == null;
if (isNewPlaceholder) {
placeholder = (T)createPlaceholder(clazz, invocationSequence);
PLACEHOLDER_BY_INVOCATION.put(invocationSequence, placeholder);
}
if (isNewPlaceholder || isLimitedValues(placeholder))
bindArgument(placeholder, new Argument(invocationSequence));
return placeholder;
}
private static Object createPlaceholder(Class> clazz, InvocationSequence invocationSequence) {
return !Modifier.isFinal(clazz.getModifiers()) ?
createProxy(new ProxyArgument(clazz, invocationSequence), clazz, false) :
createArgumentPlaceholder(clazz);
}
// ////////////////////////////////////////////////////////////////////////
// /// Arguments
// ////////////////////////////////////////////////////////////////////////
private static final Map © 2015 - 2025 Weber Informatics LLC | Privacy Policy