All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
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.
com.gh.bmd.jrt.core.FunctionRoutineBuilder Maven / Gradle / Ivy
/*
* 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 com.gh.bmd.jrt.core;
import com.gh.bmd.jrt.builder.RoutineBuilder;
import com.gh.bmd.jrt.builder.RoutineConfiguration;
import com.gh.bmd.jrt.builder.RoutineConfiguration.OrderType;
import com.gh.bmd.jrt.channel.ResultChannel;
import com.gh.bmd.jrt.invocation.Invocation;
import com.gh.bmd.jrt.invocation.InvocationFactory;
import com.gh.bmd.jrt.invocation.Invocations.Function0;
import com.gh.bmd.jrt.invocation.Invocations.Function1;
import com.gh.bmd.jrt.invocation.Invocations.Function2;
import com.gh.bmd.jrt.invocation.Invocations.Function3;
import com.gh.bmd.jrt.invocation.Invocations.Function4;
import com.gh.bmd.jrt.invocation.Invocations.FunctionN;
import com.gh.bmd.jrt.invocation.SingleCallInvocation;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static com.gh.bmd.jrt.builder.RoutineConfiguration.withInputOrder;
/**
* Class implementing a builder of routine objects based on function or procedure instances.
*
* Created by davide on 9/21/14.
*
* @param the input data type.
* @param the output data type.
*/
class FunctionRoutineBuilder extends DefaultRoutineBuilder {
/**
* Constructor.
*
* @param factory the invocation factory.
* @throws java.lang.NullPointerException if the factory is null.
*/
private FunctionRoutineBuilder(@Nonnull final InvocationFactory factory) {
super(factory);
super.withConfiguration(withInputOrder(OrderType.PASSING_ORDER));
}
/**
* Returns a new builder based on the specified function.
*
* Note that the function object must be stateless in order to avoid concurrency issues.
*
* @param function the function instance.
* @param the output data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified function is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromFunction(
@Nonnull final Function0 extends OUTPUT> function) {
if (function == null) {
throw new NullPointerException("the function must not be null");
}
return new FunctionRoutineBuilder(new InvocationFactory() {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation() {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends Void> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 0) {
throw new IllegalArgumentException(
"[" + function.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 0");
}
result.pass(function.call());
}
};
}
});
}
/**
* Returns a new builder based on the specified function.
*
* Note that the function object must be stateless in order to avoid concurrency issues.
*
* @param function the function instance.
* @param the input data type.
* @param the output data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified function is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromFunction(
@Nonnull final Function1 function) {
if (function == null) {
throw new NullPointerException("the function must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 1) {
throw new IllegalArgumentException(
"[" + function.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 1");
}
result.pass(function.call((INPUT1) inputs.get(0)));
}
};
}
});
}
/**
* Returns a new builder based on the specified function.
*
* Note that the function object must be stateless in order to avoid concurrency issues.
*
* @param function the function instance.
* @param the input data type.
* @param the first parameter type.
* @param the second parameter type.
* @param the output data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified function is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static
FunctionRoutineBuilder fromFunction(
@Nonnull final Function2 function) {
if (function == null) {
throw new NullPointerException("the function must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 2) {
throw new IllegalArgumentException(
"[" + function.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 2");
}
result.pass(function.call((INPUT1) inputs.get(0), (INPUT2) inputs.get(1)));
}
};
}
});
}
/**
* Returns a new builder based on the specified function.
*
* Note that the function object must be stateless in order to avoid concurrency issues.
*
* @param function the function instance.
* @param the input data type.
* @param the first parameter type.
* @param the second parameter type.
* @param the third parameter type.
* @param the output data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified function is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static
FunctionRoutineBuilder fromFunction(
@Nonnull final Function3 function) {
if (function == null) {
throw new NullPointerException("the function must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 3) {
throw new IllegalArgumentException(
"[" + function.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 3");
}
result.pass(function.call((INPUT1) inputs.get(0), (INPUT2) inputs.get(1),
(INPUT3) inputs.get(2)));
}
};
}
});
}
/**
* Returns a new builder based on the specified function.
*
* Note that the function object must be stateless in order to avoid concurrency issues.
*
* @param function the function instance.
* @param the input data type.
* @param the first parameter type.
* @param the second parameter type.
* @param the third parameter type.
* @param the fourth parameter type.
* @param the output data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified function is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromFunction(
@Nonnull final Function4 function) {
if (function == null) {
throw new NullPointerException("the function must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 4) {
throw new IllegalArgumentException(
"[" + function.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 4");
}
result.pass(function.call((INPUT1) inputs.get(0), (INPUT2) inputs.get(1),
(INPUT3) inputs.get(2), (INPUT4) inputs.get(3)));
}
};
}
});
}
/**
* Returns a new builder based on the specified function.
*
* Note that the function object must be stateless in order to avoid concurrency issues.
*
* @param function the function instance.
* @param the input data type.
* @param the output data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified function is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromFunction(
@Nonnull final FunctionN function) {
if (function == null) {
throw new NullPointerException("the function must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
result.pass(function.call(inputs));
}
};
}
});
}
/**
* Returns a new builder based on the specified procedure.
* The procedure output will be discarded.
*
* Note that the procedure object must be stateless in order to avoid concurrency issues.
*
* @param procedure the procedure instance.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified procedure is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromProcedure(
@Nonnull final Function0 procedure) {
if (procedure == null) {
throw new NullPointerException("the procedure must not be null");
}
return new FunctionRoutineBuilder(new InvocationFactory() {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation() {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends Void> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 0) {
throw new IllegalArgumentException(
"[" + procedure.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 0");
}
procedure.call();
}
};
}
});
}
/**
* Returns a new builder based on the specified procedure.
* The procedure output will be discarded.
*
* Note that the procedure object must be stateless in order to avoid concurrency issues.
*
* @param procedure the procedure instance.
* @param the input data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified procedure is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromProcedure(
@Nonnull final Function1 procedure) {
if (procedure == null) {
throw new NullPointerException("the procedure must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 1) {
throw new IllegalArgumentException(
"[" + procedure.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 1");
}
procedure.call((INPUT1) inputs.get(0));
}
};
}
});
}
/**
* Returns a new builder based on the specified procedure.
* The procedure output will be discarded.
*
* Note that the procedure object must be stateless in order to avoid concurrency issues.
*
* @param procedure the procedure instance.
* @param the input data type.
* @param the first parameter type.
* @param the second parameter type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified procedure is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromProcedure(
@Nonnull final Function2 procedure) {
if (procedure == null) {
throw new NullPointerException("the procedure must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 2) {
throw new IllegalArgumentException(
"[" + procedure.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 2");
}
procedure.call((INPUT1) inputs.get(0), (INPUT2) inputs.get(1));
}
};
}
});
}
/**
* Returns a new builder based on the specified procedure.
* The procedure output will be discarded.
*
* Note that the procedure object must be stateless in order to avoid concurrency issues.
*
* @param procedure the procedure instance.
* @param the input data type.
* @param the first parameter type.
* @param the second parameter type.
* @param the third parameter type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified procedure is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static
FunctionRoutineBuilder fromProcedure(
@Nonnull final Function3 procedure) {
if (procedure == null) {
throw new NullPointerException("the procedure must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 3) {
throw new IllegalArgumentException(
"[" + procedure.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 3");
}
procedure.call((INPUT1) inputs.get(0), (INPUT2) inputs.get(1),
(INPUT3) inputs.get(2));
}
};
}
});
}
/**
* Returns a new builder based on the specified procedure.
* The procedure output will be discarded.
*
* Note that the procedure object must be stateless in order to avoid concurrency issues.
*
* @param procedure the procedure instance.
* @param the input data type.
* @param the first parameter type.
* @param the second parameter type.
* @param the third parameter type.
* @param the fourth parameter type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified procedure is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromProcedure(
@Nonnull final Function4 procedure) {
if (procedure == null) {
throw new NullPointerException("the procedure must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
if (inputs.size() != 4) {
throw new IllegalArgumentException(
"[" + procedure.getClass().getCanonicalName() +
"] wrong number of input parameters: was "
+ inputs.size() + " while expected 4");
}
procedure.call((INPUT1) inputs.get(0), (INPUT2) inputs.get(1),
(INPUT3) inputs.get(2), (INPUT4) inputs.get(3));
}
};
}
});
}
/**
* Returns a new builder based on the specified procedure.
* The procedure output will be discarded.
*
* Note that the procedure object must be stateless in order to avoid concurrency issues.
*
* @param procedure the procedure instance.
* @param the input data type.
* @return the builder instance.
* @throws java.lang.NullPointerException if the specified procedure is null.
*/
@Nonnull
@SuppressWarnings("ConstantConditions")
static FunctionRoutineBuilder fromProcedure(
@Nonnull final FunctionN procedure) {
if (procedure == null) {
throw new NullPointerException("the procedure must not be null");
}
return new FunctionRoutineBuilder (new InvocationFactory () {
@Nonnull
public Invocation newInvocation() {
return new SingleCallInvocation () {
@Override
@SuppressWarnings("unchecked")
public void onCall(@Nonnull final List extends INPUT> inputs,
@Nonnull final ResultChannel result) {
procedure.call(inputs);
}
};
}
});
}
@Nonnull
@Override
public RoutineBuilder withConfiguration(
@Nullable final RoutineConfiguration configuration) {
return super.withConfiguration(RoutineConfiguration.notNull(configuration)
.builderFrom()
.withInputOrder(OrderType.PASSING_ORDER)
.buildConfiguration());
}
}