01: /*_############################################################################
02: _##
03: _## SNMP4J - Counter32.java
04: _##
05: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
06: _##
07: _## Licensed under the Apache License, Version 2.0 (the "License");
08: _## you may not use this file except in compliance with the License.
09: _## 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>Counter32</code> class allows all the functionality of unsigned
29: * integers but is recognized as a distinct SMI type, which is used for
30: * monotonically increasing values that wrap around at 2^32-1 (4294967295).
31: *
32: * @author Frank Fock
33: * @version 1.7
34: * @since 1.0
35: */
36: public class Counter32 extends UnsignedInteger32 {
37:
38: private static final long serialVersionUID = 6140742767439142144L;
39:
40: public Counter32() {
41: }
42:
43: public Counter32(long value) {
44: super (value);
45: }
46:
47: public boolean equals(Object o) {
48: if (o instanceof Counter32) {
49: return (((Counter32) o).getValue() == getValue());
50: }
51: return false;
52: }
53:
54: public int getSyntax() {
55: return SMIConstants.SYNTAX_COUNTER32;
56: }
57:
58: public void encodeBER(OutputStream outputStream) throws IOException {
59: BER.encodeUnsignedInteger(outputStream, BER.COUNTER32,
60: getValue());
61: }
62:
63: public void decodeBER(BERInputStream inputStream)
64: throws IOException {
65: BER.MutableByte type = new BER.MutableByte();
66: long newValue = BER.decodeUnsignedInteger(inputStream, type);
67: if (type.getValue() != BER.COUNTER32) {
68: throw new IOException(
69: "Wrong type encountered when decoding Counter: "
70: + type.getValue());
71: }
72: setValue(newValue);
73: }
74:
75: public Object clone() {
76: return new Counter32(value);
77: }
78:
79: /**
80: * Increment the value of the counter by one. If the current value is
81: * 2^32-1 (4294967295) then value will be set to zero.
82: */
83: public void increment() {
84: if (value < 4294967295l) {
85: value++;
86: } else {
87: value = 0;
88: }
89: }
90:
91: public OID toSubIndex(boolean impliedLength) {
92: throw new UnsupportedOperationException();
93: }
94:
95: public void fromSubIndex(OID subIndex, boolean impliedLength) {
96: throw new UnsupportedOperationException();
97: }
98:
99: }
|