| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| OperableNumber |
|
| 0.0;0 |
| 1 | /* | |
| 2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
| 3 | Licensed under the Academic Free License version 3.0 | |
| 4 | */ | |
| 5 | ||
| 6 | package jaggregate.internal.number; | |
| 7 | ||
| 8 | import java.io.Serializable; | |
| 9 | ||
| 10 | /** | |
| 11 | * Wrapper for basic Java numeric types, that provides a uniform interface with which | |
| 12 | * {@link jaggregate.Interval Interval}s can do arithmetic. | |
| 13 | * | |
| 14 | * @param <E> the type of {@link Number Number} that is wrapped | |
| 15 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 16 | * @version $Id: OperableNumber.java,v 1.1 2008/03/26 18:01:27 pholser Exp $ | |
| 17 | */ | |
| 18 | public abstract class OperableNumber<E extends Number> implements Serializable { | |
| 19 | private static final long serialVersionUID = -1L; | |
| 20 | ||
| 21 | private E wrapped; | |
| 22 | ||
| 23 | /** | |
| 24 | * Creates a new number wrapper. | |
| 25 | * | |
| 26 | * @param wrapped the wrapped number | |
| 27 | */ | |
| 28 | 72499 | protected OperableNumber( E wrapped ) { |
| 29 | 72499 | this.wrapped = wrapped; |
| 30 | 72499 | } |
| 31 | ||
| 32 | /** | |
| 33 | * Tells whether this number is equal to zero. | |
| 34 | * | |
| 35 | * @return {@code true} if this number is zero | |
| 36 | */ | |
| 37 | public abstract boolean isZero(); | |
| 38 | ||
| 39 | /** | |
| 40 | * Adds the given number to this one, returning the sum. | |
| 41 | * | |
| 42 | * @param addend the number to add to this one | |
| 43 | * @return the sum | |
| 44 | */ | |
| 45 | public abstract OperableNumber<E> plus( E addend ); | |
| 46 | ||
| 47 | /** | |
| 48 | * Adds the given integer to this number, returning the sum. | |
| 49 | * | |
| 50 | * @param addend the integer to add to this number | |
| 51 | * @return the sum | |
| 52 | */ | |
| 53 | public abstract OperableNumber<E> plus( int addend ); | |
| 54 | ||
| 55 | /** | |
| 56 | * Adds the given number to this one, returning the sum. | |
| 57 | * | |
| 58 | * @param addend the number to add to this one | |
| 59 | * @return the sum | |
| 60 | */ | |
| 61 | public final OperableNumber<E> plus( OperableNumber<E> addend ) { | |
| 62 | 13400 | return plus( addend.unwrapped() ); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Subtracts the given number from this one, returning the difference. | |
| 67 | * | |
| 68 | * @param subtrahend the number to subtract from this one | |
| 69 | * @return the difference | |
| 70 | */ | |
| 71 | public abstract OperableNumber<E> minus( E subtrahend ); | |
| 72 | ||
| 73 | /** | |
| 74 | * Subtracts the given number from this one, returning the difference. | |
| 75 | * | |
| 76 | * @param subtrahend the number to subtract from this one | |
| 77 | * @return the difference | |
| 78 | */ | |
| 79 | public final OperableNumber<E> minus( OperableNumber<E> subtrahend ) { | |
| 80 | 5099 | return minus( subtrahend.unwrapped() ); |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Multiplies the given number with this one, returning the product. | |
| 85 | * | |
| 86 | * @param multiplier the number to multiply with this one | |
| 87 | * @return the product | |
| 88 | */ | |
| 89 | public abstract OperableNumber<E> times( E multiplier ); | |
| 90 | ||
| 91 | /** | |
| 92 | * Multiplies the given integer with this number, returning the product. | |
| 93 | * | |
| 94 | * @param multiplier the integer to multiply with this number | |
| 95 | * @return the product | |
| 96 | */ | |
| 97 | public abstract OperableNumber<E> times( int multiplier ); | |
| 98 | ||
| 99 | /** | |
| 100 | * Multiplies the given number with this one, returning the product. | |
| 101 | * | |
| 102 | * @param multiplier the number to multiply with this one | |
| 103 | * @return the product | |
| 104 | */ | |
| 105 | public final OperableNumber<E> times( OperableNumber<E> multiplier ) { | |
| 106 | 5099 | return times( multiplier.unwrapped() ); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Divides the given number into this one, returning the quotient. | |
| 111 | * | |
| 112 | * @param divisor the number to divide into this one | |
| 113 | * @return the quotient | |
| 114 | */ | |
| 115 | public abstract OperableNumber<E> dividedBy( E divisor ); | |
| 116 | ||
| 117 | /** | |
| 118 | * Divides the given number into this one, returning the quotient. | |
| 119 | * | |
| 120 | * @param divisor the number to divide into this one | |
| 121 | * @return the quotient | |
| 122 | */ | |
| 123 | public final OperableNumber<E> dividedBy( OperableNumber<E> divisor ) { | |
| 124 | 10198 | return dividedBy( divisor.unwrapped() ); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Divides the given number into this one, returning the modulus. | |
| 129 | * | |
| 130 | * @param divisor the number to divide into this one | |
| 131 | * @return the modulus | |
| 132 | */ | |
| 133 | public abstract OperableNumber<E> modulo( E divisor ); | |
| 134 | ||
| 135 | /** | |
| 136 | * Divides the given number into this one, returning the modulus. | |
| 137 | * | |
| 138 | * @param divisor the number to divide into this one | |
| 139 | * @return the modulus | |
| 140 | */ | |
| 141 | public final OperableNumber<E> modulo( OperableNumber<E> divisor ) { | |
| 142 | 632 | return modulo( divisor.unwrapped() ); |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * Tells whether this number is positive. | |
| 147 | * | |
| 148 | * @return {@code true} if this number is positive | |
| 149 | */ | |
| 150 | public abstract boolean isPositive(); | |
| 151 | ||
| 152 | /** | |
| 153 | * Tells whether this number is negative. | |
| 154 | * | |
| 155 | * @return {@code true} if this number is negative | |
| 156 | */ | |
| 157 | public abstract boolean isNegative(); | |
| 158 | ||
| 159 | /** | |
| 160 | * Tells whether this number is greater than another. | |
| 161 | * | |
| 162 | * @param comparand the number to compare this one with | |
| 163 | * @return {@code true} if this number is {@code > comparand} | |
| 164 | */ | |
| 165 | public abstract boolean greaterThan( E comparand ); | |
| 166 | ||
| 167 | /** | |
| 168 | * Tells whether this number is greater than another. | |
| 169 | * | |
| 170 | * @param comparand the number to compare this one with | |
| 171 | * @return {@code true} if this number is {@code > comparand} | |
| 172 | */ | |
| 173 | public final boolean greaterThan( OperableNumber<E> comparand ) { | |
| 174 | 17453 | return greaterThan( comparand.unwrapped() ); |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * Tells whether this number is greater than or equal to another. | |
| 179 | * | |
| 180 | * @param comparand the number to compare this one with | |
| 181 | * @return {@code true} if this number is {@code >= comparand} | |
| 182 | */ | |
| 183 | public abstract boolean greaterThanOrEqualTo( E comparand ); | |
| 184 | ||
| 185 | /** | |
| 186 | * Tells whether this number is greater than or equal to another. | |
| 187 | * | |
| 188 | * @param comparand the number to compare this one with | |
| 189 | * @return {@code true} if this number is {@code >= comparand} | |
| 190 | */ | |
| 191 | public final boolean greaterThanOrEqualTo( OperableNumber<E> comparand ) { | |
| 192 | 2880 | return greaterThanOrEqualTo( comparand.unwrapped() ); |
| 193 | } | |
| 194 | ||
| 195 | /** | |
| 196 | * Tells whether this number is less than another. | |
| 197 | * | |
| 198 | * @param comparand the number to compare this one with | |
| 199 | * @return {@code true} if this number is {@code < comparand} | |
| 200 | */ | |
| 201 | public abstract boolean lessThan( E comparand ); | |
| 202 | ||
| 203 | /** | |
| 204 | * Tells whether this number is less than another. | |
| 205 | * | |
| 206 | * @param comparand the number to compare this one with | |
| 207 | * @return {@code true} if this number is {@code < comparand} | |
| 208 | */ | |
| 209 | public final boolean lessThan( OperableNumber<E> comparand ) { | |
| 210 | 16753 | return lessThan( comparand.unwrapped() ); |
| 211 | } | |
| 212 | ||
| 213 | /** | |
| 214 | * Tells whether this number is less than or equal to another. | |
| 215 | * | |
| 216 | * @param comparand the number to compare this one with | |
| 217 | * @return {@code true} if this number is {@code <= comparand} | |
| 218 | */ | |
| 219 | public abstract boolean lessThanOrEqualTo( E comparand ); | |
| 220 | ||
| 221 | /** | |
| 222 | * Tells whether this number is less than or equal to another. | |
| 223 | * | |
| 224 | * @param comparand the number to compare this one with | |
| 225 | * @return {@code true} if this number is {@code <= comparand} | |
| 226 | */ | |
| 227 | public final boolean lessThanOrEqualTo( OperableNumber<E> comparand ) { | |
| 228 | 3640 | return lessThanOrEqualTo( comparand.unwrapped() ); |
| 229 | } | |
| 230 | ||
| 231 | /** | |
| 232 | * Gives the number that is the multiplicative inverse of this one. | |
| 233 | * | |
| 234 | * @return this number, negated | |
| 235 | */ | |
| 236 | public abstract OperableNumber<E> negated(); | |
| 237 | ||
| 238 | /** | |
| 239 | * Increments this number by the given amount. | |
| 240 | * | |
| 241 | * @param increment the amount to increment this number by | |
| 242 | * @throws UnsupportedOperationException if the receiver is immutable. The | |
| 243 | * instances returned by the "one" methods are immutable. | |
| 244 | * @see #one() | |
| 245 | */ | |
| 246 | public abstract void incrementBy( OperableNumber<E> increment ); | |
| 247 | ||
| 248 | /** | |
| 249 | * Gives an immutable number that is equal to one. | |
| 250 | * | |
| 251 | * @return an immutable, one-equivalent number | |
| 252 | */ | |
| 253 | public abstract OperableNumber<E> one(); | |
| 254 | ||
| 255 | /** | |
| 256 | * Gives the number that this one wraps. | |
| 257 | * | |
| 258 | * @return the wrapped number | |
| 259 | */ | |
| 260 | public final E unwrapped() { | |
| 261 | 239296 | return wrapped; |
| 262 | } | |
| 263 | ||
| 264 | /** | |
| 265 | * {@inheritDoc} | |
| 266 | */ | |
| 267 | @Override | |
| 268 | public boolean equals( Object that ) { | |
| 269 | 2688 | if ( this == that ) |
| 270 | 672 | return true; |
| 271 | ||
| 272 | 2016 | if ( that == null || !getClass().equals( that.getClass() ) ) |
| 273 | 0 | return false; |
| 274 | ||
| 275 | 2016 | OperableNumber<?> other = (OperableNumber<?>) that; |
| 276 | 2016 | return wrapped.equals( other.unwrapped() ); |
| 277 | } | |
| 278 | ||
| 279 | /** | |
| 280 | * {@inheritDoc} | |
| 281 | */ | |
| 282 | @Override | |
| 283 | public int hashCode() { | |
| 284 | 720 | return wrapped.hashCode(); |
| 285 | } | |
| 286 | ||
| 287 | /** | |
| 288 | * {@inheritDoc} | |
| 289 | */ | |
| 290 | @Override | |
| 291 | public final String toString() { | |
| 292 | 8 | return wrapped.toString(); |
| 293 | } | |
| 294 | ||
| 295 | /** | |
| 296 | * Changes the numbers wrapped by this instance. | |
| 297 | * | |
| 298 | * @param newValue the new wrapped number | |
| 299 | */ | |
| 300 | protected final void update( E newValue ) { | |
| 301 | 5086 | wrapped = newValue; |
| 302 | 5086 | } |
| 303 | } |