KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > SnmpInt


1 /*
2  * @(#)file SnmpInt.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.11
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp;
14
15
16
17 import com.sun.jmx.snmp.Enumerated;
18
19 /**
20  * Represents an SNMP integer.
21  *
22  * <p><b>This API is a Sun Microsystems internal API and is subject
23  * to change without notice.</b></p>
24  * @version 4.11 12/19/03
25  * @author Sun Microsystems, Inc
26  */

27
28 public class SnmpInt extends SnmpValue {
29
30     // CONSTRUCTORS
31
//-------------
32
/**
33      * Constructs a new <CODE>SnmpInt</CODE> from the specified integer value.
34      * @param v The initialization value.
35      * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
36      * or larger than <CODE>Integer.MAX_VALUE</CODE>.
37      */

38     public SnmpInt(int v) throws IllegalArgumentException JavaDoc {
39         if ( isInitValueValid(v) == false ) {
40             throw new IllegalArgumentException JavaDoc() ;
41         }
42         value = (long)v ;
43     }
44
45     /**
46      * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Integer</CODE> 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(Integer JavaDoc v) throws IllegalArgumentException JavaDoc {
52         this(v.intValue()) ;
53     }
54
55     /**
56      * Constructs a new <CODE>SnmpInt</CODE> from the specified long value.
57      * @param v The initialization value.
58      * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
59      * or larger than <CODE>Integer.MAX_VALUE</CODE>.
60      */

61     public SnmpInt(long v) throws IllegalArgumentException JavaDoc {
62         if ( isInitValueValid(v) == false ) {
63             throw new IllegalArgumentException JavaDoc() ;
64         }
65         value = v ;
66     }
67
68     /**
69      * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Long</CODE> value.
70      * @param v The initialization value.
71      * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
72      * or larger than <CODE>Integer.MAX_VALUE</CODE>.
73      */

74     public SnmpInt(Long JavaDoc v) throws IllegalArgumentException JavaDoc {
75         this(v.longValue()) ;
76     }
77
78     /**
79      * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Enumerated</CODE> value.
80      * @param v The initialization value.
81      * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
82      * or larger than <CODE>Integer.MAX_VALUE</CODE>.
83      * @see Enumerated
84      */

85     public SnmpInt(Enumerated v) throws IllegalArgumentException JavaDoc {
86         this(v.intValue()) ;
87     }
88
89     /**
90      * Constructs a new <CODE>SnmpInt</CODE> from the specified boolean value.
91      * This constructor applies rfc1903 rule:
92      * <p><blockquote><pre>
93      * TruthValue ::= TEXTUAL-CONVENTION
94      * STATUS current
95      * DESCRIPTION
96      * "Represents a boolean value."
97      * SYNTAX INTEGER { true(1), false(2) }
98      * </pre></blockquote>
99      * @param v The initialization value.
100      */

101     public SnmpInt(boolean v) {
102         value = v ? 1 : 2 ;
103     }
104
105     // PUBLIC METHODS
106
//---------------
107
/**
108      * Returns the long value of this <CODE>SnmpInt</CODE>.
109      * @return The value.
110      */

111     public long longValue() {
112         return value ;
113     }
114
115     /**
116      * Converts the integer value to its <CODE>Long</CODE> form.
117      * @return The <CODE>Long</CODE> representation of the value.
118      */

119     public Long JavaDoc toLong() {
120         return new Long JavaDoc(value) ;
121     }
122
123     /**
124      * Converts the integer value to its integer form.
125      * @return The integer representation of the value.
126      */

127     public int intValue() {
128         return (int) value ;
129     }
130
131     /**
132      * Converts the integer value to its <CODE>Integer</CODE> form.
133      * @return The <CODE>Integer</CODE> representation of the value.
134      */

135     public Integer JavaDoc toInteger() {
136         return new Integer JavaDoc((int)value) ;
137     }
138   
139     /**
140      * Converts the integer value to its <CODE>String</CODE> form.
141      * @return The <CODE>String</CODE> representation of the value.
142      */

143     public String JavaDoc toString() {
144         return String.valueOf(value) ;
145     }
146
147     /**
148      * Converts the integer value to its <CODE>SnmpOid</CODE> form.
149      * @return The OID representation of the value.
150      */

151     public SnmpOid toOid() {
152         return new SnmpOid(value) ;
153     }
154   
155     /**
156      * Extracts the integer from an index OID and returns its
157      * value converted as an <CODE>SnmpOid</CODE>.
158      * @param index The index array.
159      * @param start The position in the index array.
160      * @return The OID representing the integer value.
161      * @exception SnmpStatusException There is no integer value
162      * available at the start position.
163      */

164     public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
165         try {
166             return new SnmpOid(index[start]) ;
167         }
168         catch(IndexOutOfBoundsException JavaDoc e) {
169             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
170         }
171     }
172   
173     /**
174      * Scans an index OID, skips the integer value and returns the position
175      * of the next value.
176      * @param index The index array.
177      * @param start The position in the index array.
178      * @return The position of the next value.
179      * @exception SnmpStatusException There is no integer value
180      * available at the start position.
181      */

182     public static int nextOid(long[] index, int start) throws SnmpStatusException {
183         if (start >= index.length) {
184             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
185         }
186         else {
187             return start + 1 ;
188         }
189     }
190   
191     /**
192      * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpInt</CODE> to another OID.
193      * @param source An OID representing an <CODE>SnmpInt</CODE> value.
194      * @param dest Where source should be appended.
195      */

196     public static void appendToOid(SnmpOid source, SnmpOid dest) {
197         if (source.getLength() != 1) {
198             throw new IllegalArgumentException JavaDoc() ;
199         }
200         dest.append(source) ;
201     }
202
203     /**
204      * Performs a clone action. This provides a workaround for the
205      * <CODE>SnmpValue</CODE> interface.
206      * @return The <CODE>SnmpValue</CODE> clone.
207      */

208     final synchronized public SnmpValue duplicate() {
209         return (SnmpValue) clone() ;
210     }
211
212     /**
213      * Clones the <CODE>SnmpInt</CODE> object, making a copy of its data.
214      * @return The object clone.
215      */

216     final synchronized public Object JavaDoc clone() {
217         SnmpInt newclone = null ;
218         try {
219             newclone = (SnmpInt) super.clone() ;
220             newclone.value = value ;
221         } catch (CloneNotSupportedException JavaDoc e) {
222             throw new InternalError JavaDoc() ; // vm bug.
223
}
224         return newclone ;
225     }
226     
227     /**
228      * Returns a textual description of the type object.
229      * @return ASN.1 textual description.
230      */

231     public String JavaDoc getTypeName() {
232         return name ;
233     }
234
235     /**
236      * This method has been defined to allow the sub-classes
237      * of SnmpInt to perform their own control at intialization time.
238      */

239     boolean isInitValueValid(int v) {
240         if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {
241             return false;
242         }
243         return true;
244     }
245
246     /**
247      * This method has been defined to allow the sub-classes
248      * of SnmpInt to perform their own control at intialization time.
249      */

250     boolean isInitValueValid(long v) {
251         if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {
252             return false;
253         }
254         return true;
255     }
256
257     // VARIABLES
258
//----------
259
/**
260      * Name of the type.
261      */

262     final static String JavaDoc name = "Integer32" ;
263
264     /**
265      * This is where the value is stored. This long is signed.
266      * @serial
267      */

268     protected long value = 0 ;
269 }
270
Popular Tags