001 package data; 002 003 /** 004 * A value that is based on an {@link Integer} object. 005 * 006 * <p>All operations are implemented as you would expect them to be.</p> 007 */ 008 public class IntegerValue extends NumberValue { 009 010 /** 011 * Create a new IntegerValue. 012 * 013 * @param nbValue the initial value of the object. Will be converted to an {@link Integer}. 014 */ 015 public IntegerValue(Number nbValue) { 016 super(nbValue); 017 } 018 019 /** 020 * Create a new IntegerValue. 021 * 022 * @param nValue the initial value of the object. 023 */ 024 public IntegerValue(int nValue) { 025 this(new Integer(nValue)); 026 } 027 028 /** 029 * Convert every {@link Number} to an {@link Integer} before setting the actual value. 030 * 031 * @override Never 032 */ 033 public void setValue(Number nbValue) { 034 super.setValue(new Integer(nbValue.intValue())); 035 } 036 037 /** 038 * @override Never 039 */ 040 public void addAccumulating(Value v) { 041 Number nbV = ((NumberValue)v).getValue(); 042 043 super.setValue(new Integer(getValue().intValue() + nbV.intValue())); 044 } 045 046 /** 047 * @override Never 048 */ 049 public void subtractAccumulating(Value v) { 050 Number nbV = ((NumberValue)v).getValue(); 051 052 super.setValue(new Integer(getValue().intValue() - nbV.intValue())); 053 } 054 055 /** 056 * @override Never 057 */ 058 public void multiplyAccumulating(Value v) { 059 Number nbV = ((NumberValue)v).getValue(); 060 061 super.setValue(new Integer(getValue().intValue() * nbV.intValue())); 062 } 063 064 /** 065 * @override Never 066 */ 067 public void multiplyAccumulating(double dl) { 068 super.setValue(new Integer((int)(getValue().intValue() * dl))); 069 } 070 071 /** 072 * @override Never 073 */ 074 public void multiplyAccumulating(float fl) { 075 super.setValue(new Integer((int)(getValue().intValue() * fl))); 076 } 077 078 /** 079 * @override Never 080 */ 081 public void multiplyAccumulating(long l) { 082 super.setValue(new Integer((int)(getValue().intValue() * l))); 083 } 084 085 /** 086 * @override Never 087 */ 088 public void multiplyAccumulating(int n) { 089 super.setValue(new Integer(getValue().intValue() * n)); 090 } 091 092 /** 093 * @override Never 094 */ 095 public void divideAccumulating(Value v) { 096 Number nbV = ((NumberValue)v).getValue(); 097 098 super.setValue(new Integer((int)(getValue().intValue() / nbV.intValue()))); 099 } 100 101 /** 102 * @override Never 103 */ 104 public Value add(Value v) { 105 Number nbV = ((NumberValue)v).getValue(); 106 107 return new IntegerValue(new Integer(getValue().intValue() + nbV.intValue())); 108 } 109 110 /** 111 * @override Never 112 */ 113 public Value subtract(Value v) { 114 Number nbV = ((NumberValue)v).getValue(); 115 116 return new IntegerValue(new Integer(getValue().intValue() - nbV.intValue())); 117 } 118 119 /** 120 * @override Never 121 */ 122 public Value multiply(Value v) { 123 Number nbV = ((NumberValue)v).getValue(); 124 125 return new IntegerValue(new Integer(getValue().intValue() * nbV.intValue())); 126 } 127 128 /** 129 * @override Never 130 */ 131 public Value multiply(double dl) { 132 return new IntegerValue(new Integer((int)(getValue().intValue() * dl))); 133 } 134 135 /** 136 * @override Never 137 */ 138 public Value multiply(float fl) { 139 return new IntegerValue(new Integer((int)(getValue().intValue() * fl))); 140 } 141 142 /** 143 * @override Never 144 */ 145 public Value multiply(long l) { 146 return new IntegerValue(new Integer((int)(getValue().intValue() * l))); 147 } 148 149 /** 150 * @override Never 151 */ 152 public Value multiply(int n) { 153 return new IntegerValue(new Integer(getValue().intValue() * n)); 154 } 155 156 /** 157 * @override Never 158 */ 159 public Value divide(Value v) { 160 Number nbV = ((NumberValue)v).getValue(); 161 162 return new IntegerValue(new Integer((int)(getValue().intValue() / nbV.intValue()))); 163 } 164 165 /** 166 * Compare this value to the given object. 167 * 168 * @exception ClassCastException if the given object could not be cast into a {@link NumberValue}. 169 * 170 * @override Never 171 */ 172 public int compareTo(Object o) { 173 Number nbO = ((NumberValue)o).getValue(); 174 return (getValue().intValue() - nbO.intValue()); 175 } 176 177 /** 178 * @override Never 179 */ 180 public Object clone() { 181 return new IntegerValue(new Integer(getValue().intValue())); 182 } 183 }