Skip to content

Commit

Permalink
Homework dirkriehle#10: ValueObjects:
Browse files Browse the repository at this point in the history
-added final property to coordinate arguments
-added a not yet used HashMap to keep track of allCoordinates and
                                 assure uniqueness(Mapping added maybe
                                 later on!)
Changes: added another ExceptionClass for the Coordinate Classes
          ++ some smaller changes
  • Loading branch information
Peter Koch committed Dec 17, 2017
1 parent 575a2a0 commit 63b2618
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 57 deletions.
38 changes: 34 additions & 4 deletions src/main/java/org/wahlzeit/model/AbstractCoordinate.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/*
* Classname: AbstractCoordinate
*
* Version: 1.1 [8th ADAP-Homework]
* Version: 1.2 [10th ADAP-Homework]
*
* Date: 03.12.2017
* Date: 17.12.2017
*
*/
package org.wahlzeit.model;


import java.util.HashMap;

/**
* An Abstract class to represent different Coordinate classes.
*/
Expand All @@ -18,6 +21,33 @@ public abstract class AbstractCoordinate implements Coordinate {
*/
private static final double DELTA = 1E-6;

/**
* HashSet of all Coordinates to guarantee uniqueness
*/
public static final HashMap<Integer, CartesianCoordinate> allCoordinates = new HashMap<>();

/**
* generates the hashkey using the attributes from CartesianCoordinate for the HashMap "allCoordinates"
*/
public static final int generateHashkeyCartesian(double x, double y, double z){
int hash = 0;
hash += Math.floor(x * 1E6) / 1E6;
hash += Math.floor(y * 1E6) / 1E6;
hash += Math.floor(z * 1E6) / 1E6;
return hash;
}

/**
* generates the hashkey using the attributes from SphericCoordinate for the HashMap "allCoordinates"
*/
public static final int generateHashKeySpheric(double latitude, double longitude, int radius){

double x = radius * Math.cos(latitude) * Math.cos(longitude);
double y = radius * Math.cos(latitude) * Math.sin(longitude);
double z = radius * Math.sin(latitude);
return generateHashkeyCartesian(x, y, z);
}

/**
* @methodtype comparison
* @param coordinate Coordinate you want to compare with
Expand Down Expand Up @@ -119,11 +149,11 @@ private void assertObjectNotNull(Object object){
/**
* Asserts that the given double is not negative
* @methodtype assertion
* @throws IllegalStateException if given distance ist negative
* @throws CoordinateException if given distance ist negative
*/
private void assertDistanceNotNegative(double distance){
if(distance < 0){
throw new IllegalStateException("Calculated distance mustn't be null!");
throw new CoordinateException("Calculated distance mustn't be null!");
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/main/java/org/wahlzeit/model/CartesianCoordinate.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* Classname: CartesianCoordinate
*
* Version: 1.4 [8th ADAP-Homework]
* Version: 1.5 [10th ADAP-Homework]
*
* Date: 03.12.2017
* Date: 17.12.2017
*
*/
package org.wahlzeit.model;
Expand All @@ -17,15 +17,15 @@ public class CartesianCoordinate extends AbstractCoordinate {
/**
* The X value of this coordinate
*/
private double x;
private final double x;
/**
* The Y value of this coordinate
*/
private double y;
private final double y;
/**
* The Z value of this coordinate
*/
private double z;
private final double z;

/**
* lazy initialization of the CartesianCoordinate as SphericCoordinate
Expand Down Expand Up @@ -143,44 +143,44 @@ public int hashCode(){
/**
* Asserts that the given Object is not Null
* @methodtype assertion
* @throws IllegalArgumentException if given Object is null
* @throws CoordinateException if given Object is null
*/
private void assertObjectNotNull(Object object){
if(object == null){
throw new IllegalStateException("Given Object mustn't be null!");
throw new CoordinateException("Given Object mustn't be null!");
}
}

/**
* Asserts that the given Coordinate is instanceOf CartesianCoordinate
* @methodtype assertion
* @throws IllegalStateException if given Coordinate is not instanceOf CartesianCoordinate
* @throws CoordinateException if given Coordinate is not instanceOf CartesianCoordinate
*/
private void assertCoordinateInstanceOfCartesianCoordinate(Coordinate coordinate){
if(!(coordinate instanceof CartesianCoordinate)){
throw new IllegalStateException("Given Coordinate must be a CartesianCoordinate");
throw new CoordinateException("Given Coordinate must be a CartesianCoordinate");
}
}

/**
* Asserts that sphericCoord is Null
* @methodtype assertion
* @throws IllegalArgumentException if sphericCoord is not null
* @throws CoordinateException if sphericCoord is not null
*/
private void assertSphericCoordIsNull(){
if(sphericCoord != null){
throw new IllegalStateException("sphericCoord musn't be initialized twice!");
throw new CoordinateException("sphericCoord musn't be initialized twice!");
}
}

/**
* Asserts that the given double is not negative
* @methodtype assertion
* @throws IllegalStateException if given distance ist negative
* @throws CoordinateException if given distance ist negative
*/
private void assertDistanceNotNegative(double distance){
if(distance < 0){
throw new IllegalStateException("Calculated distance mustn't be null!");
throw new CoordinateException("Calculated distance mustn't be null!");
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/wahlzeit/model/CoordinateException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.wahlzeit.model;

public class CoordinateException extends RuntimeException {

public String errorMsg;

public CoordinateException(){
super();
}

public CoordinateException(String msg){
super(msg);
this.errorMsg = msg;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public class KleinbaerPhotoFactoryException extends RuntimeException {

public String errorMsg;
public RuntimeException propagatedException;

public KleinbaerPhotoFactoryException(){
super();
Expand All @@ -14,9 +13,4 @@ public KleinbaerPhotoFactoryException(String msg){
this.errorMsg = msg;
}

public KleinbaerPhotoFactoryException(String msg, RuntimeException exception){
super(msg);
this.errorMsg = msg;
this.propagatedException = exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public class KleinbaerPhotoManagerException extends RuntimeException {
public String errorMsg;
public RuntimeException propagatedException;

public KleinbaerPhotoManagerException(){
super();
Expand All @@ -12,10 +11,4 @@ public KleinbaerPhotoManagerException(String msg){
super(msg);
this.errorMsg = msg;
}

public KleinbaerPhotoManagerException(String msg, RuntimeException exception){
super(msg);
this.errorMsg = msg;
this.propagatedException = exception;
}
}
14 changes: 7 additions & 7 deletions src/main/java/org/wahlzeit/model/Location.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* Classname: Location
*
* Version: 1.1 [update 2nd ADAP-reviews]
* Version: 1.2 [10th ADAP homework]
*
* Date: 06.11.2017
* Date: 17.12.2017
*
*/
package org.wahlzeit.model;

/**
* Location class which is bound to a specific CartesianCoordinate
* Location class which is bound to a specific Coordinate
*/
public class Location {

Expand All @@ -20,17 +20,17 @@ public class Location {

/**
* @methodtype get
* Getter method for the CartesianCoordinate-class of Location
* @return CartesianCoordinate class or Null if not set
* Getter method for the Coordinate-class of Location
* @return Coordinate class or Null if not set
*/
public Coordinate getCoordinate(){
return this.coordinate;
}

/**
* @methodtype set
* Setter method for the CartesianCoordinate-class of Location
* @param coord CartesianCoordinate to be set for Location
* Setter method for the Coordinate-class of Location
* @param coord Coordinate to be set for Location
*/
public void setCoordinate(Coordinate coord){
coordinate = coord;
Expand Down
37 changes: 17 additions & 20 deletions src/main/java/org/wahlzeit/model/SphericCoordinate.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* Classname: SphericCoordinate
*
* Version: 1.1 [created while doing 7th ADAP-homework]
* Version: 1.2 [10th ADAP-homework]
*
* Date: 25.11.2017
* Date: 17.12.2017
*
*/
package org.wahlzeit.model;
Expand All @@ -17,11 +17,11 @@ public class SphericCoordinate extends AbstractCoordinate {
/**
* north-south position of a point on the Earth's surface from -90 Degrees to 90 Degrees
*/
private double latitude;
private final double latitude;
/**
* east-west position of a point on the Earth's surface from -180 Degrees to 180 Degrees
*/
private double longitude;
private final double longitude;

private CartesianCoordinate cartesianCoord;
/**
Expand All @@ -33,7 +33,7 @@ public class SphericCoordinate extends AbstractCoordinate {
* @methodtype initialization
* Constructs and initializes a spheric coordinate
*/
public SphericCoordinate(double latitude, double longitude){
public SphericCoordinate(double latitude, double longitude) throws IllegalArgumentException{
assertLatitude(latitude);
assertLongitude(longitude);

Expand Down Expand Up @@ -152,22 +152,19 @@ public int hashCode(){
* --------------------------------------- Assertions ---------------------------------------------
*/
/**
* Asserts that Latitude or Longitude never reach invalid values
* Does nothing(Here once laid the great latitude-/longitude check which became obsolete)
* @methodtype assertion
* @throws IllegalStateException if Latitude or Longitude are set to an invalid value
*/
private void assertClassInvariants(){
if(Math.abs(this.latitude) > 90 || Math.abs(this.longitude) > 180){
throw(new IllegalStateException("Latitude or Longitude of coordinate are set to an invalid value!"));
}
//nothing
}

/**
* Assert that given latitude has a valid value
* @methodtype assertion
* @throws IllegalArgumentException if latitude is a invalid value
*/
private void assertLatitude(double latitude){
private void assertLatitude(double latitude) throws IllegalArgumentException{
if(Math.abs(latitude) > 90){
throw(new IllegalArgumentException("latitude mustn't be below -90 or above 90 Degrees"));
}
Expand All @@ -178,7 +175,7 @@ private void assertLatitude(double latitude){
* @methodtype assertion
* @throws IllegalArgumentException if longitude is a invalid value
*/
private void assertLongitude(double longitude){
private void assertLongitude(double longitude) throws IllegalArgumentException{
if(Math.abs(longitude) > 180){
throw(new IllegalArgumentException("latitude mustn't be below -180 or above 180 Degrees"));
}
Expand All @@ -187,44 +184,44 @@ private void assertLongitude(double longitude){
/**
* Asserts that the given Object is not Null
* @methodtype assertion
* @throws IllegalArgumentException if given Object is null
* @throws CoordinateException if given Object is null
*/
private void assertObjectNotNull(Object object){
if(object == null){
throw new IllegalStateException("Given Object mustn't be null!");
throw new CoordinateException("Given Object mustn't be null!");
}
}

/**
* Asserts that the given Coordinate is instanceOf CartesianCoordinate
* @methodtype assertion
* @throws IllegalStateException if given Coordinate is not instanceOf CartesianCoordinate
* @throws CoordinateException if given Coordinate is not instanceOf CartesianCoordinate
*/
private void assertCoordinateInstanceOfSphericCoordinate(Coordinate coordinate){
if(!(coordinate instanceof SphericCoordinate)){
throw new IllegalStateException("Given Coordinate must be a SphericCoordinate");
throw new CoordinateException("Given Coordinate must be a SphericCoordinate");
}
}

/**
* Asserts that cartesianCoord is Null
* @methodtype assertion
* @throws IllegalArgumentException if cartesianCoord is not null
* @throws CoordinateException if cartesianCoord is not null
*/
private void assertCartesianCoordIsNull(){
if(cartesianCoord != null){
throw new IllegalStateException("cartesianCoord musn't be initialized twice!");
throw new CoordinateException("cartesianCoord musn't be initialized twice!");
}
}

/**
* Asserts that the given double is not negative
* @methodtype assertion
* @throws IllegalStateException if given distance ist negative
* @throws CoordinateException if given distance ist negative
*/
private void assertDistanceNotNegative(double distance){
if(distance < 0){
throw new IllegalStateException("Calculated distance mustn't be null!");
throw new CoordinateException("Calculated distance mustn't be null!");
}
}
}

0 comments on commit 63b2618

Please sign in to comment.