-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kurtis LoVerde
committed
Nov 2, 2014
0 parents
commit 3c5513a
Showing
13 changed files
with
1,520 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src/main/java"/> | ||
<classpathentry kind="src" path="src/test/java"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.gradle | ||
/.settings | ||
/bin | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>GeographicCoordinate</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (c) 2013, Kurtis LoVerde | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
GeographicCoordinate | ||
==================== | ||
|
||
A simple Java library for latitude and longitude |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
apply plugin: "eclipse" | ||
|
||
apply from: buildScriptsDir + "/build_java_lib.gradle" | ||
//apply from: publishScriptsDir + "/publish.gradle" | ||
|
||
dependencies { | ||
testCompile group: "junit", name: "junit", version: "3.8+" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Scripts for building the project. Get BuildScripts from https://github.com/kloverde/BuildScripts. | ||
|
||
buildScriptsDir=../BuildScripts | ||
|
||
|
||
# BuildScripts configuration | ||
|
||
projectName=GeographicCoordinate | ||
releaseVersion=1.0 | ||
specVersion=1.0 | ||
javaSourceCompatibility=1.6 | ||
javaTargetCompatibility=1.6 | ||
jarName=geographiccoordinate | ||
builtByName= | ||
|
||
|
||
# Scripts for publishing to Maven Central and local Ivy server (not publicly released) | ||
|
||
publishScriptsDir=../PublishScripts |
206 changes: 206 additions & 0 deletions
206
src/main/java/org/loverde/geographiccoordinate/GeographicCoordinate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/* | ||
* Copyright (C) 2013 Kurtis LoVerde | ||
* All rights reserved | ||
*/ | ||
|
||
package org.loverde.geographiccoordinate; | ||
|
||
import java.util.Locale; | ||
|
||
|
||
public abstract class GeographicCoordinate | ||
{ | ||
protected static enum Type | ||
{ | ||
LATITUDE, | ||
LONGITUDE | ||
} | ||
|
||
private Type type; | ||
|
||
private int degrees; | ||
private int minutes; | ||
private double seconds; | ||
|
||
private int maxValueDegrees; | ||
|
||
private static final int MAX_VALUE_MINUTES = 59; | ||
private static final double MAX_VALUE_SECONDS = 59.9999999999999d; | ||
|
||
public GeographicCoordinate( final Type type ) | ||
{ | ||
setType( type ); | ||
setMaxValueDegrees( type == Type.LATITUDE ? Latitude.MAX_VALUE : Longitude.MAX_VALUE ); | ||
} | ||
|
||
public GeographicCoordinate( final Type type, final int degrees, final int minutes, final double seconds ) | ||
throws GeographicCoordinateException | ||
{ | ||
this( type ); | ||
|
||
setDegrees( degrees ); | ||
setMinutes( minutes ); | ||
setSeconds( seconds ); | ||
} | ||
|
||
private void setType( final Type t ) | ||
{ | ||
if( t == null ) | ||
{ | ||
throw new IllegalArgumentException( GeographicCoordinateException.Messages.COORDINATE_TYPE_NULL ); | ||
} | ||
|
||
type = t; | ||
} | ||
|
||
private Type getType() | ||
{ | ||
return type; | ||
} | ||
|
||
private void setMaxValueDegrees( final int maxValHrs ) | ||
{ | ||
maxValueDegrees = maxValHrs; | ||
} | ||
|
||
private int getMaxValueDegrees() | ||
{ | ||
return maxValueDegrees; | ||
} | ||
|
||
public void setDegrees( final int deg ) | ||
throws GeographicCoordinateException | ||
{ | ||
if( deg < 0 || deg > getMaxValueDegrees() ) | ||
{ | ||
throw new GeographicCoordinateException( getType().equals(Type.LATITUDE) | ||
? GeographicCoordinateException.Messages.LATITUDE_DEGREES_RANGE | ||
: GeographicCoordinateException.Messages.LONGITUDE_DEGREES_RANGE ); | ||
} | ||
|
||
if( deg == getMaxValueDegrees() && (getMinutes() != 0 || getSeconds() != 0) ) | ||
{ | ||
throw new GeographicCoordinateException( getType().equals(Type.LATITUDE) | ||
? GeographicCoordinateException.Messages.LATITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO | ||
: GeographicCoordinateException.Messages.LONGITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO ); | ||
} | ||
|
||
degrees = deg; | ||
} | ||
|
||
public int getDegrees() | ||
{ | ||
return degrees; | ||
} | ||
|
||
public void setMinutes( final int mins ) | ||
throws GeographicCoordinateException | ||
{ | ||
if( mins < 0 || mins > MAX_VALUE_MINUTES ) | ||
{ | ||
throw new GeographicCoordinateException( getType().equals(Type.LATITUDE) | ||
? GeographicCoordinateException.Messages.LATITUDE_MINUTES_RANGE | ||
: GeographicCoordinateException.Messages.LONGITUDE_MINUTES_RANGE ); | ||
} | ||
|
||
if( getDegrees() == getMaxValueDegrees() && mins != 0 ) | ||
{ | ||
throw new GeographicCoordinateException( getType().equals(Type.LATITUDE) | ||
? GeographicCoordinateException.Messages.LATITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO | ||
: GeographicCoordinateException.Messages.LONGITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO ); | ||
} | ||
|
||
this.minutes = mins; | ||
} | ||
|
||
public int getMinutes() | ||
{ | ||
return minutes; | ||
} | ||
|
||
public void setSeconds( final double seconds ) | ||
throws GeographicCoordinateException | ||
{ | ||
if( seconds < 0.0d || seconds > MAX_VALUE_SECONDS ) | ||
{ | ||
throw new GeographicCoordinateException( getType().equals(Type.LATITUDE) | ||
? GeographicCoordinateException.Messages.LATITUDE_SECONDS_RANGE | ||
: GeographicCoordinateException.Messages.LONGITUDE_SECONDS_RANGE ); | ||
} | ||
|
||
if( getDegrees() == getMaxValueDegrees() && seconds != 0.0d ) | ||
{ | ||
throw new GeographicCoordinateException( getType().equals(Type.LATITUDE) | ||
? GeographicCoordinateException.Messages.LATITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO | ||
: GeographicCoordinateException.Messages.LONGITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO ); | ||
} | ||
|
||
this.seconds = seconds; | ||
} | ||
|
||
public double getSeconds() | ||
{ | ||
return seconds; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
final int prime = 31; | ||
int result = 1; | ||
|
||
result = prime * result + degrees; | ||
result = prime * result + minutes; | ||
|
||
final long temp = Double.doubleToLongBits( seconds ); | ||
result = prime * result + (int) (temp ^ (temp >>> 32)); | ||
|
||
result = prime * result + (type == null ? 0 : type.hashCode()); | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Compares this {@code GeographicCoordinate} to another | ||
* | ||
* @param compareTo The {@code GeographicCoordinate} to compare to | ||
* | ||
* @return true if equivalent, false if not | ||
*/ | ||
@Override | ||
public boolean equals( final Object compareTo ) | ||
throws IllegalArgumentException | ||
{ | ||
final GeographicCoordinate other; | ||
|
||
if( this == compareTo ) return true; | ||
|
||
if( !(compareTo instanceof GeographicCoordinate) ) return false; | ||
|
||
other = (GeographicCoordinate) compareTo; | ||
|
||
if( getType() != null && other.getType() == null ) return false; | ||
if( getType() == null && other.getType() != null ) return false; | ||
if( !getType().equals(other.getType()) ) return false; | ||
|
||
if( getDegrees() != other.getDegrees() ) return false; | ||
|
||
if( getMaxValueDegrees() != other.getMaxValueDegrees() ) return false; | ||
|
||
if( getMinutes() != other.getMinutes() ) return false; | ||
|
||
if( getSeconds() != other.getSeconds() ) return false; | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return String.format( Locale.US, | ||
"Degrees (%d) Minutes (%d) Seconds (%f)", | ||
getDegrees(), | ||
getMinutes(), | ||
getSeconds() ); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/org/loverde/geographiccoordinate/GeographicCoordinateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (C) 2013 Kurtis LoVerde | ||
* All rights reserved | ||
*/ | ||
|
||
package org.loverde.geographiccoordinate; | ||
|
||
|
||
public class GeographicCoordinateException extends Exception | ||
{ | ||
private static final long serialVersionUID = 6477388496558631205L; | ||
|
||
public static final class Messages | ||
{ | ||
private static final String LATITUDE = "Latitude", | ||
LONGITUDE = "Longitude", | ||
|
||
MAX_LATITUDE_DEGREES = "90", | ||
MAX_LONGITUDE_DEGREES = "180", | ||
|
||
DEGREES_RANGE = " degrees must be in a range of 0-", | ||
MINUTES_AND_SECONDS_MUST_BE_ZERO = " minutes and seconds must be 0 when degrees is ", | ||
MINUTES_RANGE = " minutes must be in a range of 0-59", | ||
SECONDS_RANGE = " seconds must be in a range of 0-59.9[..]"; | ||
|
||
public static final String COORDINATE_TYPE_NULL = "Coordinate type is null", | ||
DIRECTION_NULL = "Coordinate direction is null", | ||
|
||
LATITUDE_DEGREES_RANGE = LATITUDE + DEGREES_RANGE + MAX_LATITUDE_DEGREES, | ||
LATITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO = LATITUDE + MINUTES_AND_SECONDS_MUST_BE_ZERO + MAX_LATITUDE_DEGREES, | ||
LATITUDE_MINUTES_RANGE = LATITUDE + MINUTES_RANGE, | ||
LATITUDE_SECONDS_RANGE = LATITUDE + SECONDS_RANGE, | ||
|
||
LONGITUDE_DEGREES_RANGE = LONGITUDE + DEGREES_RANGE + MAX_LONGITUDE_DEGREES, | ||
LONGITUDE_MINUTES_AND_SECONDS_MUST_BE_ZERO = LONGITUDE + MINUTES_AND_SECONDS_MUST_BE_ZERO + MAX_LONGITUDE_DEGREES, | ||
LONGITUDE_MINUTES_RANGE = LONGITUDE + MINUTES_RANGE, | ||
LONGITUDE_SECONDS_RANGE = LONGITUDE + SECONDS_RANGE; | ||
} | ||
|
||
public GeographicCoordinateException() | ||
{ | ||
super(); | ||
} | ||
|
||
public GeographicCoordinateException( final String msg ) | ||
{ | ||
super( msg ); | ||
} | ||
|
||
public GeographicCoordinateException( final Throwable t ) | ||
{ | ||
super( t ); | ||
} | ||
|
||
public GeographicCoordinateException( final String msg, final Throwable t ) | ||
{ | ||
super( msg, t ); | ||
} | ||
} |
Oops, something went wrong.