KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file SnmpCounter64.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.8
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 /**
18  * Represents an SNMP 64bits counter.
19  *
20  * <p><b>This API is a Sun Microsystems internal API and is subject
21  * to change without notice.</b></p>
22  * @version 4.8 12/19/03
23  * @author Sun Microsystems, Inc
24  */

25
26 public class SnmpCounter64 extends SnmpValue {
27
28     // CONSTRUCTORS
29
//-------------
30
/**
31      * Constructs a new <CODE>SnmpCounter64</CODE> from the specified long value.
32      * @param v The initialization value.
33      * @exception IllegalArgumentException The specified value is negative
34      * or larger than <CODE>Long.MAX_VALUE</CODE>.
35      */

36     public SnmpCounter64(long v) throws IllegalArgumentException JavaDoc {
37
38     // NOTE:
39
// The max value for a counter64 variable is 2^64 - 1.
40
// The max value for a Long is 2^63 - 1.
41
// All the allowed values for a conuter64 variable cannot be covered !!!
42
//
43
if ((v < 0) || (v > Long.MAX_VALUE)) {
44         throw new IllegalArgumentException JavaDoc() ;
45     }
46     value = v ;
47     }
48   
49     /**
50      * Constructs a new <CODE>SnmpCounter64</CODE> from the specified <CODE>Long</CODE> value.
51      * @param v The initialization value.
52      * @exception IllegalArgumentException The specified value is negative
53      * or larger than <CODE>Long.MAX_VALUE</CODE>.
54      */

55     public SnmpCounter64(Long JavaDoc v) throws IllegalArgumentException JavaDoc {
56     this(v.longValue()) ;
57     }
58   
59     // PUBLIC METHODS
60
//---------------
61
/**
62      * Returns the counter value of this <CODE>SnmpCounter64</CODE>.
63      * @return The value.
64      */

65     public long longValue() {
66     return value ;
67     }
68
69     /**
70      * Converts the counter value to its <CODE>Long</CODE> form.
71      * @return The <CODE>Long</CODE> representation of the value.
72      */

73     public Long JavaDoc toLong() {
74     return new Long JavaDoc(value) ;
75     }
76     
77     /**
78      * Converts the counter value to its integer form.
79      * @return The integer representation of the value.
80      */

81     public int intValue() {
82     return (int)value ;
83     }
84   
85     /**
86      * Converts the counter value to its <CODE>Integer</CODE> form.
87      * @return The <CODE>Integer</CODE> representation of the value.
88      */

89     public Integer JavaDoc toInteger() {
90     return new Integer JavaDoc((int)value) ;
91     }
92
93     /**
94      * Converts the counter value to its <CODE>String</CODE> form.
95      * @return The <CODE>String</CODE> representation of the value.
96      */

97     public String JavaDoc toString() {
98     return String.valueOf(value) ;
99     }
100   
101     /**
102      * Converts the counter value to its <CODE>SnmpOid</CODE> form.
103      * @return The OID representation of the value.
104      */

105     public SnmpOid toOid() {
106     return new SnmpOid(value) ;
107     }
108   
109     /**
110      * Extracts the counter from an index OID and returns its
111      * value converted as an <CODE>SnmpOid</CODE>.
112      * @param index The index array.
113      * @param start The position in the index array.
114      * @return The OID representing the counter value.
115      * @exception SnmpStatusException There is no counter value
116      * available at the start position.
117      */

118     public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
119     try {
120         return new SnmpOid(index[start]) ;
121     }
122     catch(IndexOutOfBoundsException JavaDoc e) {
123         throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
124     }
125     }
126
127     /**
128      * Scans an index OID, skips the counter value and returns the position
129      * of the next value.
130      * @param index The index array.
131      * @param start The position in the index array.
132      * @return The position of the next value.
133      * @exception SnmpStatusException There is no counter value
134      * available at the start position.
135      */

136     public static int nextOid(long[] index, int start) throws SnmpStatusException {
137     if (start >= index.length) {
138         throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
139     }
140     else {
141         return start + 1 ;
142     }
143     }
144     
145     /**
146      * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpCounter64</CODE> to another OID.
147      * @param source An OID representing an <CODE>SnmpCounter64</CODE> value.
148      * @param dest Where source should be appended.
149      */

150     public static void appendToOid(SnmpOid source, SnmpOid dest) {
151     if (source.getLength() != 1) {
152         throw new IllegalArgumentException JavaDoc() ;
153     }
154     dest.append(source) ;
155     }
156   
157     /**
158      * Performs a clone action. This provides a workaround for the
159      * <CODE>SnmpValue</CODE> interface.
160      * @return The SnmpValue clone.
161      */

162     final synchronized public SnmpValue duplicate() {
163     return (SnmpValue)clone() ;
164     }
165
166     /**
167      * Clones the <CODE>SnmpCounter64</CODE> object, making a copy of its data.
168      * @return The object clone.
169      */

170     final synchronized public Object JavaDoc clone() {
171         SnmpCounter64 newclone = null ;
172         try {
173         newclone = (SnmpCounter64) super.clone() ;
174         newclone.value = value ;
175         } catch (CloneNotSupportedException JavaDoc e) {
176         throw new InternalError JavaDoc() ; // vm bug.
177
}
178         return newclone ;
179     }
180
181     /**
182      * Returns a textual description of the type object.
183      * @return ASN.1 textual description.
184      */

185     final public String JavaDoc getTypeName() {
186         return name ;
187     }
188
189     // VARIABLES
190
//----------
191
/**
192      * Name of the type.
193      */

194     final static String JavaDoc name = "Counter64" ;
195   
196     /**
197      * This is where the value is stored. This long is positive.
198      * @serial
199      */

200     private long value = 0 ;
201 }
202
Popular Tags