1. /*
  2. * @(#)file SnmpInt.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.11
  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. import com.sun.jmx.snmp.Enumerated;
  13. /**
  14. * Represents an SNMP integer.
  15. *
  16. * <p><b>This API is a Sun Microsystems internal API and is subject
  17. * to change without notice.</b></p>
  18. * @version 4.11 12/19/03
  19. * @author Sun Microsystems, Inc
  20. */
  21. public class SnmpInt extends SnmpValue {
  22. // CONSTRUCTORS
  23. //-------------
  24. /**
  25. * Constructs a new <CODE>SnmpInt</CODE> from the specified integer value.
  26. * @param v The initialization value.
  27. * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
  28. * or larger than <CODE>Integer.MAX_VALUE</CODE>.
  29. */
  30. public SnmpInt(int v) throws IllegalArgumentException {
  31. if ( isInitValueValid(v) == false ) {
  32. throw new IllegalArgumentException() ;
  33. }
  34. value = (long)v ;
  35. }
  36. /**
  37. * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Integer</CODE> value.
  38. * @param v The initialization value.
  39. * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
  40. * or larger than <CODE>Integer.MAX_VALUE</CODE>.
  41. */
  42. public SnmpInt(Integer v) throws IllegalArgumentException {
  43. this(v.intValue()) ;
  44. }
  45. /**
  46. * Constructs a new <CODE>SnmpInt</CODE> from the specified long value.
  47. * @param v The initialization value.
  48. * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
  49. * or larger than <CODE>Integer.MAX_VALUE</CODE>.
  50. */
  51. public SnmpInt(long v) throws IllegalArgumentException {
  52. if ( isInitValueValid(v) == false ) {
  53. throw new IllegalArgumentException() ;
  54. }
  55. value = v ;
  56. }
  57. /**
  58. * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Long</CODE> value.
  59. * @param v The initialization value.
  60. * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
  61. * or larger than <CODE>Integer.MAX_VALUE</CODE>.
  62. */
  63. public SnmpInt(Long v) throws IllegalArgumentException {
  64. this(v.longValue()) ;
  65. }
  66. /**
  67. * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Enumerated</CODE> value.
  68. * @param v The initialization value.
  69. * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
  70. * or larger than <CODE>Integer.MAX_VALUE</CODE>.
  71. * @see Enumerated
  72. */
  73. public SnmpInt(Enumerated v) throws IllegalArgumentException {
  74. this(v.intValue()) ;
  75. }
  76. /**
  77. * Constructs a new <CODE>SnmpInt</CODE> from the specified boolean value.
  78. * This constructor applies rfc1903 rule:
  79. * <p><blockquote><pre>
  80. * TruthValue ::= TEXTUAL-CONVENTION
  81. * STATUS current
  82. * DESCRIPTION
  83. * "Represents a boolean value."
  84. * SYNTAX INTEGER { true(1), false(2) }
  85. * </pre></blockquote>
  86. * @param v The initialization value.
  87. */
  88. public SnmpInt(boolean v) {
  89. value = v ? 1 : 2 ;
  90. }
  91. // PUBLIC METHODS
  92. //---------------
  93. /**
  94. * Returns the long value of this <CODE>SnmpInt</CODE>.
  95. * @return The value.
  96. */
  97. public long longValue() {
  98. return value ;
  99. }
  100. /**
  101. * Converts the integer value to its <CODE>Long</CODE> form.
  102. * @return The <CODE>Long</CODE> representation of the value.
  103. */
  104. public Long toLong() {
  105. return new Long(value) ;
  106. }
  107. /**
  108. * Converts the integer value to its integer form.
  109. * @return The integer representation of the value.
  110. */
  111. public int intValue() {
  112. return (int) value ;
  113. }
  114. /**
  115. * Converts the integer value to its <CODE>Integer</CODE> form.
  116. * @return The <CODE>Integer</CODE> representation of the value.
  117. */
  118. public Integer toInteger() {
  119. return new Integer((int)value) ;
  120. }
  121. /**
  122. * Converts the integer value to its <CODE>String</CODE> form.
  123. * @return The <CODE>String</CODE> representation of the value.
  124. */
  125. public String toString() {
  126. return String.valueOf(value) ;
  127. }
  128. /**
  129. * Converts the integer value to its <CODE>SnmpOid</CODE> form.
  130. * @return The OID representation of the value.
  131. */
  132. public SnmpOid toOid() {
  133. return new SnmpOid(value) ;
  134. }
  135. /**
  136. * Extracts the integer from an index OID and returns its
  137. * value converted as an <CODE>SnmpOid</CODE>.
  138. * @param index The index array.
  139. * @param start The position in the index array.
  140. * @return The OID representing the integer value.
  141. * @exception SnmpStatusException There is no integer value
  142. * available at the start position.
  143. */
  144. public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
  145. try {
  146. return new SnmpOid(index[start]) ;
  147. }
  148. catch(IndexOutOfBoundsException e) {
  149. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  150. }
  151. }
  152. /**
  153. * Scans an index OID, skips the integer value and returns the position
  154. * of the next value.
  155. * @param index The index array.
  156. * @param start The position in the index array.
  157. * @return The position of the next value.
  158. * @exception SnmpStatusException There is no integer value
  159. * available at the start position.
  160. */
  161. public static int nextOid(long[] index, int start) throws SnmpStatusException {
  162. if (start >= index.length) {
  163. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  164. }
  165. else {
  166. return start + 1 ;
  167. }
  168. }
  169. /**
  170. * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpInt</CODE> to another OID.
  171. * @param source An OID representing an <CODE>SnmpInt</CODE> value.
  172. * @param dest Where source should be appended.
  173. */
  174. public static void appendToOid(SnmpOid source, SnmpOid dest) {
  175. if (source.getLength() != 1) {
  176. throw new IllegalArgumentException() ;
  177. }
  178. dest.append(source) ;
  179. }
  180. /**
  181. * Performs a clone action. This provides a workaround for the
  182. * <CODE>SnmpValue</CODE> interface.
  183. * @return The <CODE>SnmpValue</CODE> clone.
  184. */
  185. final synchronized public SnmpValue duplicate() {
  186. return (SnmpValue) clone() ;
  187. }
  188. /**
  189. * Clones the <CODE>SnmpInt</CODE> object, making a copy of its data.
  190. * @return The object clone.
  191. */
  192. final synchronized public Object clone() {
  193. SnmpInt newclone = null ;
  194. try {
  195. newclone = (SnmpInt) super.clone() ;
  196. newclone.value = value ;
  197. } catch (CloneNotSupportedException e) {
  198. throw new InternalError() ; // vm bug.
  199. }
  200. return newclone ;
  201. }
  202. /**
  203. * Returns a textual description of the type object.
  204. * @return ASN.1 textual description.
  205. */
  206. public String getTypeName() {
  207. return name ;
  208. }
  209. /**
  210. * This method has been defined to allow the sub-classes
  211. * of SnmpInt to perform their own control at intialization time.
  212. */
  213. boolean isInitValueValid(int v) {
  214. if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {
  215. return false;
  216. }
  217. return true;
  218. }
  219. /**
  220. * This method has been defined to allow the sub-classes
  221. * of SnmpInt to perform their own control at intialization time.
  222. */
  223. boolean isInitValueValid(long v) {
  224. if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. // VARIABLES
  230. //----------
  231. /**
  232. * Name of the type.
  233. */
  234. final static String name = "Integer32" ;
  235. /**
  236. * This is where the value is stored. This long is signed.
  237. * @serial
  238. */
  239. protected long value = 0 ;
  240. }