1. /*
  2. * @(#)file SnmpRequestCounter.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 1.4
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. // Copyright (c) 1995-96 by Cisco Systems, Inc.
  12. package com.sun.jmx.snmp.daemon;
  13. /**
  14. * A static instance of this class is usually created. It contains a
  15. * counter that is incremented every time it is accessed. For example,
  16. * this class can be used in <CODE>SnmpSession</CODE> to generate a request
  17. * identifier that is used to identify a message in a client-server session.
  18. * The class wraps around when it reaches the maximum positive value, 2^31 - 1.
  19. */
  20. final class SnmpRequestCounter {
  21. /**
  22. * A counter with value between 1...2^31-1.
  23. */
  24. int reqid = 0 ;
  25. public SnmpRequestCounter() {}
  26. /**
  27. * Returns the next request identifier.
  28. * @return next request identifier. The value wraps to 1 if it reaches negative value.
  29. */
  30. public synchronized int getNewId() {
  31. if (++reqid < 0)
  32. reqid = 1 ;
  33. return reqid ;
  34. }
  35. }