KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > smi > UnsignedInteger32


1 /*_############################################################################
2   _##
3   _## SNMP4J - UnsignedInteger32.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.smi;
22
23 import java.io.*;
24 import org.snmp4j.asn1.BER;
25 import org.snmp4j.asn1.BERInputStream;
26
27 /**
28  * UnsignedInteger32 type is an SNMP type that represents unsigned 32bit
29  * integer values (0 to 4294967295).
30  *
31  * @author Frank Fock
32  * @version 1.8
33  */

34 public class UnsignedInteger32 extends AbstractVariable
35     implements AssignableFromLong, AssignableFromString {
36
37   private static final long serialVersionUID = -2155365655395258383L;
38
39   protected long value = 0;
40
41   public UnsignedInteger32() {
42   }
43
44   /**
45    * Creates an <code>UnsignedInteger32</code> from a <code>long</code> value.
46    * @param value
47    * a <code>long</code> value which must not be greater 2^32-1 and not less
48    * zero.
49    * @throws IllegalArgumentException
50    * if <code>value</code> &lt; 0 or &gt; 2^32-1.
51    */

52   public UnsignedInteger32(long value) {
53     setValue(value);
54   }
55
56   /**
57    * Creates an unsigned integer from a signed int value. Negative values
58    * will become 2^31-1 through 2^32-1.
59    * @param signedIntValue
60    * a signed int value.
61    * @since 1.7
62    */

63   public UnsignedInteger32(int signedIntValue) {
64     setValue(signedIntValue & 0xFFFFFFFFL);
65   }
66
67   /**
68    * Creates an unsigned integer from a signed byte value. Negative values
69    * will become 2^7-1 through 2^8-1.
70    * @param signedByteValue
71    * a signed byte value.
72    * @since 1.7
73    */

74   public UnsignedInteger32(byte signedByteValue) {
75     setValue(signedByteValue & 0xFF);
76   }
77
78   public void encodeBER(OutputStream outputStream) throws java.io.IOException JavaDoc {
79     BER.encodeUnsignedInteger(outputStream, BER.GAUGE, value);
80   }
81
82   public void decodeBER(BERInputStream inputStream) throws java.io.IOException JavaDoc {
83     BER.MutableByte type = new BER.MutableByte();
84     long newValue = BER.decodeUnsignedInteger(inputStream, type);
85     if (type.getValue() != BER.GAUGE) {
86       throw new IOException("Wrong type encountered when decoding Gauge: "+
87                             type.getValue());
88     }
89     setValue(newValue);
90   }
91
92   public int getSyntax() {
93     return SMIConstants.SYNTAX_UNSIGNED_INTEGER32;
94   }
95
96   public int hashCode() {
97     return (int)value;
98   }
99
100   public int getBERLength() {
101     if (value < 0x80L) {
102       return 3;
103     }
104     else if (value < 0x8000L) {
105       return 4;
106     }
107     else if (value < 0x800000L) {
108       return 5;
109     }
110     else if (value < 0x80000000L) {
111       return 6;
112     }
113     return 7;
114   }
115
116   public boolean equals(Object JavaDoc o) {
117     if (o instanceof UnsignedInteger32) {
118       return (((UnsignedInteger32)o).value == value);
119     }
120     return false;
121   }
122
123   public int compareTo(Object JavaDoc o) {
124     long diff = (value - ((UnsignedInteger32)o).getValue());
125     if (diff < 0) {
126       return -1;
127     }
128     else if (diff > 0) {
129       return 1;
130     }
131     return 0;
132   }
133
134   public String JavaDoc toString() {
135     return Long.toString(value);
136   }
137
138   public final void setValue(String JavaDoc value) {
139     setValue(Long.parseLong(value));
140   }
141
142   public void setValue(long value) {
143     if ((value < 0) || (value > 4294967295L)) {
144       throw new IllegalArgumentException JavaDoc(
145           "Argument must be an unsigned 32bit value");
146     }
147     this.value = value;
148   }
149
150   public long getValue() {
151     return value;
152   }
153
154   public Object JavaDoc clone() {
155     return new UnsignedInteger32(value);
156   }
157
158   public final int toInt() {
159     return (int)getValue();
160   }
161
162   public final long toLong() {
163     return getValue();
164   }
165
166   public OID toSubIndex(boolean impliedLength) {
167     return new OID(new int[] { toInt() });
168   }
169
170   public void fromSubIndex(OID subIndex, boolean impliedLength) {
171     setValue(subIndex.getUnsigned(0));
172   }
173
174 }
175
176
Popular Tags