1 package calculator.functions;
2
3 import calculator.Expression;
4 import calculator.IllegalConstruction;
5 import calculator.Notation;
6 import calculator.atoms.Complex;
7 import calculator.atoms.IntegerAtom;
8 import calculator.atoms.Rationnal;
9 import calculator.atoms.Real;
10 import visitor.Printer;
11 import visitor.Visitor;
12
13
14
15
16
17
18 public abstract class UnaryFunction implements Expression {
19
20
21
22 public Expression arg;
23
24
25
26
27 protected String symbol;
28
29
30
31
32
33
34
35 protected UnaryFunction(Expression arg)
36 throws IllegalConstruction {
37 if (arg == null) {
38 throw new IllegalConstruction();
39 } else {
40 this.arg = arg;
41 }
42 }
43
44
45
46
47
48
49 public Expression getArg() {
50 return arg;
51 }
52
53
54
55
56
57
58 public String getSymbol() {
59 return symbol;
60 }
61
62 public abstract Real op(Real r);
63
64 public abstract Complex op(Complex c);
65
66 public abstract IntegerAtom op(IntegerAtom i);
67
68 public abstract Rationnal op(Rationnal q);
69
70
71
72
73
74
75
76 public void accept(Visitor v) {
77 v.visit(this);
78 }
79
80 @Override
81 public final String toString() {
82 Printer p = new Printer();
83 this.accept(p);
84 return p.getResult();
85 }
86
87 public final String toString(Notation n) {
88 Printer p = new Printer(n);
89 this.accept(p);
90 return p.getResult();
91 }
92
93 @Override
94 public boolean equals(Object o) {
95 if (o == null)
96 return false;
97 if (this == o)
98 return true;
99 if (getClass() != o.getClass())
100 return false;
101 UnaryFunction other = (UnaryFunction) o;
102 return this.arg.equals(other.getArg());
103 }
104
105 @Override
106 public int hashCode() {
107 return arg.hashCode();
108 }
109 }