001: /*
002: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.jmx.snmp;
027:
028: /**
029: * Represents an SNMP 64bits counter.
030: *
031: * <p><b>This API is a Sun Microsystems internal API and is subject
032: * to change without notice.</b></p>
033: */
034:
035: public class SnmpCounter64 extends SnmpValue {
036: private static final long serialVersionUID = 8784850650494679937L;
037:
038: // CONSTRUCTORS
039: //-------------
040: /**
041: * Constructs a new <CODE>SnmpCounter64</CODE> from the specified long value.
042: * @param v The initialization value.
043: * @exception IllegalArgumentException The specified value is negative
044: * or larger than <CODE>Long.MAX_VALUE</CODE>.
045: */
046: public SnmpCounter64(long v) throws IllegalArgumentException {
047:
048: // NOTE:
049: // The max value for a counter64 variable is 2^64 - 1.
050: // The max value for a Long is 2^63 - 1.
051: // All the allowed values for a conuter64 variable cannot be covered !!!
052: //
053: if ((v < 0) || (v > Long.MAX_VALUE)) {
054: throw new IllegalArgumentException();
055: }
056: value = v;
057: }
058:
059: /**
060: * Constructs a new <CODE>SnmpCounter64</CODE> from the specified <CODE>Long</CODE> value.
061: * @param v The initialization value.
062: * @exception IllegalArgumentException The specified value is negative
063: * or larger than <CODE>Long.MAX_VALUE</CODE>.
064: */
065: public SnmpCounter64(Long v) throws IllegalArgumentException {
066: this (v.longValue());
067: }
068:
069: // PUBLIC METHODS
070: //---------------
071: /**
072: * Returns the counter value of this <CODE>SnmpCounter64</CODE>.
073: * @return The value.
074: */
075: public long longValue() {
076: return value;
077: }
078:
079: /**
080: * Converts the counter value to its <CODE>Long</CODE> form.
081: * @return The <CODE>Long</CODE> representation of the value.
082: */
083: public Long toLong() {
084: return new Long(value);
085: }
086:
087: /**
088: * Converts the counter value to its integer form.
089: * @return The integer representation of the value.
090: */
091: public int intValue() {
092: return (int) value;
093: }
094:
095: /**
096: * Converts the counter value to its <CODE>Integer</CODE> form.
097: * @return The <CODE>Integer</CODE> representation of the value.
098: */
099: public Integer toInteger() {
100: return new Integer((int) value);
101: }
102:
103: /**
104: * Converts the counter value to its <CODE>String</CODE> form.
105: * @return The <CODE>String</CODE> representation of the value.
106: */
107: public String toString() {
108: return String.valueOf(value);
109: }
110:
111: /**
112: * Converts the counter value to its <CODE>SnmpOid</CODE> form.
113: * @return The OID representation of the value.
114: */
115: public SnmpOid toOid() {
116: return new SnmpOid(value);
117: }
118:
119: /**
120: * Extracts the counter from an index OID and returns its
121: * value converted as an <CODE>SnmpOid</CODE>.
122: * @param index The index array.
123: * @param start The position in the index array.
124: * @return The OID representing the counter value.
125: * @exception SnmpStatusException There is no counter value
126: * available at the start position.
127: */
128: public static SnmpOid toOid(long[] index, int start)
129: throws SnmpStatusException {
130: try {
131: return new SnmpOid(index[start]);
132: } catch (IndexOutOfBoundsException e) {
133: throw new SnmpStatusException(
134: SnmpStatusException.noSuchName);
135: }
136: }
137:
138: /**
139: * Scans an index OID, skips the counter value and returns the position
140: * of the next value.
141: * @param index The index array.
142: * @param start The position in the index array.
143: * @return The position of the next value.
144: * @exception SnmpStatusException There is no counter value
145: * available at the start position.
146: */
147: public static int nextOid(long[] index, int start)
148: throws SnmpStatusException {
149: if (start >= index.length) {
150: throw new SnmpStatusException(
151: SnmpStatusException.noSuchName);
152: } else {
153: return start + 1;
154: }
155: }
156:
157: /**
158: * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpCounter64</CODE> to another OID.
159: * @param source An OID representing an <CODE>SnmpCounter64</CODE> value.
160: * @param dest Where source should be appended.
161: */
162: public static void appendToOid(SnmpOid source, SnmpOid dest) {
163: if (source.getLength() != 1) {
164: throw new IllegalArgumentException();
165: }
166: dest.append(source);
167: }
168:
169: /**
170: * Performs a clone action. This provides a workaround for the
171: * <CODE>SnmpValue</CODE> interface.
172: * @return The SnmpValue clone.
173: */
174: final synchronized public SnmpValue duplicate() {
175: return (SnmpValue) clone();
176: }
177:
178: /**
179: * Clones the <CODE>SnmpCounter64</CODE> object, making a copy of its data.
180: * @return The object clone.
181: */
182: final synchronized public Object clone() {
183: SnmpCounter64 newclone = null;
184: try {
185: newclone = (SnmpCounter64) super .clone();
186: newclone.value = value;
187: } catch (CloneNotSupportedException e) {
188: throw new InternalError(); // vm bug.
189: }
190: return newclone;
191: }
192:
193: /**
194: * Returns a textual description of the type object.
195: * @return ASN.1 textual description.
196: */
197: final public String getTypeName() {
198: return name;
199: }
200:
201: // VARIABLES
202: //----------
203: /**
204: * Name of the type.
205: */
206: final static String name = "Counter64";
207:
208: /**
209: * This is where the value is stored. This long is positive.
210: * @serial
211: */
212: private long value = 0;
213: }
|