More Refactoring + Improvements of getRacePlaces()

This commit is contained in:
Lennart Hensler committed 2012-01-12 14:48:00 +01:00
1 parent 34600328e3
commit 51d874d192
95 files changed
+248 -201

No files matched your search

@@ -5,4 +5,5 @@ Bundle-SymbolicName: com.sap.sailing.domain.common
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: com.sap.sailing.domain.common
Export-Package: com.sap.sailing.domain.common,
com.sap.sailing.domain.common.impl
@@ -1,5 +1,7 @@
package com.sap.sailing.domain.common;
import com.sap.sailing.domain.common.impl.DegreeBearingImpl;
public abstract class AbstractBearing implements Bearing {
@Override
public Bearing reverse() {
@@ -1,5 +1,7 @@
package com.sap.sailing.domain.common;
import com.sap.sailing.domain.common.impl.KilometersPerHourSpeedImpl;
public abstract class AbstractDistance implements Distance {
@Override
@@ -1,5 +1,7 @@
package com.sap.sailing.domain.common;
import com.sap.sailing.domain.common.impl.RadianBearingImpl;
public class AbstractPosition implements Position {
public int hashCode() {
return (int) (4711. * getLngRad() * getLatRad());
@@ -0,0 +1,5 @@
package com.sap.sailing.domain.common;
public interface Named {
String getName();
}
@@ -0,0 +1,112 @@
package com.sap.sailing.domain.common;
import java.util.Comparator;
/**
* 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;
}
}
}
@@ -0,0 +1,8 @@
package com.sap.sailing.domain.common;
public interface RacePlaceOrder extends Named {
Iterable<Placemark> getPlaces();
}
@@ -1,4 +1,10 @@
package com.sap.sailing.domain.common;
package com.sap.sailing.domain.common.impl;
import com.sap.sailing.domain.common.Distance;
import com.sap.sailing.domain.common.Mile;
import com.sap.sailing.domain.common.NauticalMileDistance;
import com.sap.sailing.domain.common.Speed;
import com.sap.sailing.domain.common.TimePoint;
public abstract class AbstractSpeedImpl implements Speed {
@@ -1,4 +1,7 @@
package com.sap.sailing.domain.common;
package com.sap.sailing.domain.common.impl;
import com.sap.sailing.domain.common.AbstractBearing;
import com.sap.sailing.domain.common.Bearing;
public class DegreeBearingImpl extends AbstractBearing implements Bearing {
@@ -1,4 +1,7 @@
package com.sap.sailing.domain.common;
package com.sap.sailing.domain.common.impl;
import com.sap.sailing.domain.common.Mile;
import com.sap.sailing.domain.common.Speed;
public class KilometersPerHourSpeedImpl extends AbstractSpeedImpl implements Speed {
private final double speedInKilometersPerHour;
@@ -0,0 +1,23 @@
package com.sap.sailing.domain.common.impl;
import com.sap.sailing.domain.common.Named;
public class NamedImpl implements Named {
private final String name;
public NamedImpl(String name) {
super();
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
}
@@ -0,0 +1,123 @@
package com.sap.sailing.domain.common.impl;
import com.sap.sailing.domain.common.DegreePosition;
import com.sap.sailing.domain.common.Distance;
import com.sap.sailing.domain.common.Placemark;
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 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;
}
}
@@ -0,0 +1,67 @@
package com.sap.sailing.domain.common.impl;
import com.sap.sailing.domain.common.Placemark;
import com.sap.sailing.domain.common.RacePlaceOrder;
public class RacePlaceOrderImpl extends NamedImpl implements RacePlaceOrder {
private final Iterable<Placemark> places;
public RacePlaceOrderImpl(String raceName, Iterable<Placemark> places) {
super(raceName);
this.places = places;
}
@Override
public Iterable<Placemark> getPlaces() {
return places;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder(getName() + "(");
boolean first = true;
for (Placemark place : places) {
if (first) {
b.append(place.getCountryCode() + ", " + place.getName());
first = false;
} else {
b.append(" -> " + place.getCountryCode() + ", " + place.getName());
}
}
b.append(")");
return b.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((places == null) ? 0 : places.hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().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;
RacePlaceOrderImpl other = (RacePlaceOrderImpl) obj;
if (places == null) {
if (other.places != null)
return false;
} else if (!places.equals(other.places))
return false;
if (getName() == null) {
if (other.getName() != null)
return false;
} else if (!getName().equals(other.getName()))
return false;
return true;
}
}
@@ -1,4 +1,7 @@
package com.sap.sailing.domain.common;
package com.sap.sailing.domain.common.impl;
import com.sap.sailing.domain.common.AbstractBearing;
import com.sap.sailing.domain.common.Bearing;
public class RadianBearingImpl extends AbstractBearing implements Bearing {
private final double bearingRad;