zhake
zhake

Reputation: 29

Call different methods depending on input parameters

Please give me an advice and example how I can implement this situation when I should call different methods up to incoming params with Map.

public String mainMethod(Int count) {
    if (count == 1) { return myMethod }
    else if (count == 2) { return myMethod2 }
    else if (count == 3) { return myMethod3 }
}

public String myMethod1() {
    ..............  
} 

public String myMethod2() {
    ..............  
} 

public String myMethod3() {
    ..............  
} 

Upvotes: 2

Views: 3502

Answers (6)

OlgaMaciaszek
OlgaMaciaszek

Reputation: 3912

The solution with adding callables to a map looks good, but now with Java 8 it can be simplified like so:

Map<Integer, Callable> map = new HashMap<>();
map.put(1, () -> null);
map.put(2, () -> 33);
map.put(3, () -> 66);

or if you want to use other methods defined in the class:

Map<Integer, Callable> map = new HashMap<>();
map.put(1, ClassName::myMethod1);
map.put(2, ClassName::myMethod2);
map.put(3, ClassName::myMethod3);

And then call the method same as in the previous solution: map.get(count).call();

Upvotes: 0

sid
sid

Reputation: 98

You can also use reflection for the same.

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class InvokeMethod {

    /**
     * Map of input integer to method name to be invoked.
     */
    private final static Map<Integer, String> methods = new HashMap<>();
    static {
        methods.put(1, "method1");
        methods.put(2, "method2");
        methods.put(3, "method3");
    }

    /**
     * @param count Method identifier.
     */
    public static void mainMethod(int count) {
        String methodName = methods.get(count);
        Method method = null;

        try {
            // Check the javadoc of the following method to see how to
            // handle multiple arguments
            method = InvokeMethod.class.getDeclaredMethod(methodName, int.class);
        } catch (NoSuchMethodException e) {
            System.out.println("ERROR: Method not found: " + methodName);
            System.exit(1);
        } catch (SecurityException e) {
            System.out.println("ERROR: Unable to access the method: " + methodName);
            System.exit(1);
        }

        try {
            // Invoking statically, so passing null.
            // Else the class instance needs to be passed.
            // Go through the javadoc of the following method to see
            // how to handle multiple arguments.
            method.invoke(null, 5);
        } catch (InvocationTargetException | IllegalAccessException e) {
            System.out.println("ERROR: Unable to access the method: " + methodName);
            System.exit(1);
        } catch (IllegalArgumentException e) {
            System.out.println("ERROR: Incorrect arguments passed: " + methodName);
            System.exit(1);
        }
    }

    /**
     * Prints the number as is.
     * @param in Input integer.
     */
    public static void method1(int in) {
        System.out.println(in);
    }

    /**
     * Doubles the number and prints it.
     * @param in Input integer.
     */
    public static void method2(int in) {
        System.out.println(in * 2);
    }

    /**
     * Squares the number and prints it.
     * @param in Input integer.
     */
    public static void method3(int in) {
        System.out.println(in * in);
    }
}

Upvotes: 0

Maxim
Maxim

Reputation: 339

public abstract class MethodImplementation
{
    private static final Map<Integer, SomeInterface>    IMPLEMENTATION_MAP;

    static
    {
        IMPLEMENTATION_MAP  = new HashMap<>();

        IMPLEMENTATION_MAP.put( 1, new Implementation1() );
        IMPLEMENTATION_MAP.put( 2, new Implementation2() );
        IMPLEMENTATION_MAP.put( 3, new Implementation3() );

    }

    public static interface SomeInterface
    {
        public String Method();

    }

    public static String Selector( int count_ )
    {
        String          result          = null;
        SomeInterface   implementation  = IMPLEMENTATION_MAP.get( count_ );

        if( null != implementation )
        {
            result  = implementation.Method();

        }

        return result;

    }

    public static class Implementation1 implements SomeInterface
    {
        @Override
        public String Method()
        {
            String  result  = "Method1";

            return result;

        }

    }

    public static class Implementation2 implements SomeInterface
    {
        @Override
        public String Method()
        {
            String  result  = "Method2";

            return result;

        }

    }

    public static class Implementation3 implements SomeInterface
    {
        @Override
        public String Method()
        {
            String  result  = "Method3";

            return result;

        }

    }

    private MethodImplementation()
    {

    }

}

usage:

String  res = MethodImplementation.Selector( i );

Upvotes: 0

Pshemo
Pshemo

Reputation: 124225

What was mentioned in question you pointed out in your comment was something like this:

Map<Integer, Callable> map = new HashMap<>();
map.put(1, new Callable() {
    public Object call() throws Exception {
        //code for argument 1
        return null;
    }
});
map.put(2, new Callable() {
    public Object call() throws Exception {
        //code for argument 2
        return null;
    }
});
map.put(3, new Callable() {
    public Object call() throws Exception {
        //code for argument 3
        return null;
    }
});

Now you can use it as

map.get(count).call();

Upvotes: 3

Smutje
Smutje

Reputation: 18143

You need a static Map<Integer, Callable> filled with anonymous classes for every possible integer input and then return MAP.get(count).call();.

Upvotes: 0

mbroshi
mbroshi

Reputation: 979

You should use an enum with descriptive names of the different method calls, and make the myMethod's private. For example, something like:

public enum Method { GOOD, BAD, UGLY; }

public String mainMethod(Method method) {
    switch (method) {
    case GOOD: return myMethod1();
    case BAD:  return myMethod2();
    case UGLY: return myMethod3();
    }
}

private String myMethod1() { ... };
private String myMethod2() { ... };
private String myMethod3() { ... };

Upvotes: 1

Related Questions