Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.invocation;
 
 
 import java.util.List;
 
 
 import static com.gh.bmd.jrt.common.Reflection.findConstructor;

Utility class for creating invocation factory objects.

Created by davide on 2/12/15.

 
 public class Invocations {

    
Avoid direct instantiation.
 
     protected Invocations() {
 
     }

    
Builds and returns a new invocation factory creating instances of the specified class.

Note that inner and anonymous classes can be passed as well. Remember however that Java creates synthetic constructor for such classes, so be sure to specify the correct arguments to guarantee proper instantiation (see withArgs(java.lang.Object[])). In fact, inner classes always have the outer instance as first constructor parameter, and anonymous classes has both the outer instance and all the variables captured in the closure.

Parameters:
invocationClass the invocation class.
<INPUT> the input data type.
<OUTPUT> the output data type.
Returns:
the invocation factory.
Throws:
java.lang.IllegalArgumentException if no constructor taking the specified objects as parameters was found.
 
     @Nonnull
     public static <INPUT, OUTPUT> InvocationFactory<INPUT, OUTPUT> factoryOf(
             @Nonnull final Class<? extends Invocation<INPUT, OUTPUT>> invocationClass) {
 
         return new DefaultInvocationFactory<INPUT, OUTPUT>(findConstructor(invocationClass),
                                                            .);
     }

    
Builds and returns a new invocation factory creating instances of the specified class token.

Note that class tokens of inner and anonymous class can be passed as well. Remember however that Java creates synthetic constructor for such classes, so be sure to specify the correct arguments to guarantee proper instantiation (see withArgs(java.lang.Object[])). In fact, inner classes always have the outer instance as first constructor parameter, and anonymous classes has both the outer instance and all the variables captured in the closure.

Parameters:
invocationToken the invocation class token.
<INPUT> the input data type.
<OUTPUT> the output data type.
Returns:
the invocation factory.
Throws:
java.lang.IllegalArgumentException if no constructor taking the specified objects as parameters was found.
 
     @Nonnull
     public static <INPUT, OUTPUT> InvocationFactory<INPUT, OUTPUT> factoryOf(
             @Nonnull final ClassToken<? extends Invocation<INPUT, OUTPUT>> invocationToken) {
 
         return factoryOf(invocationToken.getRawClass());
     }

    
Creates and returns a factory builder passing the specified arguments to the invocation constructor.
Note that, in case no constructor taking the specified arguments as parameters is found, an exception will be thrown.

Parameters:
args the constructor arguments.
Returns:
the factory builder.
 
     @Nonnull
    public static FactoryBuilder withArgs(@Nullable final Object... args) {
        return new FactoryBuilder(args);
    }

    
Interface defining a function taking no parameters.

Parameters:
<OUTPUT> the result data type.
    public interface Function0<OUTPUT> {

        
Calls this function.

Returns:
the result.
        OUTPUT call();
    }

    
Interface defining a function taking a single parameter.

Parameters:
<INPUT1> the first parameter type.
<OUTPUT> the result data type.
    public interface Function1<INPUT1, OUTPUT> {

        
Calls this function.

Parameters:
param1 the first parameter.
Returns:
the result.
        OUTPUT call(INPUT1 param1);
    }

    
Interface defining a function taking two parameters.

Parameters:
<INPUT1> the first parameter type.
<INPUT2> the second parameter type.
<OUTPUT> the result data type.
    public interface Function2<INPUT1, INPUT2, OUTPUT> {

        
Calls this function.

Parameters:
param1 the first parameter.
param2 the second parameter.
Returns:
the result.
        OUTPUT call(INPUT1 param1, INPUT2 param2);
    }

    
Interface defining a function taking three parameters.

Parameters:
<INPUT1> the first parameter type.
<INPUT2> the second parameter type.
<INPUT3> the third parameter type.
<OUTPUT> the result data type.
    public interface Function3<INPUT1, INPUT2, INPUT3, OUTPUT> {

        
Calls this function.

Parameters:
param1 the first parameter.
param2 the second parameter.
param3 the third parameter.
Returns:
the result.
        OUTPUT call(INPUT1 param1, INPUT2 param2, INPUT3 param3);
    }

    
Interface defining a function taking four parameters.

Parameters:
<INPUT1> the first parameter type.
<INPUT2> the second parameter type.
<INPUT3> the third parameter type.
<INPUT4> the fourth parameter type.
<OUTPUT> the result data type.
    public interface Function4<INPUT1, INPUT2, INPUT3, INPUT4, OUTPUT> {

        
Calls this function.

Parameters:
param1 the first parameter.
param2 the second parameter.
param3 the third parameter.
param4 the fourth parameter.
Returns:
the result.
        OUTPUT call(INPUT1 param1, INPUT2 param2, INPUT3 param3, INPUT4 param4);
    }

    
Interface defining a function taking an undefined number of parameters.

Parameters:
<PARAM> the parameters type.
<OUTPUT> the result data type.
    public interface FunctionN<PARAM, OUTPUT> {

        
Calls this function.

Parameters:
params the list of parameters.
Returns:
the result.
        OUTPUT call(@Nonnull final List<? extends PARAM> params);
    }

    
Implementation of a builder of invocation factories.
    public static class FactoryBuilder {
        private final Object[] mArgs;

        
Constructor.

Parameters:
args the invocation constructor arguments.
        private FactoryBuilder(@Nullable final Object[] args) {
             = (args == null) ? . : args;
        }

        
Builds and returns a new invocation factory creating instances of the specified class token.

Note that class tokens of inner and anonymous class can be passed as well. Remember however that Java creates synthetic constructor for such classes, so be sure to specify the correct arguments to guarantee proper instantiation. In fact, inner classes always have the outer instance as first constructor parameter, and anonymous classes has both the outer instance and all the variables captured in the closure.

Parameters:
invocationToken the invocation class token.
<INPUT> the input data type.
<OUTPUT> the output data type.
Returns:
the invocation factory.
Throws:
java.lang.IllegalArgumentException if no constructor taking the specified objects as parameters was found.
        @Nonnull
        public <INPUT, OUTPUT> InvocationFactory<INPUT, OUTPUT> factoryOf(
                @Nonnull final ClassToken<? extends Invocation<INPUT, OUTPUT>> invocationToken) {
            return factoryOf(invocationToken.getRawClass());
        }

        
Builds and returns a new invocation factory creating instances of the specified class.

Note that inner and anonymous classes can be passed as well. Remember however that Java creates synthetic constructor for such classes, so be sure to specify the correct arguments to guarantee proper instantiation. In fact, inner classes always have the outer instance as first constructor parameter, and anonymous classes has both the outer instance and all the variables captured in the closure.

Parameters:
invocationClass the invocation class.
<INPUT> the input data type.
<OUTPUT> the output data type.
Returns:
the invocation factory.
Throws:
java.lang.IllegalArgumentException if no constructor taking the specified objects as parameters was found.
        @Nonnull
        public <INPUT, OUTPUT> InvocationFactory<INPUT, OUTPUT> factoryOf(
                @Nonnull final Class<? extends Invocation<INPUT, OUTPUT>> invocationClass) {
            final Object[] args = ;
            return new DefaultInvocationFactory<INPUT, OUTPUT>(
                    findConstructor(invocationClassargs), args);
        }
    }

    
Default implementation of an invocation factory.

Parameters:
<INPUT> the input data type.
<OUTPUT> the output data type.
    private static class DefaultInvocationFactory<INPUT, OUTPUT>
            implements InvocationFactory<INPUT, OUTPUT> {
        private final Object[] mArgs;
        private Constructor<? extends Invocation<INPUT, OUTPUT>> mConstructor;

        
Constructor.

Parameters:
constructor the invocation constructor.
args the invocation constructor arguments.
        private DefaultInvocationFactory(
                @Nonnull final Constructor<? extends Invocation<INPUT, OUTPUT>> constructor,
                @Nonnull final Object[] args) {
             = constructor;
             = args;
        }
        @Nonnull
        public Invocation<INPUT, OUTPUT> newInvocation() {
            try {
                return .newInstance();
            } catch (final RoutineException e) {
                throw e;
            } catch (final Throwable t) {
                throw new InvocationException(t);
            }
        }
    }
New to GrepCode? Check out our FAQ X