KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - Integer32.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  * The <code>Integer32</code> represents 32bit signed integer values for SNMP.
29  *
30  * @author Frank Fock
31  * @version 1.8
32  */

33 public class Integer32 extends AbstractVariable
34     implements AssignableFromInteger, AssignableFromString {
35
36   private static final long serialVersionUID = 5046132399890132416L;
37
38   private int value = 0;
39
40   /**
41    * Creates an <code>Integer32</code> with a zero value.
42    */

43   public Integer32() {
44   }
45
46   /**
47    * Creates an <code>Integer32</code> variable with the supplied value.
48    * @param value
49    * an integer value.
50    */

51   public Integer32(int value) {
52     setValue(value);
53   }
54
55   public void encodeBER(OutputStream outputStream) throws java.io.IOException JavaDoc {
56     BER.encodeInteger(outputStream, BER.INTEGER, value);
57   }
58
59   public void decodeBER(BERInputStream inputStream) throws java.io.IOException JavaDoc {
60     BER.MutableByte type = new BER.MutableByte();
61     int newValue = BER.decodeInteger(inputStream, type);
62     if (type.getValue() != BER.INTEGER) {
63       throw new IOException("Wrong type encountered when decoding Counter: "+type.getValue());
64     }
65     setValue(newValue);
66   }
67
68   public int getSyntax() {
69     return SMIConstants.SYNTAX_INTEGER;
70   }
71
72   public int hashCode() {
73     return value;
74   }
75
76   public int getBERLength() {
77     if ((value < 0x80) &&
78         (value >= -0x80)) {
79       return 3;
80     }
81     else if ((value < 0x8000) &&
82              (value >= -0x8000)) {
83       return 4;
84     }
85     else if ((value < 0x800000) &&
86              (value >= -0x800000)) {
87       return 5;
88     }
89     return 6;
90   }
91
92   public boolean equals(Object JavaDoc o) {
93     if (o instanceof Integer32) {
94       return ((Integer32)o).value == value;
95     }
96     return false;
97   }
98
99   public int compareTo(Object JavaDoc o) {
100     return value - ((Integer32)o).value;
101   }
102
103   public String JavaDoc toString() {
104     return Integer.toString(value);
105   }
106
107   public final void setValue(String JavaDoc value) {
108     this.value = Integer.parseInt(value);
109   }
110
111   /**
112    * Sets the value of this integer.
113    * @param value
114    * an integer value.
115    */

116   public final void setValue(int value) {
117     this.value = value;
118   }
119
120   /**
121    * Gets the value.
122    * @return
123    * an integer.
124    */

125   public final int getValue() {
126     return value;
127   }
128
129   public Object JavaDoc clone() {
130     return new Integer32(value);
131   }
132
133   public final int toInt() {
134     return getValue();
135   }
136
137   public final long toLong() {
138     return getValue();
139   }
140
141   public OID toSubIndex(boolean impliedLength) {
142     return new OID(new int[] { value });
143   }
144
145   public void fromSubIndex(OID subIndex, boolean impliedLength) {
146     setValue(subIndex.get(0));
147   }
148 }
149
150
Popular Tags