001: /*_############################################################################
002: _##
003: _## SNMP4J - UnsignedInteger32.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: * UnsignedInteger32 type is an SNMP type that represents unsigned 32bit
029: * integer values (0 to 4294967295).
030: *
031: * @author Frank Fock
032: * @version 1.8
033: */
034: public class UnsignedInteger32 extends AbstractVariable implements
035: AssignableFromLong, AssignableFromString {
036:
037: private static final long serialVersionUID = -2155365655395258383L;
038:
039: protected long value = 0;
040:
041: public UnsignedInteger32() {
042: }
043:
044: /**
045: * Creates an <code>UnsignedInteger32</code> from a <code>long</code> value.
046: * @param value
047: * a <code>long</code> value which must not be greater 2^32-1 and not less
048: * zero.
049: * @throws IllegalArgumentException
050: * if <code>value</code> < 0 or > 2^32-1.
051: */
052: public UnsignedInteger32(long value) {
053: setValue(value);
054: }
055:
056: /**
057: * Creates an unsigned integer from a signed int value. Negative values
058: * will become 2^31-1 through 2^32-1.
059: * @param signedIntValue
060: * a signed int value.
061: * @since 1.7
062: */
063: public UnsignedInteger32(int signedIntValue) {
064: setValue(signedIntValue & 0xFFFFFFFFL);
065: }
066:
067: /**
068: * Creates an unsigned integer from a signed byte value. Negative values
069: * will become 2^7-1 through 2^8-1.
070: * @param signedByteValue
071: * a signed byte value.
072: * @since 1.7
073: */
074: public UnsignedInteger32(byte signedByteValue) {
075: setValue(signedByteValue & 0xFF);
076: }
077:
078: public void encodeBER(OutputStream outputStream)
079: throws java.io.IOException {
080: BER.encodeUnsignedInteger(outputStream, BER.GAUGE, value);
081: }
082:
083: public void decodeBER(BERInputStream inputStream)
084: throws java.io.IOException {
085: BER.MutableByte type = new BER.MutableByte();
086: long newValue = BER.decodeUnsignedInteger(inputStream, type);
087: if (type.getValue() != BER.GAUGE) {
088: throw new IOException(
089: "Wrong type encountered when decoding Gauge: "
090: + type.getValue());
091: }
092: setValue(newValue);
093: }
094:
095: public int getSyntax() {
096: return SMIConstants.SYNTAX_UNSIGNED_INTEGER32;
097: }
098:
099: public int hashCode() {
100: return (int) value;
101: }
102:
103: public int getBERLength() {
104: if (value < 0x80L) {
105: return 3;
106: } else if (value < 0x8000L) {
107: return 4;
108: } else if (value < 0x800000L) {
109: return 5;
110: } else if (value < 0x80000000L) {
111: return 6;
112: }
113: return 7;
114: }
115:
116: public boolean equals(Object o) {
117: if (o instanceof UnsignedInteger32) {
118: return (((UnsignedInteger32) o).value == value);
119: }
120: return false;
121: }
122:
123: public int compareTo(Object o) {
124: long diff = (value - ((UnsignedInteger32) o).getValue());
125: if (diff < 0) {
126: return -1;
127: } else if (diff > 0) {
128: return 1;
129: }
130: return 0;
131: }
132:
133: public String toString() {
134: return Long.toString(value);
135: }
136:
137: public final void setValue(String value) {
138: setValue(Long.parseLong(value));
139: }
140:
141: public void setValue(long value) {
142: if ((value < 0) || (value > 4294967295L)) {
143: throw new IllegalArgumentException(
144: "Argument must be an unsigned 32bit value");
145: }
146: this .value = value;
147: }
148:
149: public long getValue() {
150: return value;
151: }
152:
153: public Object clone() {
154: return new UnsignedInteger32(value);
155: }
156:
157: public final int toInt() {
158: return (int) getValue();
159: }
160:
161: public final long toLong() {
162: return getValue();
163: }
164:
165: public OID toSubIndex(boolean impliedLength) {
166: return new OID(new int[] { toInt() });
167: }
168:
169: public void fromSubIndex(OID subIndex, boolean impliedLength) {
170: setValue(subIndex.getUnsigned(0));
171: }
172:
173: }
|