001: /*_############################################################################
002: _##
003: _## SNMP4J - Integer32.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.smi;
022:
023: import java.io.*;
024: import org.snmp4j.asn1.BER;
025: import org.snmp4j.asn1.BERInputStream;
026:
027: /**
028: * The <code>Integer32</code> represents 32bit signed integer values for SNMP.
029: *
030: * @author Frank Fock
031: * @version 1.8
032: */
033: public class Integer32 extends AbstractVariable implements
034: AssignableFromInteger, AssignableFromString {
035:
036: private static final long serialVersionUID = 5046132399890132416L;
037:
038: private int value = 0;
039:
040: /**
041: * Creates an <code>Integer32</code> with a zero value.
042: */
043: public Integer32() {
044: }
045:
046: /**
047: * Creates an <code>Integer32</code> variable with the supplied value.
048: * @param value
049: * an integer value.
050: */
051: public Integer32(int value) {
052: setValue(value);
053: }
054:
055: public void encodeBER(OutputStream outputStream)
056: throws java.io.IOException {
057: BER.encodeInteger(outputStream, BER.INTEGER, value);
058: }
059:
060: public void decodeBER(BERInputStream inputStream)
061: throws java.io.IOException {
062: BER.MutableByte type = new BER.MutableByte();
063: int newValue = BER.decodeInteger(inputStream, type);
064: if (type.getValue() != BER.INTEGER) {
065: throw new IOException(
066: "Wrong type encountered when decoding Counter: "
067: + type.getValue());
068: }
069: setValue(newValue);
070: }
071:
072: public int getSyntax() {
073: return SMIConstants.SYNTAX_INTEGER;
074: }
075:
076: public int hashCode() {
077: return value;
078: }
079:
080: public int getBERLength() {
081: if ((value < 0x80) && (value >= -0x80)) {
082: return 3;
083: } else if ((value < 0x8000) && (value >= -0x8000)) {
084: return 4;
085: } else if ((value < 0x800000) && (value >= -0x800000)) {
086: return 5;
087: }
088: return 6;
089: }
090:
091: public boolean equals(Object o) {
092: if (o instanceof Integer32) {
093: return ((Integer32) o).value == value;
094: }
095: return false;
096: }
097:
098: public int compareTo(Object o) {
099: return value - ((Integer32) o).value;
100: }
101:
102: public String toString() {
103: return Integer.toString(value);
104: }
105:
106: public final void setValue(String value) {
107: this .value = Integer.parseInt(value);
108: }
109:
110: /**
111: * Sets the value of this integer.
112: * @param value
113: * an integer value.
114: */
115: public final void setValue(int value) {
116: this .value = value;
117: }
118:
119: /**
120: * Gets the value.
121: * @return
122: * an integer.
123: */
124: public final int getValue() {
125: return value;
126: }
127:
128: public Object clone() {
129: return new Integer32(value);
130: }
131:
132: public final int toInt() {
133: return getValue();
134: }
135:
136: public final long toLong() {
137: return getValue();
138: }
139:
140: public OID toSubIndex(boolean impliedLength) {
141: return new OID(new int[] { value });
142: }
143:
144: public void fromSubIndex(OID subIndex, boolean impliedLength) {
145: setValue(subIndex.get(0));
146: }
147: }
|