1

I'm doing a little method and i need to pass a method as a parameter, so i don't have to repeat code. So i have to use this method and the only thing that changes is the method i use inside this method. So, if i could pass a method in a parameter, it would simplify a lot my code. here's my code. can i use java's refletct?

public static void testsForLinkedLists(int potencia,
            int repeticoesteste, int somarAoArray, String ficheiroExcel,
            int validararray, Method pushMethod)

And i have this two methods that i want to use it as parameters

public class measuring_tests {
    public static double timeToPushLinkedStack(int intendedPushes) {
        final LinkedStackOfStrings measuringTimeToPush = new LinkedStackOfStrings();
        final String element = "measuring_test";
        int numberOfPushesDone = 0;
        double totalPushTime = 0;
        Stopwatch stopwatch = new Stopwatch();

        while (numberOfPushesDone < intendedPushes) {

            measuringTimeToPush.push(element);

            numberOfPushesDone++;

        }
        totalPushTime = stopwatch.elapsedTime();

        while (measuringTimeToPush.size > 0) {
            measuringTimeToPush.pop();
        }

        return totalPushTime;

    }

    public static double timeToPopLinkedStack(int intendedPops) {

        final LinkedStackOfStrings measuringTimeToPop = new LinkedStackOfStrings();
        final String element = "measuring_test";

        while (measuringTimeToPop.size < intendedPops) {
            measuringTimeToPop.push(element);
        }

        double totalPopTime = 0;
        Stopwatch stopwatch = new Stopwatch();

        while (measuringTimeToPop.size > 0) {
            measuringTimeToPop.pop();
        }

        totalPopTime = stopwatch.elapsedTime();

        return totalPopTime;
    }
Dantex
  • 53
  • 1
  • 1
  • 7
  • Do all the methods that you want to pass have the same signature? What is it? – assylias Apr 12 '17 at 14:42
  • i have like 4 or 5 methods. two methods for push's and pop's on linked lists and another two for the same but on resizable arrays – Dantex Apr 12 '17 at 14:44
  • I really cant understand what you are trying to do. I think your approach is wrong and there is a better approach. Can you explain why do you need to put it as parameter? as you will call your method with parameter n times, you can just call your methods, it will be the same effort – strash Apr 12 '17 at 15:06
  • I have an enormous method code to write in excel some tsts data. I'm doing push and pop in linked lists and resizable arrays, so now i have to repeat this code 2 times, on for tests on linked lists and another for the resizable tests. If i could pass the method that calculate the pushes and pops, i could just use one method that would write to excel the output of the method i want to pass as a parameter – Dantex Apr 12 '17 at 15:18

2 Answers2

0

If all the methods have the same signature double m(int), then you can use something like the IntToDoubleFunction interface and pass a method reference:

public static void testsForLinkedLists(int potencia,
        int repeticoesteste, int somarAoArray, String ficheiroExcel,
        int validararray, IntToDoubleFunction pushMethod)

and you'd call it with:

testsForLinkedLists(...., measuring_tests::timeToPushLinkedStack);
assylias
  • 321,522
  • 82
  • 660
  • 783
  • i've done your suggestion and edited to `public static void testsForLinkedLists(int potencia, int repeticoesteste, int somarAoArray, String ficheiroExcel, int validararray, Method pushMethod, IntToDoubleFunction popMethod)` but when i use the pushMethod inside this testsForLinkedLists gives me errors. I'm doing it like this: `double timetopush = pushMethod(resultadopotencia); ` it says that i gotta create a pushMethod method :/ – Dantex Apr 12 '17 at 15:03
  • IntToDoubleFunction is an interface - you need to call its method: `double timetopush = pushMethod.applyAsDouble(resultadopotencia);`. – assylias Apr 12 '17 at 15:10
0

Ok. I understood :) try taking a look in this tutorial: https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html also you can get your methods by: Method fooMethod = MyObject.class.getMethod("foo")

strash
  • 1,291
  • 2
  • 15
  • 29