1. /*
  2. * @(#)file SnmpCounter64.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.8
  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. package com.sun.jmx.snmp;
  12. /**
  13. * Represents an SNMP 64bits counter.
  14. *
  15. * <p><b>This API is a Sun Microsystems internal API and is subject
  16. * to change without notice.</b></p>
  17. * @version 4.8 12/19/03
  18. * @author Sun Microsystems, Inc
  19. */
  20. public class SnmpCounter64 extends SnmpValue {
  21. // CONSTRUCTORS
  22. //-------------
  23. /**
  24. * Constructs a new <CODE>SnmpCounter64</CODE> from the specified long value.
  25. * @param v The initialization value.
  26. * @exception IllegalArgumentException The specified value is negative
  27. * or larger than <CODE>Long.MAX_VALUE</CODE>.
  28. */
  29. public SnmpCounter64(long v) throws IllegalArgumentException {
  30. // NOTE:
  31. // The max value for a counter64 variable is 2^64 - 1.
  32. // The max value for a Long is 2^63 - 1.
  33. // All the allowed values for a conuter64 variable cannot be covered !!!
  34. //
  35. if ((v < 0) || (v > Long.MAX_VALUE)) {
  36. throw new IllegalArgumentException() ;
  37. }
  38. value = v ;
  39. }
  40. /**
  41. * Constructs a new <CODE>SnmpCounter64</CODE> from the specified <CODE>Long</CODE> value.
  42. * @param v The initialization value.
  43. * @exception IllegalArgumentException The specified value is negative
  44. * or larger than <CODE>Long.MAX_VALUE</CODE>.
  45. */
  46. public SnmpCounter64(Long v) throws IllegalArgumentException {
  47. this(v.longValue()) ;
  48. }
  49. // PUBLIC METHODS
  50. //---------------
  51. /**
  52. * Returns the counter value of this <CODE>SnmpCounter64</CODE>.
  53. * @return The value.
  54. */
  55. public long longValue() {
  56. return value ;
  57. }
  58. /**
  59. * Converts the counter value to its <CODE>Long</CODE> form.
  60. * @return The <CODE>Long</CODE> representation of the value.
  61. */
  62. public Long toLong() {
  63. return new Long(value) ;
  64. }
  65. /**
  66. * Converts the counter value to its integer form.
  67. * @return The integer representation of the value.
  68. */
  69. public int intValue() {
  70. return (int)value ;
  71. }
  72. /**
  73. * Converts the counter value to its <CODE>Integer</CODE> form.
  74. * @return The <CODE>Integer</CODE> representation of the value.
  75. */
  76. public Integer toInteger() {
  77. return new Integer((int)value) ;
  78. }
  79. /**
  80. * Converts the counter value to its <CODE>String</CODE> form.
  81. * @return The <CODE>String</CODE> representation of the value.
  82. */
  83. public String toString() {
  84. return String.valueOf(value) ;
  85. }
  86. /**
  87. * Converts the counter value to its <CODE>SnmpOid</CODE> form.
  88. * @return The OID representation of the value.
  89. */
  90. public SnmpOid toOid() {
  91. return new SnmpOid(value) ;
  92. }
  93. /**
  94. * Extracts the counter from an index OID and returns its
  95. * value converted as an <CODE>SnmpOid</CODE>.
  96. * @param index The index array.
  97. * @param start The position in the index array.
  98. * @return The OID representing the counter value.
  99. * @exception SnmpStatusException There is no counter value
  100. * available at the start position.
  101. */
  102. public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
  103. try {
  104. return new SnmpOid(index[start]) ;
  105. }
  106. catch(IndexOutOfBoundsException e) {
  107. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  108. }
  109. }
  110. /**
  111. * Scans an index OID, skips the counter value and returns the position
  112. * of the next value.
  113. * @param index The index array.
  114. * @param start The position in the index array.
  115. * @return The position of the next value.
  116. * @exception SnmpStatusException There is no counter value
  117. * available at the start position.
  118. */
  119. public static int nextOid(long[] index, int start) throws SnmpStatusException {
  120. if (start >= index.length) {
  121. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  122. }
  123. else {
  124. return start + 1 ;
  125. }
  126. }
  127. /**
  128. * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpCounter64</CODE> to another OID.
  129. * @param source An OID representing an <CODE>SnmpCounter64</CODE> value.
  130. * @param dest Where source should be appended.
  131. */
  132. public static void appendToOid(SnmpOid source, SnmpOid dest) {
  133. if (source.getLength() != 1) {
  134. throw new IllegalArgumentException() ;
  135. }
  136. dest.append(source) ;
  137. }
  138. /**
  139. * Performs a clone action. This provides a workaround for the
  140. * <CODE>SnmpValue</CODE> interface.
  141. * @return The SnmpValue clone.
  142. */
  143. final synchronized public SnmpValue duplicate() {
  144. return (SnmpValue)clone() ;
  145. }
  146. /**
  147. * Clones the <CODE>SnmpCounter64</CODE> object, making a copy of its data.
  148. * @return The object clone.
  149. */
  150. final synchronized public Object clone() {
  151. SnmpCounter64 newclone = null ;
  152. try {
  153. newclone = (SnmpCounter64) super.clone() ;
  154. newclone.value = value ;
  155. } catch (CloneNotSupportedException e) {
  156. throw new InternalError() ; // vm bug.
  157. }
  158. return newclone ;
  159. }
  160. /**
  161. * Returns a textual description of the type object.
  162. * @return ASN.1 textual description.
  163. */
  164. final public String getTypeName() {
  165. return name ;
  166. }
  167. // VARIABLES
  168. //----------
  169. /**
  170. * Name of the type.
  171. */
  172. final static String name = "Counter64" ;
  173. /**
  174. * This is where the value is stored. This long is positive.
  175. * @serial
  176. */
  177. private long value = 0 ;
  178. }