1 package de.fhdw.wtf.context.model;
2
3 import java.math.BigInteger;
4
5 import de.fhdw.wtf.persistence.meta.IntegerValue;
6
7 /**
8 * Represents integer values in the range [-10^31 up to 10^31-1].
9 */
10 public final class Int extends AnyValue {
11
12 /**
13 * The underlying value.
14 */
15 private final BigInteger value;
16
17 /**
18 * Creates a new Int object from a BigInteger.
19 *
20 * @param value
21 * The integer value.
22 */
23 public Int(final BigInteger value) {
24 this.value = value;
25 }
26
27 /**
28 * Creates a new Int object from an IntegerValue.
29 *
30 * @param value
31 * The integer value.
32 */
33 public Int(final IntegerValue value) {
34 this(value.getValue());
35 }
36
37 /**
38 * Creates a new Int object from a string.
39 *
40 * @param value
41 * The string value denoting an integer.
42 */
43 public Int(final String value) {
44 this(new BigInteger(value));
45 }
46
47 /**
48 * Creates a new Int object from a value of type long.
49 *
50 * @param value
51 * The integer value.
52 */
53 public Int(final long value) {
54 this(BigInteger.valueOf(value));
55 }
56
57 /**
58 * Performs the addition of two Ints.
59 *
60 * @param summand
61 * Another Int object which will be added.
62 * @return The sum of this Int and the other Int.
63 */
64 public Int add(final Int summand) {
65 return new Int(this.value.add(summand.value));
66 }
67
68 /**
69 * Performs the subtraction of two Ints.
70 *
71 * @param subtrahend
72 * Another Int object which will be subtracted.
73 * @return The difference of this Int and the other Int.
74 */
75 public Int sub(final Int subtrahend) {
76 return new Int(this.value.subtract(subtrahend.value));
77 }
78
79 /**
80 * Performs the multiplication of two Ints.
81 *
82 * @param factor
83 * Another Int object which will be multiplied with.
84 * @return The product of this Int and the other Int.
85 */
86 public Int mul(final Int factor) {
87 return new Int(this.value.multiply(factor.value));
88 }
89
90 /**
91 * Performs the integer division of two Ints. The possible remainder of the division is discarded.
92 *
93 * @param divisor
94 * Another Int object which will be used as the divisor.
95 * @return The quotient of this Int and the other Int.
96 */
97 public Int div(final Int divisor) {
98 return new Int(this.value.divide(divisor.value));
99 }
100
101 /**
102 * Compares one Int object to another and checks if this is less than or equal to another.
103 *
104 * @param compareTo
105 * The other Int object.
106 * @return True if this is less than or equal to compareTo.
107 */
108 public boolean lessEq(final Int compareTo) {
109 return this.value.compareTo(compareTo.value) <= 0;
110 }
111
112 @Override
113 public boolean equals(final Object obj) {
114 if (obj instanceof Int) {
115 final Int other = (Int) obj;
116 return this.value.equals(other.value);
117 }
118 return false;
119 }
120
121 /**
122 * Returns the integer value as a BigInteger.
123 *
124 * @return The integer value as a BigInteger.
125 */
126 public BigInteger getVal() {
127 return this.value;
128 }
129
130 @Override
131 public int hashCode() {
132 return this.value.hashCode();
133 }
134
135 @Override
136 public String toString() {
137 return this.value.toString();
138 }
139
140 }