001/* 002 * Copyright 2011 Marc Wick, geonames.org 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 */ 017package org.geonames.utils; 018 019import java.util.Comparator; 020 021import org.geonames.WikipediaArticle; 022 023/** 024 * @author marc 025 * @since 16.02.2011 026 * 027 */ 028public class DistanceOrder implements Comparator<WikipediaArticle> { 029 private double latitude; 030 private double longitude; 031 032 public DistanceOrder(double pLat, double pLng) { 033 latitude = pLat; 034 longitude = pLng; 035 } 036 037 public int compare(WikipediaArticle o1, WikipediaArticle o2) { 038 double dist1 = dist(o1); 039 double dist2 = dist(o2); 040 return Double.compare(dist1, dist2); 041 } 042 043 private double dist(WikipediaArticle o) { 044 return Distance.distanceKM(latitude, longitude, o.getLatitude(), o 045 .getLongitude()); 046 } 047 048}