Fix plot ID issues

This commit is contained in:
Alexander Söderberg 2020-07-23 14:11:34 +02:00
parent da2fa24e74
commit 87285e08dc

View File

@ -36,7 +36,7 @@ import java.util.Iterator;
* Plot (X,Y) tuples for plot locations * Plot (X,Y) tuples for plot locations
* within a plot area * within a plot area
*/ */
public class PlotId { public final class PlotId {
private final int x; private final int x;
private final int y; private final int y;
@ -48,7 +48,7 @@ public class PlotId {
* @param x The plot x coordinate * @param x The plot x coordinate
* @param y The plot y coordinate * @param y The plot y coordinate
*/ */
private PlotId(int x, int y) { private PlotId(final int x, final int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.hash = (this.getX() << 16) | (this.getY() & 0xFFFF); this.hash = (this.getX() << 16) | (this.getY() & 0xFFFF);
@ -61,20 +61,21 @@ public class PlotId {
* @param y The plot y coordinate * @param y The plot y coordinate
*/ */
@Nonnull public static PlotId of(final int x, final int y) { @Nonnull public static PlotId of(final int x, final int y) {
return PlotId.of(x, y); return new PlotId(x, y);
} }
/** /**
* Get a Plot Id based on a string * Get a Plot Id based on a string
* *
* @param string to create id from * @param string to create id from
* @return the PlotId representation of the arguement * @return the PlotId representation of the argument
* @throws IllegalArgumentException if the string does not contain a valid PlotId * @throws IllegalArgumentException if the string does not contain a valid PlotId
*/ */
@Nonnull public static PlotId fromString(@Nonnull String string) { @Nonnull public static PlotId fromString(@Nonnull final String string) {
PlotId plot = fromStringOrNull(string); final PlotId plot = fromStringOrNull(string);
if (plot == null) if (plot == null) {
throw new IllegalArgumentException("Cannot create PlotID. String invalid."); throw new IllegalArgumentException("Cannot create PlotID. String invalid.");
}
return plot; return plot;
} }
@ -84,8 +85,8 @@ public class PlotId {
* @param string ID string * @param string ID string
* @return Plot ID, or {@code null} if none could be parsed * @return Plot ID, or {@code null} if none could be parsed
*/ */
@Nullable public static PlotId fromStringOrNull(@Nonnull String string) { @Nullable public static PlotId fromStringOrNull(@Nonnull final String string) {
String[] parts = string.split("[;,.]"); final String[] parts = string.split("[;,.]");
if (parts.length < 2) { if (parts.length < 2) {
return null; return null;
} }
@ -94,7 +95,7 @@ public class PlotId {
try { try {
x = Integer.parseInt(parts[0]); x = Integer.parseInt(parts[0]);
y = Integer.parseInt(parts[1]); y = Integer.parseInt(parts[1]);
} catch (NumberFormatException ignored) { } catch (final NumberFormatException ignored) {
return null; return null;
} }
return of(x, y); return of(x, y);
@ -126,7 +127,7 @@ public class PlotId {
* @return X component * @return X component
*/ */
public int getX() { public int getX() {
return this.getX(); return this.x;
} }
/** /**
@ -135,7 +136,7 @@ public class PlotId {
* @return Y component * @return Y component
*/ */
public int getY() { public int getY() {
return this.getY(); return this.y;
} }
/** /**
@ -144,8 +145,8 @@ public class PlotId {
* @return Next plot ID * @return Next plot ID
*/ */
@Nonnull public PlotId getNextId() { @Nonnull public PlotId getNextId() {
int absX = Math.abs(x); final int absX = Math.abs(x);
int absY = Math.abs(y); final int absY = Math.abs(y);
if (absX > absY) { if (absX > absY) {
if (x > 0) { if (x > 0) {
return PlotId.of(x, y + 1); return PlotId.of(x, y + 1);