View Javadoc

1   package org.minetti.astrodevice.server.hibernate.object.embedded;
2   
3   import java.io.Serializable;
4   import javax.persistence.Embeddable;
5   import org.minetti.astrodevice.common.coordinate.Coordinate;
6   import org.minetti.astrodevice.common.coordinate.DecimalCoordinate;
7   import org.minetti.astrodevice.common.coordinate.SexagesimalCoordinate;
8   
9   /**
10   * Object representing a coordinate stored as sexagesimal format (embeddable class for Hibernate
11   * framework).
12   * @author Jean-Philippe MINETTI
13   */
14  @Embeddable
15  public final class EmbeddedSexagesimalCoordinate
16  		implements Serializable {
17  
18  	/**
19  	 * Serial number.
20  	 */
21  	private static final long serialVersionUID = 5380898268847795274L;
22  
23  	/**
24  	 * Angle part of coordinate in degrees (°) or hours (h).
25  	 */
26  	private int angle;
27  
28  	/**
29  	 * Minutes part of coordinate (′).
30  	 */
31  	private int minutes;
32  
33  	/**
34  	 * Seconds part of coordinate (″).
35  	 */
36  	private double seconds;
37  
38  	/**
39  	 * Constructor.
40  	 */
41  	public EmbeddedSexagesimalCoordinate () {
42  		super();
43  	}
44  
45  	/**
46  	 * Constructor.
47  	 * @param hourAngle <code>TRUE</code> if the coordinate must be an hour angle and therefore
48  	 *            expressed in hours.
49  	 * @param coordinate Coordinate.
50  	 */
51  	public EmbeddedSexagesimalCoordinate (final boolean hourAngle, final Coordinate coordinate) {
52  		super();
53  		final Coordinate convertedCoordinate = convert(hourAngle, coordinate);
54  		this.angle = convertedCoordinate.getAngle();
55  		this.minutes = convertedCoordinate.getMinutes();
56  		this.seconds = convertedCoordinate.getSeconds();
57  	}
58  
59  	/**
60  	 * Converts a coordinate in the right unit.
61  	 * @param hourAngle <code>TRUE</code> if the coordinate must be an hour angle and therefore
62  	 *            expressed in hours.
63  	 * @param coordinate Coordinate to convert.
64  	 * @return Converted coordinate.
65  	 */
66  	private static Coordinate convert (final boolean hourAngle, final Coordinate coordinate) {
67  		Coordinate result;
68  		if (hourAngle == coordinate.isHourAngle()) {
69  			result = coordinate;
70  		}
71  		else {
72  			final double value = coordinate.getValue() / (coordinate.isHourAngle() ? 24.0 : 360.0) * (hourAngle ? 24.0 : 360.0);
73  			result = new DecimalCoordinate(hourAngle, value);
74  		}
75  		return result;
76  	}
77  
78  	/**
79  	 * Returns the angle part of coordinate.
80  	 * @return Angle part of coordinate in degrees (°) or hours (h).
81  	 */
82  	public int getAngle () {
83  		return this.angle;
84  	}
85  
86  	/**
87  	 * Sets the angle part of coordinate.
88  	 * @param angle Angle part of coordinate in degrees (°) or hours (h).
89  	 */
90  	public void setAngle (final int angle) {
91  		this.angle = angle;
92  	}
93  
94  	/**
95  	 * Returns the minutes part of coordinate.
96  	 * @return Minutes part of coordinate (′).
97  	 */
98  	public int getMinutes () {
99  		return this.minutes;
100 	}
101 
102 	/**
103 	 * Sets the minutes part of coordinate.
104 	 * @param minutes Minutes part of coordinate (′).
105 	 */
106 	public void setMinutes (final int minutes) {
107 		this.minutes = minutes;
108 	}
109 
110 	/**
111 	 * Returns the seconds part of coordinate.
112 	 * @return Seconds part of coordinate (″).
113 	 */
114 	public double getSeconds () {
115 		return this.seconds;
116 	}
117 
118 	/**
119 	 * Sets the seconds part of coordinate.
120 	 * @param seconds Seconds part of coordinate (″).
121 	 */
122 	public void setSeconds (double seconds) {
123 		this.seconds = seconds;
124 	}
125 
126 	/**
127 	 * Returns the coordinate.
128 	 * @param hourAngle <code>TRUE</code> if the coordinate must be an hour angle and therefore
129 	 *            expressed in hours.
130 	 * @return Coordinate.
131 	 */
132 	public Coordinate toCoordinate (final boolean hourAngle) {
133 		return new SexagesimalCoordinate(hourAngle, this.angle, this.minutes, this.seconds);
134 	}
135 
136 	/*
137 	 * (non-Javadoc)
138 	 * @see java.lang.Object#toString()
139 	 */
140 	@Override
141 	public String toString () {
142 		return "EmbeddedSexagesimalCoordinate[angle=" + this.angle + ", minutes=" + this.minutes + ", seconds=" + this.seconds + "]";
143 	}
144 
145 }