pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / geometry / FloatLatLng.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.spatial.geometry;
19
20 /**
21  * <p><font color="red"><b>NOTE:</b> This API is still in
22  * flux and might change in incompatible ways in the next
23  * release.</font>
24  */
25 public class FloatLatLng extends LatLng {
26   private double lat;
27   private double lng;
28   private boolean normalized;
29   
30   public FloatLatLng(double lat, double lng) {
31     if (lat>90.0 || lat<-90.0) throw new IllegalArgumentException("Illegal latitude value " + lat);
32     this.lat=lat;
33     this.lng=lng;
34   }
35   
36   public FloatLatLng(LatLng ll) {
37     this.lat=ll.getLat();
38     this.lng=ll.getLng();
39   }
40   
41   @Override
42   public LatLng copy() {
43     return new FloatLatLng(this);
44   }
45
46   @Override
47   public int getFixedLat() {
48     return FixedLatLng.doubleToFixed(this.lat);
49   }
50
51   @Override
52   public int getFixedLng() {
53     return FixedLatLng.doubleToFixed(this.lng);
54   }
55
56   @Override
57   public double getLat() {
58     return this.lat;
59   }
60
61   @Override
62   public double getLng() {
63     return this.lng;
64   }
65
66   @Override
67   public boolean isFixedPoint() {
68     return false;
69   }
70
71   @Override
72   public FixedLatLng toFixed() {
73     return new FixedLatLng(this);
74   }
75
76   @Override
77   public FloatLatLng toFloat() {
78     return this;
79   }
80   
81   @Override
82   public boolean isNormalized() {
83     return 
84       normalized || (
85           (lng>=-180) &&
86           (lng<=180)
87           );
88   }
89
90   @Override
91   public LatLng normalize() {
92     if (isNormalized()) return this;
93     
94     double delta=0;
95     if (lng<0) delta=360;
96     if (lng>=0) delta=-360;
97     
98     double newLng=lng;
99     while (newLng<=-180 || newLng>=180) {
100       newLng+=delta;
101     }
102     
103     FloatLatLng ret=new FloatLatLng(lat, newLng);
104     ret.normalized=true;
105     return ret;
106   }
107
108   @Override
109   public LatLng calculateMidpoint(LatLng other) {
110     return new FloatLatLng(
111         (lat+other.getLat())/2.0,
112         (lng+other.getLng())/2.0);
113   }
114
115   @Override
116   public int hashCode() {
117     final int prime = 31;
118     long temp;
119     temp = Double.doubleToLongBits(lat);
120     int result = prime  + (int) (temp ^ (temp >>> 32));
121     temp = Double.doubleToLongBits(lng);
122     result = prime * result + (int) (temp ^ (temp >>> 32));
123     result = prime * result + (normalized ? 1231 : 1237);
124     return result;
125   }
126
127   @Override
128   public boolean equals(Object obj) {
129     if (this == obj)
130       return true;
131     if (getClass() != obj.getClass())
132       return false;
133     FloatLatLng other = (FloatLatLng) obj;
134     if (Double.doubleToLongBits(lat) != Double.doubleToLongBits(other.lat))
135       return false;
136     if (Double.doubleToLongBits(lng) != Double.doubleToLongBits(other.lng))
137       return false;
138     if (normalized != other.normalized)
139       return false;
140     return true;
141   }
142
143 }