void setClosed(Object closed) { this.closed = closed; if (isClosedOnFreeVar()) freeVarsNumber++; }
private boolean checkCurriedOnClosed(Object curried, int position) { if (position != 1 || !isClosedOnFreeVar()) return false; checkClosedType(curried); closed = curried; freeVarsNumber--; return true; }
/** * 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; }
private boolean checkParams(Object... vars) { if (vars == null || vars.length == 0) { if (freeVarsNumber != 0) throw new WrongClosureInvocationException("Closure invoked without vars instead of the expected " + freeVarsNumber); if (curriedVars == null && argsList == null) return true; } if (freeVarsNumber != vars.length) throw new WrongClosureInvocationException("Closure invoked with " + vars.length + " vars instead of the expected " + freeVarsNumber); if (isClosedOnFreeVar()) checkClosedType(vars[0]); return false; }
private List<Object[]> bindParams(Object... vars) throws WrongClosureInvocationException { if (checkParams(vars)) return null; int varCounter = isClosedOnFreeVar() ? 1 : 0; int curriedParamCounter = 0; List<Object[]> boundParams = new ArrayList<Object[]>();