View Javadoc
1   package calculator.atoms;
2   
3   import calculator.Expression;
4   import calculator.operations.Operation;
5   import calculator.atoms.visitor.AtomVisitor;
6   import visitor.Visitor;
7   import java.util.Objects;
8   import calculator.functions.*;
9   
10  /**
11   * Complex Number type is used to represent complex e.g. 3 + 2i
12   * 
13   * @see Expression
14   * @see Operation
15   * @see Atom
16   */
17  public class Complex implements Atom {
18  
19  	private final org.apache.commons.numbers.complex.Complex value;
20  
21  	/**
22  	 * Constructor method
23  	 * 
24  	 * @param real      The real part
25  	 * @param imaginary The imaginary part
26  	 */
27  	public Complex(double real, double imaginary) {
28  		this.value = org.apache.commons.numbers.complex.Complex.ofCartesian(real, imaginary);
29  	}
30  
31  	/**
32  	 * Constructor method from an existing Complex object
33  	 * 
34  	 * @param value The commons-numbers Complex object
35  	 */
36  	public Complex(org.apache.commons.numbers.complex.Complex value) {
37  		this.value = value;
38  	}
39  
40  	/**
41  	 * Returns a Complex representing a NaN
42  	 *
43  	 * @return a NaN Complex
44  	 */
45  	public static Complex nan() {
46  		return new Complex(org.apache.commons.numbers.complex.Complex.ofCartesian(Double.NaN, Double.NaN));
47  	}
48  
49  	/**
50  	 * Returns if a complex is a NaN
51  	 *
52  	 * @return true iff the complex represents a NaN
53  	 */
54  	public boolean isNaN() {
55  		return value.isNaN();
56  
57  	}
58  
59  	/**
60  	 * getter method to obtain the value contained in the object
61  	 *
62  	 * @return The commons-numbers Complex number contained in the object
63  	 */
64  	public org.apache.commons.numbers.complex.Complex getValue() {
65  		return value;
66  	}
67  
68  	@Override
69  	public Complex apply(Operation o, Atom a) {
70  		return o.op(this, (Complex) a);
71  	}
72  
73  	@Override
74  	public Atom apply(BinaryFunction f, Atom a) {
75  		return f.op(this, (Complex) a);
76  	}
77  
78  	@Override
79  	public Complex apply(UnaryFunction o) {
80  		return o.op(this);
81  	}
82  
83  	@Override
84  	public void accept(AtomVisitor v) {
85  		v.visit(this);
86  	}
87  
88  	@Override
89  	public void accept(Visitor v) {
90  		v.visit(this);
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 (!(o instanceof Complex))
100 			return false;
101 		Complex other = (Complex) o;
102 		return this.value.equals(other.getValue());
103 	}
104 
105 	@Override
106 	public int hashCode() {
107 		return Objects.hash(value);
108 	}
109 
110 	@Override
111 	public String toString() {return value.toString(); }
112 
113 }