add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / geometry / DistanceUnits.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  * Enum representing difference distance units, currently only kilometers and
22  * miles
23  */
24 public enum DistanceUnits {
25
26   MILES("miles", 3959, 24902),
27   KILOMETERS("km", 6371, 40076);
28
29   private static final double MILES_KILOMETRES_RATIO = 1.609344;
30
31   private final String unit;
32   
33   private final double earthCircumference;
34   
35   private final double earthRadius;
36
37   /**
38    * Creates a new DistanceUnit that represents the given unit
39    *
40    * @param unit Distance unit in String form
41    * @param earthRadius Radius of the Earth in the specific distance unit
42    * @param earthCircumfence Circumference of the Earth in the specific distance unit
43    */
44   DistanceUnits(String unit, double earthRadius, double earthCircumfence) {
45     this.unit = unit;
46     this.earthCircumference = earthCircumfence;
47     this.earthRadius = earthRadius;
48   }
49
50   /**
51    * Returns the DistanceUnit which represents the given unit
52    *
53    * @param unit Unit whose DistanceUnit should be found
54    * @return DistanceUnit representing the unit
55    * @throws IllegalArgumentException if no DistanceUnit which represents the given unit is found
56    */
57   public static DistanceUnits findDistanceUnit(String unit) {
58     if (MILES.getUnit().equalsIgnoreCase(unit) || unit.equalsIgnoreCase("mi")) {
59       return MILES;
60     }
61
62     if (KILOMETERS.getUnit().equalsIgnoreCase(unit)) {
63       return KILOMETERS;
64     }
65
66     throw new IllegalArgumentException("Unknown distance unit " + unit);
67   }
68
69   /**
70    * Converts the given distance in given DistanceUnit, to a distance in the unit represented by {@code this} 
71    *
72    * @param distance Distance to convert
73    * @param from Unit to convert the distance from
74    * @return Given distance converted to the distance in the given unit
75    */
76   public double convert(double distance, DistanceUnits from) {
77     if (from == this) {
78       return distance;
79     }
80     return (this == MILES) ? distance / MILES_KILOMETRES_RATIO : distance * MILES_KILOMETRES_RATIO;
81   }
82
83   /**
84    * Returns the string representation of the distance unit
85    *
86    * @return String representation of the distance unit
87    */
88   public String getUnit() {
89     return unit;
90   }
91   
92   /**
93    * Returns the <a href="http://en.wikipedia.org/wiki/Earth_radius">average earth radius</a>
94    *
95    * @return the average earth radius
96    */
97   public double earthRadius() {
98     return earthRadius;
99   }
100   
101   /**
102    * Returns the <a href="http://www.lyberty.com/encyc/articles/earth.html">circumference of the Earth</a>
103    * 
104    * @return  the circumference of the Earth
105    */
106   public double earthCircumference() {
107     return earthCircumference;
108   }
109 }
110