1

I'm trying to implement stopwatch class that will wrap all the required methods that i'm trying to measure

The main purpose is to create an framwork that when ever you want to measure the execution time of a method you will change the exiting code from:

<Classname> x = A();

or

A();

To:

<Classname> x = PSW.startWatch(MethodToWatch.B).invoke("123123123", A());

or

PSW.startWatch(MethodToWatch.B).invoke("123123123", A());

This is the code:

public class PerformanceStopWatch {
    private long start;
    private long end;
    private String uniqueID;
    private MethodToWatch methodName;

    public enum MethodToWatch {
        A,
        B
    }

    public PerformanceStopWatch startWatch(MethodToWatch methodName) {
        this.methodName = methodName;
        start();
        return this;
    }

    public void invoke(String uniqeID) {
        this.uniqueID = uniqeID;
        stop();
    }

    public void invoke(UUID machineId, void a) {
       invoke(machineId);
    }

    private void start() {
        start = System.nanoTime();
    }
    private void stop() {
        end = System.nanoTime();
        System.out.println("[PerformanceStopWatch|"+methodName.name()+"] - Unique ID: "+ uniqueID + " Took: <"+(end-start)+"> Nanosec");
    }
}

public void a() {
PerformanceStopWatch PSW = new PerformanceStopWatch();
..
..
..
PSW.startWatch(MethodToWatch.B).invoke("123123123", b());

}

public void b() {
...
...
..

}

The problem is that the compiler dose not allow me to use

public void invoke(UUID machineId, void a) {
    }

void is illegle type.

any idea?

5
  • It makes absolutely no sense to try to pass void to a method. Why are you trying that? Commented Dec 1, 2015 at 9:18
  • nevertheless no downvote candidate for me... Commented Dec 1, 2015 at 9:25
  • @Tom have you tried to read the question? i'm trying to measure the execution time of method b from method a Commented Dec 1, 2015 at 9:25
  • @USer22999299 And you're doing it in a very ugly way. Please read this question for some inspiration: stackoverflow.com/questions/3382954/… Commented Dec 1, 2015 at 9:28
  • @Tom thanks, but i will try to ignor your advice. if you would like to help in here it would be nice, read the question before answering. Commented Dec 1, 2015 at 9:30

3 Answers 3

4

You can try Void insteed of void (Capital V) as the second one is not a type in strict sense. Void on the other hand is. But in general if you want to invoke with "void" you simply ommit arguments (talking about reflection here)

EDIT: to your comment

public void invoke(UUID machineId, Void a) { //note the capital V
   invoke(machineId);
}

Will compile just fine, but I dont see any usable usecase for such method signature.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks! for your second part of the answer, how i can ommit the arguments and call it with void?
invoke(UUID machineId, Void... a)
while changing it to public void invoke(UUID machineId, Void a), and using is as .PSW.startWatch(MethodToWatch.B).invoke("123123123", b()); i'm still getting "wrong 2nd arrgument type found "void" required "java.lang.Void"
@USer22999299 pass null insteed of b() You must undestand that void in general is not a type but declaration that function does not return a value. Void is private dummy type introduce for generics.
@USer22999299 Simply speaking, you cannot pass void as argument that would be different than null.
|
3

It looks like you want to pass a method to the invoke() method. Since the method you are passing returns nothing and has no arguments, you should use Runnable interface :

public void invoke(UUID machineId, Runnable a) {
   invoke(machineId);
   a.run();
}

and

PSW.startWatch(MethodToWatch.B).invoke("123123123", () -> {b();});

or (if you are not using Java 8) :

PSW.startWatch(MethodToWatch.B).invoke("123123123",
                                       new Runnable() {
                                             public void run() {
                                               b();
                                             }
                                       });

3 Comments

cool! and in case the method will return int / string, is there an option as well to use it?
@USer22999299 There are all kinds of functional interfaces added in Java 8. For example, IntSupplier has a single method that returns an int.
Thanks, this one solved my issue, in case you need a return value you can consider using the callable interface insted of the runable :)
1

What exactly do you want to do? There are no objects or values of type void, so if it would have been allowed, you would not be able to call the method.

Maybe what you want is to be able to pass an object of any type. In this case you can use Object:

public void invoke(UUID machineId, Object a) {
    invoke(machineId);
}

Since every class is a subclass of Object, now you can pass any object as the second argument to this method, including arrays. You can also pass primitive types (they will be wrapped to their corresponding wrapper class).

3 Comments

I would like to measure the time of calling method b as i wrote in the question
@USer22999299. Yes, that is what you want to do with the class. But what purpose does that method have? It takes an argument a but just ignores it.
the purpose is to build a framwork and a patern that when ever you want to masure the time of a method, this is what you have to do regardless of what this method is returning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.