Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer.Context should be able to be subclassed #686

Closed
bkyle opened this issue Oct 10, 2014 · 2 comments
Closed

Timer.Context should be able to be subclassed #686

bkyle opened this issue Oct 10, 2014 · 2 comments

Comments

@bkyle
Copy link

bkyle commented Oct 10, 2014

Timer.Context cannot be subclassed due to its private constructor. The constructor should be made protected and optionally the class should be moved out into its own top-level class TimerContext (or something similar). This would allow TimerContext to be subclassed. Alternatively, many of the classes within the framework have their interface and implementation separated. Either of these options would allow for extension where none is currently permitted.

@RichMacDonald
Copy link

Agreed. Or simply create a Context interface. A trivial change that goes a long way and avoids the hacking some of us currently do. For example:

Using the Timer.Context causes garbage collected instances. Unacceptable in tight loops. If we had a public interface, we could use a pool.

@arteam
Copy link
Member

arteam commented Nov 3, 2017

I think that's a good change for the 4.0 version. We can create an interface inside Timer:

public interface Context extends AutoCloseable {

        long stop();

        @Override
        default void close() {
            stop();
        }
}

and a default private implementation:

private static class DefaultContext implements Context {

        private final Timer timer;
        private final Clock clock;
        private final long startTime;

        private DefaultContext(Timer timer, Clock clock) {
            this.timer = timer;
            this.clock = clock;
            this.startTime = clock.getTick();
        }
        
        @Override
        public long stop() {
            final long elapsed = clock.getTick() - startTime;
            timer.update(elapsed, TimeUnit.NANOSECONDS);
            return elapsed;
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants