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: import com.sun.jmx.snmp.Enumerated;
029:
030: /**
031: * Represents an SNMP integer.
032: *
033: * <p><b>This API is a Sun Microsystems internal API and is subject
034: * to change without notice.</b></p>
035: */
036:
037: public class SnmpInt extends SnmpValue {
038: private static final long serialVersionUID = -7163624758070343373L;
039:
040: // CONSTRUCTORS
041: //-------------
042: /**
043: * Constructs a new <CODE>SnmpInt</CODE> from the specified integer value.
044: * @param v The initialization value.
045: * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
046: * or larger than <CODE>Integer.MAX_VALUE</CODE>.
047: */
048: public SnmpInt(int v) throws IllegalArgumentException {
049: if (isInitValueValid(v) == false) {
050: throw new IllegalArgumentException();
051: }
052: value = (long) v;
053: }
054:
055: /**
056: * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Integer</CODE> value.
057: * @param v The initialization value.
058: * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
059: * or larger than <CODE>Integer.MAX_VALUE</CODE>.
060: */
061: public SnmpInt(Integer v) throws IllegalArgumentException {
062: this (v.intValue());
063: }
064:
065: /**
066: * Constructs a new <CODE>SnmpInt</CODE> from the specified long value.
067: * @param v The initialization value.
068: * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
069: * or larger than <CODE>Integer.MAX_VALUE</CODE>.
070: */
071: public SnmpInt(long v) throws IllegalArgumentException {
072: if (isInitValueValid(v) == false) {
073: throw new IllegalArgumentException();
074: }
075: value = v;
076: }
077:
078: /**
079: * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Long</CODE> value.
080: * @param v The initialization value.
081: * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
082: * or larger than <CODE>Integer.MAX_VALUE</CODE>.
083: */
084: public SnmpInt(Long v) throws IllegalArgumentException {
085: this (v.longValue());
086: }
087:
088: /**
089: * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Enumerated</CODE> value.
090: * @param v The initialization value.
091: * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
092: * or larger than <CODE>Integer.MAX_VALUE</CODE>.
093: * @see Enumerated
094: */
095: public SnmpInt(Enumerated v) throws IllegalArgumentException {
096: this (v.intValue());
097: }
098:
099: /**
100: * Constructs a new <CODE>SnmpInt</CODE> from the specified boolean value.
101: * This constructor applies rfc1903 rule:
102: * <p><blockquote><pre>
103: * TruthValue ::= TEXTUAL-CONVENTION
104: * STATUS current
105: * DESCRIPTION
106: * "Represents a boolean value."
107: * SYNTAX INTEGER { true(1), false(2) }
108: * </pre></blockquote>
109: * @param v The initialization value.
110: */
111: public SnmpInt(boolean v) {
112: value = v ? 1 : 2;
113: }
114:
115: // PUBLIC METHODS
116: //---------------
117: /**
118: * Returns the long value of this <CODE>SnmpInt</CODE>.
119: * @return The value.
120: */
121: public long longValue() {
122: return value;
123: }
124:
125: /**
126: * Converts the integer value to its <CODE>Long</CODE> form.
127: * @return The <CODE>Long</CODE> representation of the value.
128: */
129: public Long toLong() {
130: return new Long(value);
131: }
132:
133: /**
134: * Converts the integer value to its integer form.
135: * @return The integer representation of the value.
136: */
137: public int intValue() {
138: return (int) value;
139: }
140:
141: /**
142: * Converts the integer value to its <CODE>Integer</CODE> form.
143: * @return The <CODE>Integer</CODE> representation of the value.
144: */
145: public Integer toInteger() {
146: return new Integer((int) value);
147: }
148:
149: /**
150: * Converts the integer value to its <CODE>String</CODE> form.
151: * @return The <CODE>String</CODE> representation of the value.
152: */
153: public String toString() {
154: return String.valueOf(value);
155: }
156:
157: /**
158: * Converts the integer value to its <CODE>SnmpOid</CODE> form.
159: * @return The OID representation of the value.
160: */
161: public SnmpOid toOid() {
162: return new SnmpOid(value);
163: }
164:
165: /**
166: * Extracts the integer from an index OID and returns its
167: * value converted as an <CODE>SnmpOid</CODE>.
168: * @param index The index array.
169: * @param start The position in the index array.
170: * @return The OID representing the integer value.
171: * @exception SnmpStatusException There is no integer value
172: * available at the start position.
173: */
174: public static SnmpOid toOid(long[] index, int start)
175: throws SnmpStatusException {
176: try {
177: return new SnmpOid(index[start]);
178: } catch (IndexOutOfBoundsException e) {
179: throw new SnmpStatusException(
180: SnmpStatusException.noSuchName);
181: }
182: }
183:
184: /**
185: * Scans an index OID, skips the integer value and returns the position
186: * of the next value.
187: * @param index The index array.
188: * @param start The position in the index array.
189: * @return The position of the next value.
190: * @exception SnmpStatusException There is no integer value
191: * available at the start position.
192: */
193: public static int nextOid(long[] index, int start)
194: throws SnmpStatusException {
195: if (start >= index.length) {
196: throw new SnmpStatusException(
197: SnmpStatusException.noSuchName);
198: } else {
199: return start + 1;
200: }
201: }
202:
203: /**
204: * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpInt</CODE> to another OID.
205: * @param source An OID representing an <CODE>SnmpInt</CODE> value.
206: * @param dest Where source should be appended.
207: */
208: public static void appendToOid(SnmpOid source, SnmpOid dest) {
209: if (source.getLength() != 1) {
210: throw new IllegalArgumentException();
211: }
212: dest.append(source);
213: }
214:
215: /**
216: * Performs a clone action. This provides a workaround for the
217: * <CODE>SnmpValue</CODE> interface.
218: * @return The <CODE>SnmpValue</CODE> clone.
219: */
220: final synchronized public SnmpValue duplicate() {
221: return (SnmpValue) clone();
222: }
223:
224: /**
225: * Clones the <CODE>SnmpInt</CODE> object, making a copy of its data.
226: * @return The object clone.
227: */
228: final synchronized public Object clone() {
229: SnmpInt newclone = null;
230: try {
231: newclone = (SnmpInt) super .clone();
232: newclone.value = value;
233: } catch (CloneNotSupportedException e) {
234: throw new InternalError(); // vm bug.
235: }
236: return newclone;
237: }
238:
239: /**
240: * Returns a textual description of the type object.
241: * @return ASN.1 textual description.
242: */
243: public String getTypeName() {
244: return name;
245: }
246:
247: /**
248: * This method has been defined to allow the sub-classes
249: * of SnmpInt to perform their own control at intialization time.
250: */
251: boolean isInitValueValid(int v) {
252: if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {
253: return false;
254: }
255: return true;
256: }
257:
258: /**
259: * This method has been defined to allow the sub-classes
260: * of SnmpInt to perform their own control at intialization time.
261: */
262: boolean isInitValueValid(long v) {
263: if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {
264: return false;
265: }
266: return true;
267: }
268:
269: // VARIABLES
270: //----------
271: /**
272: * Name of the type.
273: */
274: final static String name = "Integer32";
275:
276: /**
277: * This is where the value is stored. This long is signed.
278: * @serial
279: */
280: protected long value = 0;
281: }
|