/** * Invokes this closure once by applying the given set of variables to it. * @param vars The set of variables used to invoke this closure once * @return The result of the closure invocation * @throws WrongClosureInvocationException if the number of the passed variables doesn't correspond to one * with which this closure has been defined */ Object closeOne(Object... vars) throws WrongClosureInvocationException { if (invokables.isEmpty()) { unhandeledInvocations.add(vars); return null; } List<Object[]> boundParams = bindParams(vars); Object result = isClosedOnFreeVar() ? vars[0] : closed; Iterator<Object[]> argsIterator = boundParams != null ? boundParams.iterator() : null; for (Invokable invokable : invokables) { result = invokable.invoke(result, argsIterator != null ? argsIterator.next() : null); } return result; }
/** * Defines the method invoked by this closure. * @param closedObject The object on which the closure has to be invoked. It can be a fixed object or a Class. * In this last case, if the method is not static, it is treated as it was an * unbound argument defined through the {@link ch.lambdaj.Lambda#var(Class)} method * @param methodName The name of the method invoked by this closure or {@link AbstractClosure#CONSTRUCTOR} * if you want to call a constructor * @param args The arguments used to invoke this closure. They can be a mixed of fixed value and * unbound one defined through the {@link ch.lambdaj.Lambda#var(Class)} method * @return The closure itself */ protected AbstractClosure of(Object closedObject, String methodName, Object ... args) { Class<?> closedClass = closedObject instanceof Class<?> ? (Class<?>)closedObject : closedObject.getClass(); Invokable invokable = methodName.equals(CONSTRUCTOR) ? new InvokableConstructor(findConstructor(closedClass, args)) : new InvokableMethod(findMethod(closedClass, methodName, args)); bindInvocation(invokable, args); setClosed(invokable.isStatic() ? null : closedObject); return this; }