1 23 package info.monitorenter.util.math; 24 25 39 public class IntegerReuseable { 40 41 45 public static final int MAX_VALUE = 0x7fffffff; 46 47 51 public static final int MIN_VALUE = 0x80000000; 52 53 56 private int m_value; 57 58 65 public IntegerReuseable(final int value) { 66 this.m_value = value; 67 } 68 69 79 public void add(final int i) throws ArithmeticException { 80 int oldval = this.m_value; 81 this.m_value += i; 82 if (oldval > this.m_value) { 83 this.m_value = oldval; 84 throw new ArithmeticException ("Overflow detected. Value saved unchanged."); 85 } 86 } 87 88 98 public void add(final Integer i) throws ArithmeticException { 99 this.add(i.intValue()); 100 } 101 102 112 public void add(final IntegerReuseable i) throws ArithmeticException { 113 this.add(i.getValue()); 114 } 115 116 122 public int getValue() { 123 return this.m_value; 124 } 125 126 132 public int intValue() { 133 return this.m_value; 134 } 135 136 143 public void setValue(final int value) { 144 this.m_value = value; 145 } 146 147 157 public void sub(final int i) throws ArithmeticException { 158 int oldval = this.m_value; 159 this.m_value -= i; 160 if (oldval < this.m_value) { 161 this.m_value = oldval; 162 throw new ArithmeticException ("Carry detected. Value saved unchanged."); 163 } 164 } 165 166 176 public void sub(final Integer i) throws ArithmeticException { 177 this.sub(i.intValue()); 178 } 179 180 190 public void sub(final IntegerReuseable i) throws ArithmeticException { 191 this.sub(i.intValue()); 192 } 193 194 207 public String toString() { 208 return String.valueOf(this.m_value); 209 } 210 } 211 | Popular Tags |