AtomicCounter.java 711 B

1234567891011121314151617181920212223242526
  1. package services;
  2. import java.util.concurrent.atomic.AtomicInteger;
  3. import javax.inject.*;
  4. /**
  5. * This class is a concrete implementation of the {@link Counter} trait.
  6. * It is configured for Guice dependency injection in the {@link Module}
  7. * class.
  8. *
  9. * This class has a {@link Singleton} annotation because we need to make
  10. * sure we only use one counter per application. Without this
  11. * annotation we would get a new instance every time a {@link Counter} is
  12. * injected.
  13. */
  14. @Singleton
  15. public class AtomicCounter implements Counter {
  16. private final AtomicInteger atomicCounter = new AtomicInteger();
  17. @Override
  18. public int nextCount() {
  19. return atomicCounter.getAndIncrement();
  20. }
  21. }