More Refactoring + Improvements of getRacePlaces()

This commit is contained in:
Lennart Hensler
2012-01-12 14:48:00 +01:00
parent 34600328e3
commit 51d874d192
95 changed files with 248 additions and 201 deletions
@@ -1,115 +0,0 @@
package com.sap.sailing.geocoding;
import java.util.Comparator;
import com.sap.sailing.domain.common.Distance;
import com.sap.sailing.domain.common.Position;
/**
* Used to define a populated place in the world.<br />
* Used by {@link ReverseGeocoder}.
* @author Lennart Hensler (D054527)
*
*/
public interface Placemark {
/**
* @return The name of the Placemark
*/
String getName();
/**
* @return The 2 letters country code of the Placemark
*/
String getCountryCode();
/**
* @return The {@link Position} of the Placemark
*/
Position getPosition();
/**
* @return The population of the Placemark
*/
long getPopulation();
/**
* @param position The {@link Position} from which the distance is calculated
* @return The {@link Distance} between the given {@link Position} and the Placemark
*/
Distance distanceFrom(Position position);
/**
* Creates a new {@link Position} out of the given coordinates and calculates the distance between {@link Position} and
* the Placemark via {@link Placemark#distanceFrom(Position) distanceFrom(Position)}.
* @param latDeg Latitude in degrees
* @param lngDeg Longitude in degrees
* @return The {@link Distance} between the given coordinates and the Placemark
*/
Distance distanceFrom(double latDeg, double lngDeg);
/**
* @return The name of the country where the Placemark is located
*/
String getCountryName();
/**
* Sorts the Placemarks by Population from low to high.
* @author Lennart Hensler (D054527)
*
*/
public class ByPopulation implements Comparator<Placemark> {
@Override
public int compare(Placemark p1, Placemark p2) {
return p1.getPopulation() > p2.getPopulation() ? 1 : p1.getPopulation() < p2.getPopulation() ? -1 : 0;
}
}
/**
* Sorts the Placemarks by distance to a {@link Position} from near to far.<br /><br />
*
* If you use this for {@link ReverseGeocoder#getPlacemarkLast(Position, float, Comparator) getPlacemarkBest(Position position,...)} to
* get the nearest Placemark for <code>position</code> you can use {@link ReverseGeocoder#getPlacemarkNearest(Position) getPlacemark} instead.
* <code>getPlacemark</code> returns per default the nearest Placemark for a Position.
* @author Lennart Hensler (D054527)
*
*/
public class ByDistance implements Comparator<Placemark> {
private Position position;
public ByDistance(Position position) {
this.position = position;
}
@Override
public int compare(Placemark p1, Placemark p2) {
Distance d1 = p1.distanceFrom(position);
Distance d2 = p2.distanceFrom(position);
return d1.compareTo(d2);
}
}
public class ByPopulationDistanceRatio implements Comparator<Placemark> {
private Position position;
public ByPopulationDistanceRatio(Position position) {
this.position = position;
}
@Override
public int compare(Placemark p1, Placemark p2) {
double r1 = p1.getPopulation() / p1.distanceFrom(position).getKilometers();
double r2 = p2.getPopulation() / p2.distanceFrom(position).getKilometers();
return r1 > r2 ? 1 : r1 < r2 ? -1 : 0;
}
}
}
@@ -6,6 +6,7 @@ import java.util.List;
import org.json.simple.parser.ParseException;
import com.sap.sailing.domain.common.Placemark;
import com.sap.sailing.domain.common.Position;
import com.sap.sailing.geocoding.impl.ReverseGeocoderImpl;
@@ -1,124 +0,0 @@
package com.sap.sailing.geocoding.impl;
import com.sap.sailing.domain.common.DegreePosition;
import com.sap.sailing.domain.common.Distance;
import com.sap.sailing.domain.common.Position;
import com.sap.sailing.geocoding.Placemark;
import com.sap.sailing.geocoding.ReverseGeocoder;
/**
* Used to define a populated place in the world.<br />
* Used by {@link ReverseGeocoder}.
* @author Lennart Hensler (D054527)
*
*/
public class PlacemarkImpl implements Placemark {
private String name;
private String countryCode;
private String countryName;
private Position position;
private long population;
/**
* Creates a new Placemark with the given parameters as attributes.
*/
public PlacemarkImpl(String name, String countryCode, String countryName,Position position, long population) {
this.name = name;
this.countryCode = countryCode;
this.countryName = countryName;
this.position = position;
this.population = population;
}
/**
* Creates a new Placemark with the given parameters as attributes and a <code>population</code> of <code>0</code>;
*/
public PlacemarkImpl(String name, String countryCode, String countryName, Position position, String type) {
this(name, countryCode, countryName, position, 0);
}
public String getName() {
return name;
}
public String getCountryCode() {
return countryCode;
}
public Position getPosition() {
return position;
}
public long getPopulation() {
return population;
}
@Override
public Distance distanceFrom(Position position) {
return this.position.getDistance(position);
}
@Override
public Distance distanceFrom(double latDeg, double lngDeg) {
Position p = new DegreePosition(latDeg, lngDeg);
return distanceFrom(p);
}
@Override
public String getCountryName() {
return countryName;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder(Placemark.class.getSimpleName() + "[");
b.append(name + ", ");
b.append(countryCode + ", ");
b.append(position.toString() + ", ");
b.append(population + "]");
return b.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((countryCode == null) ? 0 : countryCode.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + (int) (population ^ (population >>> 32));
result = prime * result + ((position == null) ? 0 : position.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PlacemarkImpl other = (PlacemarkImpl) obj;
if (countryCode == null) {
if (other.countryCode != null)
return false;
} else if (!countryCode.equals(other.countryCode))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (population != other.population)
return false;
if (position == null) {
if (other.position != null)
return false;
} else if (!position.equals(other.position))
return false;
return true;
}
}
@@ -17,8 +17,9 @@ import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.sap.sailing.domain.common.DegreePosition;
import com.sap.sailing.domain.common.Placemark;
import com.sap.sailing.domain.common.Position;
import com.sap.sailing.geocoding.Placemark;
import com.sap.sailing.domain.common.impl.PlacemarkImpl;
import com.sap.sailing.geocoding.ReverseGeocoder;
public class ReverseGeocoderImpl implements ReverseGeocoder {