Skip to content

Commit

Permalink
added units of length
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurtis LoVerde committed Mar 3, 2016
1 parent 44b47db commit 8b73068
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* GeographicCoordinateException has been changed from a checked exception to a runtime exception. You can now decide for yourself whether to explicitly catch it during instantiation.
* Added a 'name' field to the Point class
* Added Direction.NEITHER to Latitude and Longitude to represent the Equator and Prime Meridian, since they are neither north, south, east nor west
* Added the international foot, U.S. survey foot and yard as units of length to DistanceCalculator
* Fixed NullPointerException in Latitude/Longitude's .equals() when null was passed


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ public static void main( String args[] ) {
* Units of distance - use this with the {@code distance} methods in this class.
*/
public enum Unit {
// Members are initialized with a conversion factor expressed in terms of one kilometer.
// Members are initialized with a conversion factor expressed in terms of 1 kilometer.

METERS( 1000.0d ),
/**
* This is the international foot. For those in the U.S., yes, that is the
* foot you are accustomed to (12 inches = 1 ft).
*/
FEET( 1000.0d / .3048d ),

MILES( 1.0d / 1.609344d ),
KILOMETERS( 1 ),

KILOMETERS( 1.0d ),
METERS( 1000 ),

MILES( 1.0d / 1.609344d ),

/**
* This is the <strong>international</strong> nautical mile. It's not to be confused with:
Expand All @@ -73,7 +79,28 @@ public enum Unit {
*
* @see <a href="https://en.wikipedia.org/wiki/Nautical_mile">https://en.wikipedia.org/wiki/Nautical_mile</a>
*/
NAUTICAL_MILES( 1.0d / 1.852d );
NAUTICAL_MILES( 1.0d / 1.852d ),

/**
* <p>
* For those of you living in the U.S., the U.S. Survey Foot is NOT the foot
* you think of when you think of feet. That is the
* {@link Unit#INTERNATIONAL_FEET international foot}. The survey foot is
* used in geodetic surveys. As defined by the National Bureau of Standards
* in 1959:
* </p>
*
* <p>
* "Any data expressed in feet derived from and published as a result of geodetic surveys
* within the United States will continue to bear the following relationship as defined
* in 1893: 1 foot = 1200/3937 meter"
* </p>
*
* @see <a href="http://www.ngs.noaa.gov/PUBS_LIB/FedRegister/FRdoc59-5442.pdf">http://www.ngs.noaa.gov/PUBS_LIB/FedRegister/FRdoc59-5442.pdf</a>
*/
US_SURVEY_FEET( 1000.0d / (1200/3937.0d) ),

YARDS( 1000.0d / .9144d );

private double perKilometer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public void testDistance_nullLongitudePoint2() {
}
}

public void testDistance_feet() {
final double distance = DistanceCalculator.distance( Unit.FEET, point1, point2 );
assertEquals( 1070811.8236831673228346456692913d, distance, fpDelta );
}

public void testDistance_kilometers() {
final double distance = DistanceCalculator.distance( Unit.KILOMETERS, point1, point2 );
assertEquals( 326.3834438586294d, distance, fpDelta );
Expand All @@ -154,6 +159,16 @@ public void testDistance_nauticalMiles() {
assertEquals( 176.23296104677613d, distance, fpDelta );
}

public void testDistance_usSurveyfeet() {
final double distance = DistanceCalculator.distance( Unit.US_SURVEY_FEET, point1, point2 );
assertEquals( 1070809.6820595199565d, distance, fpDelta );
}

public void testDistance_yards() {
final double distance = DistanceCalculator.distance( Unit.YARDS, point1, point2 );
assertEquals( 356937.2745610558d, distance, fpDelta );
}

public void testDistance_bothMethodsCalculateSameValues() {
assertEquals( DistanceCalculator.distance( Unit.KILOMETERS, point1, point2 ),
DistanceCalculator.distance( Unit.KILOMETERS, latitude1, longitude1, latitude2, longitude2) );
Expand Down

0 comments on commit 8b73068

Please sign in to comment.