forgotten design improve from Latitude/Longitude Objects introduction
This commit is contained in:
parent
cbbc40c50a
commit
75cfcb1184
|
@ -3,24 +3,30 @@ package cat.hack3.codingtests.marsrover.cartography;
|
||||||
public record Coordinates(Latitude latitude, Longitude longitude) {
|
public record Coordinates(Latitude latitude, Longitude longitude) {
|
||||||
|
|
||||||
public static Coordinates of(long latitude, long longitude) {
|
public static Coordinates of(long latitude, long longitude) {
|
||||||
return new Coordinates(new Latitude(latitude), new Longitude(longitude));
|
return new Coordinates(Latitude.of(latitude), Longitude.of(longitude));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates ofUpdatedLatitude(long newLatitude) {
|
public Coordinates withUpdated(Latitude newLatitude) {
|
||||||
return Coordinates.of(newLatitude, longitude.pointer);
|
return Coordinates.of(newLatitude.pointer, longitude.pointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates ofUpdatedLongitude(long newLongitude) {
|
public Coordinates withUpdated(Longitude newLongitude) {
|
||||||
return Coordinates.of(latitude.pointer, newLongitude);
|
return Coordinates.of(latitude.pointer, newLongitude.pointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
record Latitude(long pointer) {
|
record Latitude(long pointer) {
|
||||||
|
static Latitude of(long pointer) {
|
||||||
|
return new Latitude(pointer);
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.valueOf(pointer);
|
return String.valueOf(pointer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
record Longitude(long pointer) {
|
record Longitude(long pointer) {
|
||||||
|
static Longitude of(long pointer) {
|
||||||
|
return new Longitude(pointer);
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.valueOf(pointer);
|
return String.valueOf(pointer);
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package cat.hack3.codingtests.marsrover.cartography;
|
package cat.hack3.codingtests.marsrover.cartography;
|
||||||
|
|
||||||
|
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Latitude;
|
||||||
|
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Longitude;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notice that the representation is the following:
|
* Notice that the representation is the following:
|
||||||
|
@ -13,7 +15,7 @@ import java.util.logging.Logger;
|
||||||
* 3 3-1 3-2 3-3
|
* 3 3-1 3-2 3-3
|
||||||
*/
|
*/
|
||||||
public class MarsMap {
|
public class MarsMap {
|
||||||
Logger logger = Logger.getLogger(this.getClass().getName());
|
|
||||||
private static final int FIRST_POSITION_IN_MAP = 1;
|
private static final int FIRST_POSITION_IN_MAP = 1;
|
||||||
private static final int INCREMENT_UNIT = 1;
|
private static final int INCREMENT_UNIT = 1;
|
||||||
|
|
||||||
|
@ -41,25 +43,25 @@ public class MarsMap {
|
||||||
private Coordinates getIncrementLatitude() {
|
private Coordinates getIncrementLatitude() {
|
||||||
var latitude = currentPosition.latitude();
|
var latitude = currentPosition.latitude();
|
||||||
long newLatitude = positionResolver.getIncremented(latitude);
|
long newLatitude = positionResolver.getIncremented(latitude);
|
||||||
return currentPosition.ofUpdatedLatitude(newLatitude);
|
return currentPosition.withUpdated(Latitude.of(newLatitude));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Coordinates getDecrementLatitude() {
|
private Coordinates getDecrementLatitude() {
|
||||||
var latitude = currentPosition.latitude();
|
var latitude = currentPosition.latitude();
|
||||||
long newLatitude = positionResolver.getDecremented(latitude);
|
long newLatitude = positionResolver.getDecremented(latitude);
|
||||||
return currentPosition.ofUpdatedLatitude(newLatitude);
|
return currentPosition.withUpdated(Latitude.of(newLatitude));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Coordinates getIncrementLongitude() {
|
private Coordinates getIncrementLongitude() {
|
||||||
var longitude = currentPosition.longitude();
|
var longitude = currentPosition.longitude();
|
||||||
long newLongitude = positionResolver.getIncremented(longitude);
|
long newLongitude = positionResolver.getIncremented(longitude);
|
||||||
return currentPosition.ofUpdatedLongitude(newLongitude);
|
return currentPosition.withUpdated(Longitude.of(newLongitude));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Coordinates getDecrementLongitude() {
|
private Coordinates getDecrementLongitude() {
|
||||||
var longitude = currentPosition.longitude();
|
var longitude = currentPosition.longitude();
|
||||||
long newLongitude = positionResolver.getDecremented(longitude);
|
long newLongitude = positionResolver.getDecremented(longitude);
|
||||||
return currentPosition.ofUpdatedLongitude(newLongitude);
|
return currentPosition.withUpdated(Longitude.of(newLongitude));
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void updateLocation(Coordinates newPosition) {
|
public synchronized void updateLocation(Coordinates newPosition) {
|
||||||
|
|
Loading…
Reference in New Issue