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