groovy.lang.TrampolineClosure Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spotless-ext-greclipse Show documentation
Show all versions of spotless-ext-greclipse Show documentation
Groovy Eclipse's formatter bundled for Spotless
The newest version!
/*
* Copyright 2003-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.lang;
/**
* A TrampolineClosure wraps a closure that needs to be executed on a functional trampoline.
* Upon calling, a TrampolineClosure will call the original closure waiting for its result.
* If the outcome of the call is another instance of a TrampolineClosure, created perhaps as a result to a call to the TrampolineClosure.trampoline()
* method, the TrampolineClosure will again be invoked. This repetitive invocation of returned TrampolineClosure instances will continue
* until a value other than TrampolineClosure is returned.
* That value will become the final result of the trampoline.
*
* @author Vaclav Pech
*/
final class TrampolineClosure extends Closure {
private final Closure original;
TrampolineClosure(final Closure original) {
super(original.getOwner(), original.getDelegate());
this.original = original;
}
/**
* Delegates to the wrapped closure
*/
@Override
public int getMaximumNumberOfParameters() {
return original.maximumNumberOfParameters;
}
/**
* Delegates to the wrapped closure
*/
@Override
public Class[] getParameterTypes() {
return original.parameterTypes;
}
/**
* Starts the trampoline loop and calls the wrapped closure as the first step.
* @return The final result of the trampoline
*/
@Override
public V call() {
return loop(original.call());
}
/**
* Starts the trampoline loop and calls the wrapped closure as the first step.
* @return The final result of the trampoline
*/
@Override
public V call(final Object arguments) {
return loop(original.call(arguments));
}
/**
* Starts the trampoline loop and calls the wrapped closure as the first step.
* @return The final result of the trampoline
*/
@Override
public V call(final Object... args) {
return loop(original.call(args));
}
private V loop(final Object lastResult) {
Object result = lastResult;
for (;;) {
if (result instanceof TrampolineClosure) {
result = ((TrampolineClosure)result).original.call();
} else return (V) result;
}
}
/**
* Builds a trampolined variant of the current closure.
* @param args Parameters to curry to the underlying closure.
* @return An instance of TrampolineClosure wrapping the original closure after currying.
*/
@Override
public Closure trampoline(final Object... args) {
return new TrampolineClosure(original.curry(args));
}
/**
* Returns itself, since it is a good enough trampolined variant of the current closure.
* @return An instance of TrampolineClosure wrapping the original closure.
*/
@Override
public Closure trampoline() {
return this;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy