KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - Counter32.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 org.snmp4j.asn1.BER;
29 import org.snmp4j.asn1.BERInputStream;
30
31 /**
32  * The <code>Counter32</code> class allows all the functionality of unsigned
33  * integers but is recognized as a distinct SMI type, which is used for
34  * monotonically increasing values that wrap around at 2^32-1 (4294967295).
35  *
36  * @author Frank Fock
37  * @version 1.7
38  * @since 1.0
39  */

40 public class Counter32 extends UnsignedInteger32 {
41
42   private static final long serialVersionUID = 6140742767439142144L;
43
44   public Counter32() {
45   }
46
47   public Counter32(long value) {
48     super(value);
49   }
50
51   public boolean equals(Object JavaDoc o) {
52     if (o instanceof Counter32) {
53       return (((Counter32)o).getValue() == getValue());
54     }
55     return false;
56   }
57
58   public int getSyntax() {
59     return SMIConstants.SYNTAX_COUNTER32;
60   }
61
62   public void encodeBER(OutputStream outputStream) throws IOException {
63     BER.encodeUnsignedInteger(outputStream, BER.COUNTER32, getValue());
64   }
65
66   public void decodeBER(BERInputStream inputStream) throws IOException {
67     BER.MutableByte type = new BER.MutableByte();
68     long newValue = BER.decodeUnsignedInteger(inputStream, type);
69     if (type.getValue() != BER.COUNTER32) {
70       throw new IOException("Wrong type encountered when decoding Counter: "+
71                             type.getValue());
72     }
73     setValue(newValue);
74   }
75
76   public Object JavaDoc clone() {
77     return new Counter32(value);
78   }
79
80   /**
81    * Increment the value of the counter by one. If the current value is
82    * 2^32-1 (4294967295) then value will be set to zero.
83    */

84   public void increment() {
85     if (value < 4294967295l) {
86       value++;
87     }
88     else {
89       value = 0;
90     }
91   }
92
93   public OID toSubIndex(boolean impliedLength) {
94     throw new UnsupportedOperationException JavaDoc();
95   }
96
97   public void fromSubIndex(OID subIndex, boolean impliedLength) {
98     throw new UnsupportedOperationException JavaDoc();
99   }
100
101 }
102
103
Popular Tags