View Javadoc
1   package calculator.atoms.visitor;
2   
3   import calculator.atoms.*;
4   
5   /**
6    * AtomComparator is an AtomVisitor whose goal is to give the largest set of number
7    * between all visited types
8    * e.g. between an Integer, Real and Complex, the Complex type is the largest
9    * as both the Integer and Real are included in Complexes
10   *
11   * @see AtomVisitor
12   */
13  public class AtomComparator extends AtomVisitor {
14  
15  	/* The highestType represents the most englobing type visited */
16  	/* Set by default to Integer (the least englobing) */
17  	private AtomType highestType = AtomType.INTEGER;
18  
19  	/**
20  	 * returns the most englobing type visited
21  	 *
22  	 * @return The most englobing type visited
23  	 */
24  	public AtomType getResult() {
25  		return highestType;
26  	}
27  
28  	/**
29  	 * Constructor of the AtomComparator visitor
30  	 * 
31  	 */
32  	public AtomComparator() {
33  	}
34  
35  	/**
36  	 * Visits a Real and changes the result if it was a Rationnal or Integer
37  	 * 
38  	 * @param r the Real to be visited
39  	 */
40  	@Override
41  	public void visit(Real r) {
42  		// only change the highestType if the actual one is the rationnal
43  		if (highestType == AtomType.RATIONNAL || highestType == AtomType.INTEGER) {
44  			highestType = AtomType.REAL;
45  		}
46  
47  	}
48  
49  	/**
50  	 * Visits a Complex and changes the result to complex as any
51  	 * Number can be converted to a complex
52  	 * 
53  	 * @param c the Complex to be visited
54  	 */
55  	@Override
56  	public void visit(Complex c) {
57  
58  		// sets the highestType to Complex as it is the highestType in regards to
59  		// Rationnals and Reals
60  		highestType = AtomType.COMPLEX;
61  
62  	}
63  
64  	/**
65  	 * Visits a Rationnal and changes the result only if it was an integer
66  	 * 
67  	 * @param q the Rationnal to be visited
68  	 */
69  	@Override
70  	public void visit(Rationnal q) {
71  		if (highestType == AtomType.INTEGER) {
72  			highestType = AtomType.RATIONNAL;
73  		}
74  	}
75  
76  	/**
77  	 * Visits a Integer and doesn't change the result as
78  	 * the integer type is the smallest set
79  	 * 
80  	 * @param i the Integer to be visited
81  	 */
82  	@Override
83  	public void visit(IntegerAtom i) {
84  	}
85  
86  }