Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call function with different combinations

Tags:

java

I have three functions test1(), test2() and test3() written in java. I want to write a some code that will iterates and call them into all different combinations.

e.g

  • First iteration execute test1(), then test2() and finally test3(),
  • Second iteration execute test2(), test1() and finally test3() e.t.c.

Is there a possible way to do this through an iterator or I have to do it manually?

like image 469
salvador Avatar asked Dec 15 '22 04:12

salvador


1 Answers

You could use reflection to get an array of methods and then go through each permutation of the list and invoke them one at a time. Take a look at the JavaDoc for Class.getDeclaredMethods(). Here is a quick example:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class SO {
  public void test1() {
        System.out.println("Running test1");
  }
  public void test2() {
        System.out.println("Running test2");
  }
  public void test3() {
        System.out.println("Running test3");
  }
  public void notATest() {
        System.err.println("THIS IS NOT A TEST");
  }

  public static void main(String ... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Class c = SO.class;
    SO that = new SO();
    Method[] methods = filter(c.getDeclaredMethods(), "test");
    PermuteMethod permutations = new PermuteMethod(methods);
    while(permutations.hasNext()) {
      for(Method permutation: permutations.next()) {
        permutation.invoke(that, null);
      }
      System.out.println("----");
    }
  }
  private static Method[] filter(Method[] declaredMethods, String startsWith) {
    List<Method> filtered = new ArrayList<>();
    for(Method method : declaredMethods) {
      if(method.getName().startsWith(startsWith)) {
        filtered.add(method);
      }
    }
    return filtered.toArray(new Method[filtered.size()]);
  }
}

This produces the follwoung output (not that it never calls the notATest() method:

Running test1
Running test2
Running test3
----
Running test1
Running test3
Running test2
----
Running test2
Running test1
Running test3
----
Running test2
Running test3
Running test1
----
Running test3
Running test1
Running test2
----
Running test3
Running test2
Running test1
----

This is using the following (modified) version of this permute class:

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class PermuteMethod implements Iterator<Method[]> {
  private final int size;
  private final Method[] elements; // copy of original 0 .. size-1
  private final Method[] ar; // array for output, 0 .. size-1
  private final int[] permutation; // perm of nums 1..size, perm[0]=0

  private boolean next = true;

  public PermuteMethod(Method[] e) {
    size = e.length;
    elements = new Method[size];
    System.arraycopy(e, 0, elements, 0, size);
    ar = new Method[size];
    System.arraycopy(e, 0, ar, 0, size);
    permutation = new int[size + 1];
    for (int i = 0; i < size + 1; i++) {
      permutation[i] = i;
    }
  }

  private void formNextPermutation() {
    for (int i = 0; i < size; i++) {
      Array.set(ar, i, elements[permutation[i + 1] - 1]);
    }
  }

  public boolean hasNext() {
    return next;
  }

  public void remove() throws UnsupportedOperationException {
    throw new UnsupportedOperationException();
  }

  private void swap(final int i, final int j) {
    final int x = permutation[i];
    permutation[i] = permutation[j];
    permutation[j] = x;
  }

  public Method[] next() throws NoSuchElementException {
    formNextPermutation(); // copy original elements
    int i = size - 1;
    while (permutation[i] > permutation[i + 1])
      i--;
    if (i == 0) {
      next = false;
      for (int j = 0; j < size + 1; j++) {
        permutation[j] = j;
      }
      return ar;
    }
    int j = size;
    while (permutation[i] > permutation[j])
      j--;
    swap(i, j);
    int r = size;
    int s = i + 1;
    while (r > s) {
      swap(r, s);
      r--;
      s++;
    }
    return ar;
  }
}
like image 130
Jason Sperske Avatar answered Dec 25 '22 17:12

Jason Sperske