Skip to content

Commit

Permalink
Add heading to GenericLocation. Add some more testing of it.
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeholz committed Mar 12, 2013
1 parent 0b86522 commit 1582c4d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
Expand Up @@ -47,6 +47,17 @@ public class GenericLocation {
private Double lat;

private Double lng;

/**
* Observed heading if any.
*
* Direction of travel in decimal degrees from -180° to +180° relative to
* true north.
*
* 0 = heading true north.
* +/-180 = heading south.
*/
private Double heading;

// Pattern for parsing lat,lng strings.
private static final String _doublePattern = "-{0,1}\\d+(\\.\\d+){0,1}";
Expand All @@ -55,15 +66,15 @@ public class GenericLocation {
+ ")(\\s*,\\s*|\\s+)(" + _doublePattern + ")\\s*$");

/**
* Constructs a GenericLocation with coordinates only.
* Constructs an empty GenericLocation.
*/
public GenericLocation() {
this.name = "";
this.place = "";
}

/**
* Constructs an empty GenericLocation.
* Constructs a GenericLocation with coordinates only.
*/
public GenericLocation(double lat, double lng) {
this.name = "";
Expand All @@ -72,6 +83,17 @@ public GenericLocation(double lat, double lng) {
this.lng = lng;
}

/**
* Constructs a GenericLocation with coordinates and heading.
*/
public GenericLocation(double lat, double lng, double heading) {
this.name = "";
this.place = "";
this.lat = lat;
this.lng = lng;
this.heading = heading;
}

/**
* Construct from a name, place pair.
*
Expand Down Expand Up @@ -144,9 +166,14 @@ public Coordinate getCoordinate() {
*/
@Override
public String toString() {
if (this.name == null || this.name.isEmpty()) {
return this.place;
if (this.place != null && !this.place.isEmpty()) {
if (this.name == null || this.name.isEmpty()) {
return this.place;
} else {
return String.format("%s::%s", this.name, this.place);
}
}
return String.format("%s::%s", this.name, this.place);

return String.format("%s,%s", this.lat, this.lng);
}
}
Expand Up @@ -97,4 +97,20 @@ public void testToString() {
assertEquals(input, loc.toString());
}

@Test
public void testFromLatLng() {
GenericLocation loc = new GenericLocation(1.0, 2.0);
Coordinate expectedCoord = new Coordinate(2.0, 1.0);
assertEquals(expectedCoord, loc.getCoordinate());
assertEquals("1.0,2.0", loc.toString());
}

@Test
public void testFromLatLngHeading() {
GenericLocation loc = new GenericLocation(1.0, 2.0, 137.2);
Coordinate expectedCoord = new Coordinate(2.0, 1.0);
assertEquals(expectedCoord, loc.getCoordinate());
assertEquals(137.2, loc.getHeading().doubleValue(), 0.0);
assertEquals("1.0,2.0", loc.toString());
}
}

0 comments on commit 1582c4d

Please sign in to comment.