1 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 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 43 public Integer32() { 44 } 45 46 51 public Integer32(int value) { 52 setValue(value); 53 } 54 55 public void encodeBER(OutputStream outputStream) throws java.io.IOException { 56 BER.encodeInteger(outputStream, BER.INTEGER, value); 57 } 58 59 public void decodeBER(BERInputStream inputStream) throws java.io.IOException { 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 o) { 93 if (o instanceof Integer32) { 94 return ((Integer32)o).value == value; 95 } 96 return false; 97 } 98 99 public int compareTo(Object o) { 100 return value - ((Integer32)o).value; 101 } 102 103 public String toString() { 104 return Integer.toString(value); 105 } 106 107 public final void setValue(String value) { 108 this.value = Integer.parseInt(value); 109 } 110 111 116 public final void setValue(int value) { 117 this.value = value; 118 } 119 120 125 public final int getValue() { 126 return value; 127 } 128 129 public Object 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 |