1 package au.gov.amsa.geo.model; 2 3 import java.io.Serializable; 4 5 import au.gov.amsa.risky.format.HasPosition; 6 7 public class Position implements HasPosition, Serializable { 8 9 private static final long serialVersionUID = 4470547471465912154L; 10 11 private final float lat, lon; 12 13 private final int hashCode; 14 15 public Position(float lat, float lon) { 16 this.lat = lat; 17 this.lon = lon; 18 this.hashCode = calculateHashCode(); 19 } 20 21 public Position(double lat, double lon) { 22 this((float) lat, (float) lon); 23 } 24 25 @Override 26 public float lat() { 27 return lat; 28 } 29 30 @Override 31 public float lon() { 32 return lon; 33 } 34 35 @Override 36 public String toString() { 37 return "Position [lat=" + lat + ", lon=" + lon + "]"; 38 } 39 40 @Override 41 public int hashCode() { 42 return hashCode; 43 } 44 45 private int calculateHashCode() { 46 final int prime = 31; 47 int result = 1; 48 long temp; 49 temp = Double.doubleToLongBits(lat); 50 result = prime * result + (int) (temp ^ (temp >>> 32)); 51 temp = Double.doubleToLongBits(lon); 52 result = prime * result + (int) (temp ^ (temp >>> 32)); 53 return result; 54 } 55 56 @Override 57 public boolean equals(Object obj) { 58 if (this == obj) 59 return true; 60 if (obj == null) 61 return false; 62 if (getClass() != obj.getClass()) 63 return false; 64 Position other = (Position) obj; 65 if (Double.doubleToLongBits(lat) != Double.doubleToLongBits(other.lat)) 66 return false; 67 if (Double.doubleToLongBits(lon) != Double.doubleToLongBits(other.lon)) 68 return false; 69 return true; 70 } 71 72 }