alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (DistanceUnits.java)

This example Lucene source code file (DistanceUnits.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

distanceunits, distanceunits, illegalargumentexception, kilometers, kilometers, miles, miles, miles_kilometres_ratio, miles_kilometres_ratio, string, unknown

The Lucene DistanceUnits.java source code

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.lucene.spatial.geometry;

/**
 * Enum representing difference distance units, currently only kilometers and
 * miles
 */
public enum DistanceUnits {

  MILES("miles", 3959, 24902),
  KILOMETERS("km", 6371, 40076);

  private static final double MILES_KILOMETRES_RATIO = 1.609344;

  private final String unit;
  
  private final double earthCircumference;
  
  private final double earthRadius;

  /**
   * Creates a new DistanceUnit that represents the given unit
   *
   * @param unit Distance unit in String form
   * @param earthRadius Radius of the Earth in the specific distance unit
   * @param earthCircumfence Circumference of the Earth in the specific distance unit
   */
  DistanceUnits(String unit, double earthRadius, double earthCircumfence) {
    this.unit = unit;
    this.earthCircumference = earthCircumfence;
    this.earthRadius = earthRadius;
  }

  /**
   * Returns the DistanceUnit which represents the given unit
   *
   * @param unit Unit whose DistanceUnit should be found
   * @return DistanceUnit representing the unit
   * @throws IllegalArgumentException if no DistanceUnit which represents the given unit is found
   */
  public static DistanceUnits findDistanceUnit(String unit) {
    if (MILES.getUnit().equalsIgnoreCase(unit) || unit.equalsIgnoreCase("mi")) {
      return MILES;
    }

    if (KILOMETERS.getUnit().equalsIgnoreCase(unit)) {
      return KILOMETERS;
    }

    throw new IllegalArgumentException("Unknown distance unit " + unit);
  }

  /**
   * Converts the given distance in given DistanceUnit, to a distance in the unit represented by {@code this} 
   *
   * @param distance Distance to convert
   * @param from Unit to convert the distance from
   * @return Given distance converted to the distance in the given unit
   */
  public double convert(double distance, DistanceUnits from) {
    if (from == this) {
      return distance;
    }
    return (this == MILES) ? distance / MILES_KILOMETRES_RATIO : distance * MILES_KILOMETRES_RATIO;
  }

  /**
   * Returns the string representation of the distance unit
   *
   * @return String representation of the distance unit
   */
  public String getUnit() {
    return unit;
  }
  
  /**
   * Returns the <a href="http://en.wikipedia.org/wiki/Earth_radius">average earth radius
   *
   * @return the average earth radius
   */
  public double earthRadius() {
    return earthRadius;
  }
  
  /**
   * Returns the <a href="http://www.lyberty.com/encyc/articles/earth.html">circumference of the Earth
   * 
   * @return  the circumference of the Earth
   */
  public double earthCircumference() {
    return earthCircumference;
  }
}

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene DistanceUnits.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.