Skip to content

Commit

Permalink
make coordinates immutable and shared
Browse files Browse the repository at this point in the history
  • Loading branch information
dmkif committed Dec 17, 2017
1 parent c1e7676 commit a7d6b00
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/wahlzeit/model/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public Photo(PhotoId myId) {
id = myId;

incWriteCount();
this.setLocation(new Location(new CartesianCoordinate(0,0,0)));
this.setLocation(new Location());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
package org.wahlzeit.model.coordinate;

import java.util.HashMap;
import java.util.Objects;

import org.wahlzeit.utils.DoubleUtil;
Expand All @@ -41,32 +42,32 @@ public class CartesianCoordinate extends AbstractCoordinate implements Coordinat

public static final double DEFAULT_Z_COORDINATE = 0.0;

private double x;

private double y;
private double z;
private final static HashMap<Integer, CartesianCoordinate> coordinateMap = new HashMap<>();

private final double x;
private final double y;
private final double z;

/**
* @methodtype constructor
*/
public CartesianCoordinate() {
private CartesianCoordinate() {
this(CartesianCoordinate.DEFAULT_X_COORDINATE, CartesianCoordinate.DEFAULT_Y_COORDINATE,
CartesianCoordinate.DEFAULT_Z_COORDINATE);
}

/**
* @methodtype constructor
*/
public CartesianCoordinate(double x, double y, double z) throws IllegalArgumentException {
private CartesianCoordinate(double x, double y, double z) throws IllegalArgumentException {
// precondition
assertClassInvariants();
assertIsValidX(x);
assertIsValidY(y);
assertIsValidZ(z);

this.setX(x);
this.setY(y);
this.setZ(z);
this.x = x;
this.y = y;
this.z = z;

// postcondition
assertClassInvariants();
Expand All @@ -78,6 +79,22 @@ public CartesianCoordinate asCartesianCoordinate() {
assertClassInvariants();
return this;
}

/**
* @methodtype helper
*/
public static CartesianCoordinate getInstance(double x, double y, double z) {
CartesianCoordinate tempCoordinate = new CartesianCoordinate(x, y, z);

if(!coordinateMap.containsKey(tempCoordinate.hashCode())) {
synchronized(coordinateMap) {
if(!coordinateMap.containsKey(tempCoordinate.hashCode())) {
coordinateMap.put(tempCoordinate.hashCode(), tempCoordinate);
}
}
}
return coordinateMap.get(tempCoordinate.hashCode());
}

/*
* @methodtype query (non-Javadoc) converts a cartesian coordinate in a spheric
Expand Down Expand Up @@ -157,36 +174,6 @@ public boolean isEqual(Coordinate coordinate) {
return false;
}

/**
* @methodtype set
* @throws IllegalArgumentException
*/
public void setX(double x) throws IllegalArgumentException {
// precondition
assertIsValidX(x);
this.x = x;
}

/**
* @methodtype set
* @throws IllegalArgumentException
*/
public void setY(double y) throws IllegalArgumentException {
// precondition
assertIsValidY(y);
this.y = y;
}

/**
* @methodtype set
* @throws IllegalArgumentException
*/
public void setZ(double z) throws IllegalArgumentException {
// precondition
assertIsValidZ(z);
this.z = z;
}

/**
* @methodtype assertion
*/
Expand Down
95 changes: 50 additions & 45 deletions src/main/java/org/wahlzeit/model/coordinate/SphericCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package org.wahlzeit.model.coordinate;

import java.util.HashMap;
import java.util.Objects;

import org.wahlzeit.utils.DoubleUtil;
Expand All @@ -25,22 +26,19 @@ public class SphericCoordinate extends AbstractCoordinate {
static final double DEFAULT_LONGITUDE = 0.0;

static final double DEFAULT_RADIUS = 6371.0; // km

private final static HashMap<Integer, SphericCoordinate> coordinateMap = new HashMap<>();

private final double latitude;
private final double longitude;
private final double radius;

private double latitude;
private double longitude;
private double radius;

/**
* @methodtype constructor
*/
public SphericCoordinate() throws IllegalArgumentException {
this(SphericCoordinate.DEFAULT_LATITUDE, SphericCoordinate.DEFAULT_LONGITUDE);
}

/**
* @methodtype constructor
*/
public SphericCoordinate(double latitude, double longitude) throws IllegalArgumentException {
private SphericCoordinate(double latitude, double longitude) throws IllegalArgumentException {
this(latitude, longitude, SphericCoordinate.DEFAULT_RADIUS);
}

Expand All @@ -61,27 +59,60 @@ public SphericCoordinate(double latitude, double longitude, double radius) throw
assertClassInvariants();
}


/**
* @methodtype helper
*/
public static SphericCoordinate getInstance(double latitude, double longitude, double radius) {
SphericCoordinate tempCoordinate = new SphericCoordinate(latitude, longitude, radius);

if(!coordinateMap.containsKey(tempCoordinate.hashCode())) {
synchronized(coordinateMap) {
if(!coordinateMap.containsKey(tempCoordinate.hashCode())) {
coordinateMap.put(tempCoordinate.hashCode(), tempCoordinate);
}
}
}
return coordinateMap.get(tempCoordinate.hashCode());
}

/**
* @methodtype helper
*/
public static SphericCoordinate getInstance(double latitude, double longitude) {
return getInstance(latitude, longitude, DEFAULT_RADIUS);
}

/**
* @methodtype helper
*/
public static SphericCoordinate getInstance() {
return getInstance(DEFAULT_LATITUDE, DEFAULT_LONGITUDE, DEFAULT_RADIUS);
}

/**
* @methodtype query transform a SphericCoordinate as a CartesianCoordinate
* source: http://keisan.casio.com/exec/system/1359534351
*/
@Override
public CartesianCoordinate asCartesianCoordinate() {
assertClassInvariants();
double radLatitude = Math.toRadians(this.getLatitude());
double radLongitude = Math.toRadians(this.getLongitude());

double x = this.getRadius() * Math.cos(radLatitude) * Math.sin(radLongitude);
double y = this.getRadius() * Math.sin(radLatitude) * Math.sin(radLongitude);
double z = this.getRadius() * Math.cos(radLongitude);

return new CartesianCoordinate(x, y, z);
return CartesianCoordinate.getInstance(x, y, z);
}

/**
* @methodtype query
*/
@Override
public SphericCoordinate asSphericCoordinate() {
assertClassInvariants();
return this;
}

Expand Down Expand Up @@ -147,7 +178,8 @@ public int hashCode() {
}

/**
* @methodtype query check if is equal and convert if necessary
* @methodtype query
* check if is equal and convert if necessary
*/
@Override
public boolean isEqual(Coordinate coordinate) {
Expand All @@ -160,39 +192,10 @@ public boolean isEqual(Coordinate coordinate) {
&& DoubleUtil.isDoubleEqual(this.getRadius(), spherCoordinate.getRadius());
}

/**
* @methodtype set
*/
public void setLatitude(double latitude) {
// precondition
assertIsNotNull(latitude);
assertIsValidLatitude(latitude);
this.latitude = latitude;
}

/**
* @methodtype set
*/
public void setLongitude(double longitude) {
// precondition
assertIsNotNull(longitude);
assertIsValidLongitude(longitude);
this.longitude = longitude;
}

/**
* @methodtype set
*/
public void setRadius(double radius) {
// precondition
assertIsNotNull(radius);
assertIsValidRadius(radius);
this.radius = radius;
}

/**
* @throws CoordinateParameterException
* @methodtype assertion inpspired by: https://github.com/Snengl/wahlzeit
* @methodtype assertion
* inpspired by: https://github.com/Snengl/wahlzeit
*/
protected void assertIsValidLatitude(double latitude) {
assertIsValidDouble(latitude);
Expand All @@ -204,7 +207,8 @@ protected void assertIsValidLatitude(double latitude) {

/**
* @throws CoordinateParameterException
* @methodtype assertion inpspired by: https://github.com/Snengl/wahlzeit
* @methodtype assertion
* inpspired by: https://github.com/Snengl/wahlzeit
*/
protected void assertIsValidLongitude(double longitude) throws IllegalArgumentException {
assertIsValidDouble(longitude);
Expand All @@ -215,7 +219,8 @@ protected void assertIsValidLongitude(double longitude) throws IllegalArgumentEx

/**
* @throws CoordinateParameterException
* @methodtype assertion inpspired by: https://github.com/Snengl/wahlzeit
* @methodtype assertion
* inpspired by: https://github.com/Snengl/wahlzeit
*/
protected void assertIsValidRadius(double radius) throws IllegalArgumentException {
assertIsValidDouble(radius);
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/wahlzeit/model/LocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class LocationTest {
*/
@Test
public void testLocation() {
assertNotNull(new Location(new CartesianCoordinate(0,0,0)));
assertNotNull(new Location(CartesianCoordinate.getInstance(0,0,0)));
}

@Test(expected=IllegalArgumentException.class)
Expand All @@ -54,9 +54,9 @@ public void testLocationException() {
*/
@Test
public void testGetCoordinate() {
CartesianCoordinate testCoordinate = new CartesianCoordinate(0,0,0);
CartesianCoordinate testCoordinate = CartesianCoordinate.getInstance(0,0,0);
Location withZeroPointCoordinateLocation = new Location(testCoordinate);
Location withNullCoordinateLocation = new Location(new CartesianCoordinate(0.0001,0.0,0.000));
Location withNullCoordinateLocation = new Location(CartesianCoordinate.getInstance(0.0001,0.0,0.000));

assertSame(testCoordinate, withZeroPointCoordinateLocation.getCoordinate());
assertNotNull(withNullCoordinateLocation.getCoordinate());
Expand All @@ -68,7 +68,7 @@ public void testGetCoordinate() {
*/
@Test
public void testSetCoordinate() {
CartesianCoordinate testCoordinate = new CartesianCoordinate(0,0,0);
CartesianCoordinate testCoordinate = CartesianCoordinate.getInstance(0,0,0);
Location testLocation = new Location(testCoordinate);

assertSame(testCoordinate, testLocation.getCoordinate());
Expand All @@ -89,22 +89,22 @@ public void testSetCoordinate() {
*/
@Test
public void testEqualsObject() {
CartesianCoordinate testCoordinate = new CartesianCoordinate(0,0,0);
CartesianCoordinate testCoordinate = CartesianCoordinate.getInstance(0,0,0);
Location firstTestLocation = new Location(testCoordinate);
Location secondTestLocation = new Location(testCoordinate);

assertTrue(firstTestLocation.equals(secondTestLocation));

secondTestLocation.setCoordinate(new CartesianCoordinate(1,1,1));
secondTestLocation.setCoordinate(CartesianCoordinate.getInstance(1,1,1));
assertFalse(firstTestLocation.equals(secondTestLocation));

firstTestLocation.setCoordinate(new CartesianCoordinate(1,1,1));
firstTestLocation.setCoordinate(CartesianCoordinate.getInstance(1,1,1));
assertTrue(firstTestLocation.equals(secondTestLocation));

}

public void testHashCode() {
CartesianCoordinate testCoordinate = new CartesianCoordinate(0,0,0);
CartesianCoordinate testCoordinate = CartesianCoordinate.getInstance(0,0,0);
Location firstTestLocation = new Location(testCoordinate);
Location secondTestLocation = new Location(testCoordinate);

Expand Down
Loading

0 comments on commit a7d6b00

Please sign in to comment.