1 /* 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 27 package com.sun.jmx.snmp; 28 29 30 31 import com.sun.jmx.snmp.Enumerated; 32 33 /** 34 * Represents an SNMP integer. 35 * 36 * <p><b>This API is a Sun Microsystems internal API and is subject 37 * to change without notice.</b></p> 38 */ 39 40 public class SnmpInt extends SnmpValue { 41 private static final long serialVersionUID = -7163624758070343373L; 42 43 // CONSTRUCTORS 44 //------------- 45 /** 46 * Constructs a new <CODE>SnmpInt</CODE> from the specified integer 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(int v) throws IllegalArgumentException { 52 if ( isInitValueValid(v) == false ) { 53 throw new IllegalArgumentException() ; 54 } 55 value = (long)v ; 56 } 57 58 /** 59 * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Integer</CODE> value. 60 * @param v The initialization value. 61 * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE> 62 * or larger than <CODE>Integer.MAX_VALUE</CODE>. 63 */ 64 public SnmpInt(Integer v) throws IllegalArgumentException { 65 this(v.intValue()) ; 66 } 67 68 /** 69 * Constructs a new <CODE>SnmpInt</CODE> from the specified long 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 v) throws IllegalArgumentException { 75 if ( isInitValueValid(v) == false ) { 76 throw new IllegalArgumentException() ; 77 } 78 value = v ; 79 } 80 81 /** 82 * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Long</CODE> value. 83 * @param v The initialization value. 84 * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE> 85 * or larger than <CODE>Integer.MAX_VALUE</CODE>. 86 */ 87 public SnmpInt(Long v) throws IllegalArgumentException { 88 this(v.longValue()) ; 89 } 90 91 /** 92 * Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Enumerated</CODE> value. 93 * @param v The initialization value. 94 * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE> 95 * or larger than <CODE>Integer.MAX_VALUE</CODE>. 96 * @see Enumerated 97 */ 98 public SnmpInt(Enumerated v) throws IllegalArgumentException { 99 this(v.intValue()) ; 100 } 101 102 /** 103 * Constructs a new <CODE>SnmpInt</CODE> from the specified boolean value. 104 * This constructor applies rfc1903 rule: 105 * <p><blockquote><pre> 106 * TruthValue ::= TEXTUAL-CONVENTION 107 * STATUS current 108 * DESCRIPTION 109 * "Represents a boolean value." 110 * SYNTAX INTEGER { true(1), false(2) } 111 * </pre></blockquote> 112 * @param v The initialization value. 113 */ 114 public SnmpInt(boolean v) { 115 value = v ? 1 : 2 ; 116 } 117 118 // PUBLIC METHODS 119 //--------------- 120 /** 121 * Returns the long value of this <CODE>SnmpInt</CODE>. 122 * @return The value. 123 */ 124 public long longValue() { 125 return value ; 126 } 127 128 /** 129 * Converts the integer value to its <CODE>Long</CODE> form. 130 * @return The <CODE>Long</CODE> representation of the value. 131 */ 132 public Long toLong() { 133 return value; 134 } 135 136 /** 137 * Converts the integer value to its integer form. 138 * @return The integer representation of the value. 139 */ 140 public int intValue() { 141 return (int) value ; 142 } 143 144 /** 145 * Converts the integer value to its <CODE>Integer</CODE> form. 146 * @return The <CODE>Integer</CODE> representation of the value. 147 */ 148 public Integer toInteger() { 149 return (int)value; 150 } 151 152 /** 153 * Converts the integer value to its <CODE>String</CODE> form. 154 * @return The <CODE>String</CODE> representation of the value. 155 */ 156 public String toString() { 157 return String.valueOf(value) ; 158 } 159 160 /** 161 * Converts the integer value to its <CODE>SnmpOid</CODE> form. 162 * @return The OID representation of the value. 163 */ 164 public SnmpOid toOid() { 165 return new SnmpOid(value) ; 166 } 167 168 /** 169 * Extracts the integer from an index OID and returns its 170 * value converted as an <CODE>SnmpOid</CODE>. 171 * @param index The index array. 172 * @param start The position in the index array. 173 * @return The OID representing the integer value. 174 * @exception SnmpStatusException There is no integer value 175 * available at the start position. 176 */ 177 public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException { 178 try { 179 return new SnmpOid(index[start]) ; 180 } 181 catch(IndexOutOfBoundsException e) { 182 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 183 } 184 } 185 186 /** 187 * Scans an index OID, skips the integer value and returns the position 188 * of the next value. 189 * @param index The index array. 190 * @param start The position in the index array. 191 * @return The position of the next value. 192 * @exception SnmpStatusException There is no integer value 193 * available at the start position. 194 */ 195 public static int nextOid(long[] index, int start) throws SnmpStatusException { 196 if (start >= index.length) { 197 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 198 } 199 else { 200 return start + 1 ; 201 } 202 } 203 204 /** 205 * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpInt</CODE> to another OID. 206 * @param source An OID representing an <CODE>SnmpInt</CODE> value. 207 * @param dest Where source should be appended. 208 */ 209 public static void appendToOid(SnmpOid source, SnmpOid dest) { 210 if (source.getLength() != 1) { 211 throw new IllegalArgumentException() ; 212 } 213 dest.append(source) ; 214 } 215 216 /** 217 * Performs a clone action. This provides a workaround for the 218 * <CODE>SnmpValue</CODE> interface. 219 * @return The <CODE>SnmpValue</CODE> clone. 220 */ 221 final synchronized public SnmpValue duplicate() { 222 return (SnmpValue) clone() ; 223 } 224 225 /** 226 * Clones the <CODE>SnmpInt</CODE> object, making a copy of its data. 227 * @return The object clone. 228 */ 229 final synchronized public Object clone() { 230 SnmpInt newclone = null ; 231 try { 232 newclone = (SnmpInt) super.clone() ; 233 newclone.value = value ; 234 } catch (CloneNotSupportedException e) { 235 throw new InternalError(e) ; // vm bug. 236 } 237 return newclone ; 238 } 239 240 /** 241 * Returns a textual description of the type object. 242 * @return ASN.1 textual description. 243 */ 244 public String getTypeName() { 245 return name ; 246 } 247 248 /** 249 * This method has been defined to allow the sub-classes 250 * of SnmpInt to perform their own control at intialization time. 251 */ 252 boolean isInitValueValid(int v) { 253 if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) { 254 return false; 255 } 256 return true; 257 } 258 259 /** 260 * This method has been defined to allow the sub-classes 261 * of SnmpInt to perform their own control at intialization time. 262 */ 263 boolean isInitValueValid(long v) { 264 if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) { 265 return false; 266 } 267 return true; 268 } 269 270 // VARIABLES 271 //---------- 272 /** 273 * Name of the type. 274 */ 275 final static String name = "Integer32" ; 276 277 /** 278 * This is where the value is stored. This long is signed. 279 * @serial 280 */ 281 protected long value = 0 ; 282 }