1 package calculator.atoms;
2
3 import calculator.Expression;
4 import calculator.atoms.visitor.AtomVisitor;
5 import calculator.operations.Operation;
6 import visitor.Visitor;
7 import calculator.functions.*;
8
9 /**
10 * IntegerAtom is a concrete class that represents arithmetic (integer) numbers,
11 * which are Atoms, a special kind of Expressions, just like operations are.
12 *
13 * @see Expression
14 * @see Operation
15 */
16 public class IntegerAtom implements Atom {
17
18 /* The internal integer value */
19 private int value;
20
21 /**
22 * Constructor method
23 *
24 * @param i The integer value
25 */
26 public IntegerAtom(int i) {
27 value = i;
28 }
29
30 /**
31 * accept method to implement the visitor design pattern to traverse Atoms.
32 * Each Integer will pass itself to the Atomvisitor object to get processed
33 * by the Atomvisitor.
34 *
35 * @param v The Atomvisitor object
36 */
37 @Override
38 public void accept(AtomVisitor v) {
39 v.visit(this);
40 }
41
42 /**
43 * accept method to implement the visitor design pattern to traverse arithmetic
44 * expressions.
45 * Each Integer number will pass itself to the visitor object to get processed
46 * by the visitor.
47 *
48 * @param v The visitor object
49 */
50 @Override
51 public void accept(Visitor v) {
52 v.visit(this);
53 }
54
55 /**
56 * Two Integers Atoms are equal if the values they contain are equal
57 *
58 * @param o The object to compare to
59 * @return A boolean representing the result of the equality test
60 */
61 @Override
62 public boolean equals(Object o) {
63 // No object should be equal to null (not including this check can result in an
64 // exception if a IntegerAtom is tested against null)
65 if (o == null)
66 return false;
67
68 // If the object is compared to itself then return true
69 if (o == this) {
70 return true;
71 }
72
73 // If the object is of another type then return false
74 if (!(o instanceof IntegerAtom)) {
75 return false;
76 }
77 // return the equality between
78 return this.value == ((IntegerAtom) o).getValue();
79 }
80
81 /**
82 * The method hashCode needs to be overridden it the equals method is
83 * overridden;
84 * otherwise there may be problems when you use your object in hashed
85 * collections
86 * such as HashMap, HashSet, LinkedHashSet.
87 *
88 * @return The result of computing the hash.
89 */
90 @Override
91 public int hashCode() {
92 return value;
93 }
94
95 /**
96 * applies an operation between two IntegerAtom
97 *
98 * @param o the operation to apply
99 * @param a the other IntegerAtom
100 * @return The result of the application of o on a and this instance
101 */
102 public Atom apply(Operation o, Atom a) {
103 return o.op(this, (IntegerAtom) a);
104 }
105
106 /**
107 * applies a unary operation to the IntegerAtom
108 *
109 * @param o the operation to apply
110 * @return The result of the application
111 */
112 @Override
113 public IntegerAtom apply(UnaryFunction o) {
114 return o.op(this);
115 }
116
117 @Override
118 public Atom apply(BinaryFunction f, Atom a) {
119 return f.op(this, (IntegerAtom) a);
120 }
121
122 /**
123 * returns the value of the integer
124 *
125 * @return the value of the integer
126 */
127 public int getValue() {
128 return value;
129 }
130
131 @Override
132 public String toString() { return Integer.toString(value); }
133
134 }