add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / geometry / FixedLatLng.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 FixedLatLng extends LatLng {
26   public static final double SCALE_FACTOR=1000000;
27   public static final int SCALE_FACTOR_INT=1000000;
28   
29   private int lat, lng;
30   private boolean normalized;
31   
32   public FixedLatLng(int lat, int lng) {
33     setLat(lat);
34     setLng(lng);
35   }
36   
37   public FixedLatLng(LatLng ll) {
38     this.lat=ll.getFixedLat();
39     this.lng=ll.getFixedLng();
40   }
41   
42   protected void setLat(int lat) {
43     if (lat>90*SCALE_FACTOR || lat<-90*SCALE_FACTOR) {
44       throw new IllegalArgumentException("Illegal lattitude");
45     }
46     this.lat=lat;
47   }
48
49   protected void setLng(int lng) {
50     this.lng=lng;
51   }
52   
53   public static double fixedToDouble(int fixed) {
54     return (fixed)/SCALE_FACTOR;
55   }
56   
57   public static int doubleToFixed(double d) {
58     return (int)(d*SCALE_FACTOR);
59   }
60   
61   @Override
62   public LatLng copy() {
63     return new FixedLatLng(this);
64   }
65
66   @Override
67   public int getFixedLat() {
68     return lat;
69   }
70
71   @Override
72   public int getFixedLng() {
73     return lng;
74   }
75
76   @Override
77   public double getLat() {
78     return fixedToDouble(lat);
79   }
80
81   @Override
82   public double getLng() {
83     return fixedToDouble(lng);
84   }
85
86   @Override
87   public boolean isFixedPoint() {
88     return true;
89   }
90
91   @Override
92   public FixedLatLng toFixed() {
93     return this;
94   }
95
96   @Override
97   public FloatLatLng toFloat() {
98     return new FloatLatLng(this);
99   }
100
101   @Override
102   public boolean isNormalized() {
103     return 
104       normalized || (
105           (lng>=-180*SCALE_FACTOR_INT) &&
106           (lng<=180*SCALE_FACTOR_INT)
107           );
108   }
109
110   @Override
111   public LatLng normalize() {
112     if (isNormalized()) return this;
113     
114     int delta=0;
115     if (lng<0) delta=360*SCALE_FACTOR_INT;
116     if (lng>=0) delta=-360*SCALE_FACTOR_INT;
117     
118     int newLng=lng;
119     while (newLng<=-180*SCALE_FACTOR_INT || newLng>=180*SCALE_FACTOR_INT) {
120       newLng+=delta;
121     }
122     
123     FixedLatLng ret=new FixedLatLng(lat, newLng);
124     ret.normalized=true;
125     return ret;
126   }
127   
128   @Override
129   public LatLng calculateMidpoint(LatLng other) {
130     return new FixedLatLng(
131         (lat+other.getFixedLat())/2,
132         (lng+other.getFixedLng())/2);
133   }
134
135   @Override
136   public int hashCode() {
137     final int prime = 31;
138     int result = prime + lat;
139     result = prime * result + lng;
140     result = prime * result + (normalized ? 1231 : 1237);
141     return result;
142   }
143
144   @Override
145   public boolean equals(Object obj) {
146     if (this == obj)
147       return true;
148     if (getClass() != obj.getClass())
149       return false;
150     FixedLatLng other = (FixedLatLng) obj;
151     if (lat != other.lat)
152       return false;
153     if (lng != other.lng)
154       return false;
155     if (normalized != other.normalized)
156       return false;
157     return true;
158   }
159   
160 }