KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - Counter64.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
22
23
24
25 package org.snmp4j.smi;
26
27 import java.io.*;
28 import java.math.BigInteger JavaDoc;
29 import org.snmp4j.asn1.BER;
30 import org.snmp4j.asn1.BERInputStream;
31
32 /**
33  * The <code>Counter64</code> class represents a 64bit unsigned integer type.
34  * It is used for monotonically increasing values that wrap around at
35  * 2^64-1 (18446744073709551615).
36  * <p>
37  * The unsigned 64bit value is represented by a signed 64bit long value
38  * internally.
39  *
40  * @author Frank Fock
41  * @version 1.8
42  */

43 public class Counter64 extends AbstractVariable
44     implements AssignableFromLong, AssignableFromString {
45
46   private static final long serialVersionUID = 8741539680564150071L;
47
48   private long value = 0;
49
50   public Counter64() {
51   }
52
53   public Counter64(long value) {
54     setValue(value);
55   }
56
57   public void encodeBER(OutputStream outputStream) throws java.io.IOException JavaDoc {
58     BER.encodeUnsignedInt64(outputStream, BER.COUNTER64, value);
59   }
60
61   public void decodeBER(BERInputStream inputStream) throws java.io.IOException JavaDoc {
62     BER.MutableByte type = new BER.MutableByte();
63     long newValue = BER.decodeUnsignedInt64(inputStream, type);
64     if (type.getValue() != BER.COUNTER64) {
65       throw new IOException("Wrong type encountered when decoding Counter64: " +
66                             type.getValue());
67     }
68     setValue(newValue);
69   }
70
71   public int getSyntax() {
72     return SMIConstants.SYNTAX_COUNTER64;
73   }
74
75   public int hashCode() {
76     return (int) value;
77   }
78
79   public int getBERLength() {
80     if (value < 0L) {
81       return 11;
82     }
83     if (value < 0x80000000L) {
84       if (value < 0x8000L) {
85           return (value < 0x80L) ? 3 : 4;
86       }
87       return (value < 0x800000L) ? 5 : 6;
88     }
89     if (value < 0x800000000000L) {
90       return (value < 0x8000000000L) ? 7 : 8;
91     }
92     return (value < 0x80000000000000L) ? 9 : 10;
93   }
94
95   public boolean equals(Object JavaDoc o) {
96     if (o instanceof Counter64) {
97       return ((Counter64) o).value == value;
98     }
99     return false;
100   }
101
102   public int compareTo(Object JavaDoc o) {
103     long other = ((Counter64) o).value;
104     for (int i = 63; i >= 0; i--) {
105       if (((value >> i) & 1) !=
106           ((other >> i) & 1)) {
107         if (((value >> i) & 1) != 0) {
108           return 1;
109         }
110         else {
111           return -1;
112         }
113       }
114     }
115     return 0;
116   }
117
118   public String JavaDoc toString() {
119     if ((value > 0) && (value < Long.MAX_VALUE)) {
120       return Long.toString(value);
121     }
122     byte[] bytes = new byte[8];
123     for (int i = 0; i < 8; i++) {
124       bytes[i] = (byte) ((value >> ((7 - i) * 8)) & 0xFF);
125     }
126     BigInteger JavaDoc i64 = new BigInteger JavaDoc(1, bytes);
127     return i64.toString();
128   }
129
130   public void setValue(String JavaDoc value) {
131     this.value = Long.parseLong(value);
132   }
133
134   public void setValue(long value) {
135     this.value = value;
136   }
137
138   public long getValue() {
139     return value;
140   }
141
142   public Object JavaDoc clone() {
143     return new Counter64(value);
144   }
145
146   /**
147    * Increment the value of the counter by one. If the current value is
148    * 2^63-1 (9223372036854775807) then value will be set to -2^63. Nevertheless,
149    * the BER encoded value of this counter will always be unsigned!
150    */

151   public void increment() {
152     value++;
153   }
154
155   public final int toInt() {
156     return (int)getValue();
157   }
158
159   public final long toLong() {
160     return getValue();
161   }
162
163   public OID toSubIndex(boolean impliedLength) {
164     throw new UnsupportedOperationException JavaDoc();
165   }
166
167   public void fromSubIndex(OID subIndex, boolean impliedLength) {
168     throw new UnsupportedOperationException JavaDoc();
169   }
170
171 }
172
Popular Tags