Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/SnmpInt.java
38924 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package com.sun.jmx.snmp;27282930import com.sun.jmx.snmp.Enumerated;3132/**33* Represents an SNMP integer.34*35* <p><b>This API is a Sun Microsystems internal API and is subject36* to change without notice.</b></p>37*/3839public class SnmpInt extends SnmpValue {40private static final long serialVersionUID = -7163624758070343373L;4142// CONSTRUCTORS43//-------------44/**45* Constructs a new <CODE>SnmpInt</CODE> from the specified integer value.46* @param v The initialization value.47* @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>48* or larger than <CODE>Integer.MAX_VALUE</CODE>.49*/50public SnmpInt(int v) throws IllegalArgumentException {51if ( isInitValueValid(v) == false ) {52throw new IllegalArgumentException() ;53}54value = (long)v ;55}5657/**58* Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Integer</CODE> value.59* @param v The initialization value.60* @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>61* or larger than <CODE>Integer.MAX_VALUE</CODE>.62*/63public SnmpInt(Integer v) throws IllegalArgumentException {64this(v.intValue()) ;65}6667/**68* Constructs a new <CODE>SnmpInt</CODE> from the specified long value.69* @param v The initialization value.70* @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>71* or larger than <CODE>Integer.MAX_VALUE</CODE>.72*/73public SnmpInt(long v) throws IllegalArgumentException {74if ( isInitValueValid(v) == false ) {75throw new IllegalArgumentException() ;76}77value = v ;78}7980/**81* Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Long</CODE> value.82* @param v The initialization value.83* @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>84* or larger than <CODE>Integer.MAX_VALUE</CODE>.85*/86public SnmpInt(Long v) throws IllegalArgumentException {87this(v.longValue()) ;88}8990/**91* Constructs a new <CODE>SnmpInt</CODE> from the specified <CODE>Enumerated</CODE> value.92* @param v The initialization value.93* @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>94* or larger than <CODE>Integer.MAX_VALUE</CODE>.95* @see Enumerated96*/97public SnmpInt(Enumerated v) throws IllegalArgumentException {98this(v.intValue()) ;99}100101/**102* Constructs a new <CODE>SnmpInt</CODE> from the specified boolean value.103* This constructor applies rfc1903 rule:104* <p><blockquote><pre>105* TruthValue ::= TEXTUAL-CONVENTION106* STATUS current107* DESCRIPTION108* "Represents a boolean value."109* SYNTAX INTEGER { true(1), false(2) }110* </pre></blockquote>111* @param v The initialization value.112*/113public SnmpInt(boolean v) {114value = v ? 1 : 2 ;115}116117// PUBLIC METHODS118//---------------119/**120* Returns the long value of this <CODE>SnmpInt</CODE>.121* @return The value.122*/123public long longValue() {124return value ;125}126127/**128* Converts the integer value to its <CODE>Long</CODE> form.129* @return The <CODE>Long</CODE> representation of the value.130*/131public Long toLong() {132return new Long(value) ;133}134135/**136* Converts the integer value to its integer form.137* @return The integer representation of the value.138*/139public int intValue() {140return (int) value ;141}142143/**144* Converts the integer value to its <CODE>Integer</CODE> form.145* @return The <CODE>Integer</CODE> representation of the value.146*/147public Integer toInteger() {148return new Integer((int)value) ;149}150151/**152* Converts the integer value to its <CODE>String</CODE> form.153* @return The <CODE>String</CODE> representation of the value.154*/155public String toString() {156return String.valueOf(value) ;157}158159/**160* Converts the integer value to its <CODE>SnmpOid</CODE> form.161* @return The OID representation of the value.162*/163public SnmpOid toOid() {164return new SnmpOid(value) ;165}166167/**168* Extracts the integer from an index OID and returns its169* value converted as an <CODE>SnmpOid</CODE>.170* @param index The index array.171* @param start The position in the index array.172* @return The OID representing the integer value.173* @exception SnmpStatusException There is no integer value174* available at the start position.175*/176public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {177try {178return new SnmpOid(index[start]) ;179}180catch(IndexOutOfBoundsException e) {181throw new SnmpStatusException(SnmpStatusException.noSuchName) ;182}183}184185/**186* Scans an index OID, skips the integer value and returns the position187* of the next value.188* @param index The index array.189* @param start The position in the index array.190* @return The position of the next value.191* @exception SnmpStatusException There is no integer value192* available at the start position.193*/194public static int nextOid(long[] index, int start) throws SnmpStatusException {195if (start >= index.length) {196throw new SnmpStatusException(SnmpStatusException.noSuchName) ;197}198else {199return start + 1 ;200}201}202203/**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*/208public static void appendToOid(SnmpOid source, SnmpOid dest) {209if (source.getLength() != 1) {210throw new IllegalArgumentException() ;211}212dest.append(source) ;213}214215/**216* Performs a clone action. This provides a workaround for the217* <CODE>SnmpValue</CODE> interface.218* @return The <CODE>SnmpValue</CODE> clone.219*/220final synchronized public SnmpValue duplicate() {221return (SnmpValue) clone() ;222}223224/**225* Clones the <CODE>SnmpInt</CODE> object, making a copy of its data.226* @return The object clone.227*/228final synchronized public Object clone() {229SnmpInt newclone = null ;230try {231newclone = (SnmpInt) super.clone() ;232newclone.value = value ;233} catch (CloneNotSupportedException e) {234throw new InternalError(e) ; // vm bug.235}236return newclone ;237}238239/**240* Returns a textual description of the type object.241* @return ASN.1 textual description.242*/243public String getTypeName() {244return name ;245}246247/**248* This method has been defined to allow the sub-classes249* of SnmpInt to perform their own control at intialization time.250*/251boolean isInitValueValid(int v) {252if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {253return false;254}255return true;256}257258/**259* This method has been defined to allow the sub-classes260* of SnmpInt to perform their own control at intialization time.261*/262boolean isInitValueValid(long v) {263if ((v < Integer.MIN_VALUE) || (v > Integer.MAX_VALUE)) {264return false;265}266return true;267}268269// VARIABLES270//----------271/**272* Name of the type.273*/274final static String name = "Integer32" ;275276/**277* This is where the value is stored. This long is signed.278* @serial279*/280protected long value = 0 ;281}282283284