Skip to content

Commit

Permalink
Make the Vector classes into Records (#2477)
Browse files Browse the repository at this point in the history
* Make the Vector classes into Records

* Drop custom equals and hashCode methods in Vector/BlockVector classes
  • Loading branch information
me4502 authored Mar 7, 2024
1 parent a72eb2d commit 0df2b6a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* An immutable 2-dimensional vector.
*/
public final class BlockVector2 {
public record BlockVector2(int x, int z) {

public static final BlockVector2 ZERO = new BlockVector2(0, 0);
public static final BlockVector2 UNIT_X = new BlockVector2(1, 0);
Expand Down Expand Up @@ -74,25 +74,13 @@ public static BlockVector2 at(int x, int z) {
return new BlockVector2(x, z);
}

private final int x;
private final int z;

/**
* Construct an instance.
*
* @param x the X coordinate
* @param z the Z coordinate
*/
private BlockVector2(int x, int z) {
this.x = x;
this.z = z;
}

/**
* Get the X coordinate.
*
* @return the x coordinate
* @deprecated use {@link #x()} instead
*/
@Deprecated(forRemoval = true)
public int getX() {
return x;
}
Expand All @@ -101,7 +89,9 @@ public int getX() {
* Get the X coordinate.
*
* @return the x coordinate
* @deprecated use {@link #x()} instead
*/
@Deprecated(forRemoval = true)
public int getBlockX() {
return x;
}
Expand All @@ -120,7 +110,9 @@ public BlockVector2 withX(int x) {
* Get the Z coordinate.
*
* @return the z coordinate
* @deprecated use {@link #z()} instead
*/
@Deprecated(forRemoval = true)
public int getZ() {
return z;
}
Expand All @@ -129,7 +121,9 @@ public int getZ() {
* Get the Z coordinate.
*
* @return the z coordinate
* @deprecated use {@link #z()} instead
*/
@Deprecated(forRemoval = true)
public int getBlockZ() {
return z;
}
Expand Down Expand Up @@ -536,21 +530,6 @@ public BlockVector3 toBlockVector3(int y) {
return BlockVector3.at(x, y, z);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BlockVector2 other)) {
return false;
}

return other.x == this.x && other.z == this.z;

}

@Override
public int hashCode() {
return (x << 16) ^ z;
}

@Override
public String toString() {
return "(" + x + ", " + z + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* An immutable 3-dimensional vector.
*/
public final class BlockVector3 {
public record BlockVector3(int x, int y, int z) {

public static final BlockVector3 ZERO = new BlockVector3(0, 0, 0);
public static final BlockVector3 UNIT_X = new BlockVector3(1, 0, 0);
Expand Down Expand Up @@ -112,23 +112,6 @@ public static Comparator<BlockVector3> sortByCoordsYzx() {
return YzxOrderComparator.YZX_ORDER;
}

private final int x;
private final int y;
private final int z;

/**
* Construct an instance.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
*/
private BlockVector3(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

public long toLongPackedForm() {
checkLongPackable(this);
return (x & BITS_26) | ((z & BITS_26) << 26) | (((y & BITS_12) << (26 + 26)));
Expand All @@ -138,7 +121,9 @@ public long toLongPackedForm() {
* Get the X coordinate.
*
* @return the x coordinate
* @deprecated use {@link #x()} instead
*/
@Deprecated(forRemoval = true)
public int getX() {
return x;
}
Expand All @@ -147,7 +132,9 @@ public int getX() {
* Get the X coordinate.
*
* @return the x coordinate
* @deprecated use {@link #x()} instead
*/
@Deprecated(forRemoval = true)
public int getBlockX() {
return x;
}
Expand All @@ -166,7 +153,9 @@ public BlockVector3 withX(int x) {
* Get the Y coordinate.
*
* @return the y coordinate
* @deprecated use {@link #y()} instead
*/
@Deprecated(forRemoval = true)
public int getY() {
return y;
}
Expand All @@ -175,7 +164,9 @@ public int getY() {
* Get the Y coordinate.
*
* @return the y coordinate
* @deprecated use {@link #y()} instead
*/
@Deprecated(forRemoval = true)
public int getBlockY() {
return y;
}
Expand All @@ -194,7 +185,9 @@ public BlockVector3 withY(int y) {
* Get the Z coordinate.
*
* @return the z coordinate
* @deprecated use {@link #z()} instead
*/
@Deprecated(forRemoval = true)
public int getZ() {
return z;
}
Expand All @@ -203,7 +196,9 @@ public int getZ() {
* Get the Z coordinate.
*
* @return the z coordinate
* @deprecated use {@link #z()} instead
*/
@Deprecated(forRemoval = true)
public int getBlockZ() {
return z;
}
Expand Down Expand Up @@ -686,20 +681,6 @@ public Vector3 toVector3() {
return Vector3.at(x, y, z);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BlockVector3 other)) {
return false;
}

return other.x == this.x && other.y == this.y && other.z == this.z;
}

@Override
public int hashCode() {
return (x ^ (z << 12)) ^ (y << 24);
}

@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
Expand Down
54 changes: 22 additions & 32 deletions worldedit-core/src/main/java/com/sk89q/worldedit/math/Vector2.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* An immutable 2-dimensional vector.
*/
public final class Vector2 {
public record Vector2(double x, double z) {

public static final Vector2 ZERO = new Vector2(0, 0);
public static final Vector2 UNIT_X = new Vector2(1, 0);
Expand All @@ -50,27 +50,24 @@ public static Vector2 at(double x, double z) {
return new Vector2(x, z);
}

private final double x;
private final double z;

/**
* Construct an instance.
* Get the X coordinate.
*
* @param x the X coordinate
* @param z the Z coordinate
* @return the x coordinate
* @deprecated use {@link #x()} instead
*/
private Vector2(double x, double z) {
this.x = x;
this.z = z;
@Deprecated(forRemoval = true)
public double getX() {
return x;
}

/**
* Get the X coordinate.
* Get the X coordinate, aligned to the block grid.
*
* @return the x coordinate
* @return the block-aligned x coordinate
*/
public double getX() {
return x;
public int blockX() {
return (int) Math.floor(x);
}

/**
Expand All @@ -83,11 +80,22 @@ public Vector2 withX(double x) {
return Vector2.at(x, z);
}

/**
* Get the Z coordinate, aligned to the block grid.
*
* @return the block-aligned z coordinate
*/
public int blockZ() {
return (int) Math.floor(z);
}

/**
* Get the Z coordinate.
*
* @return the z coordinate
* @deprecated use {@link #z()} instead
*/
@Deprecated(forRemoval = true)
public double getZ() {
return z;
}
Expand Down Expand Up @@ -457,24 +465,6 @@ public Vector3 toVector3(double y) {
return Vector3.at(x, y, z);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Vector2 other)) {
return false;
}

return other.x == this.x && other.z == this.z;

}

@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Double.hashCode(x);
hash = 31 * hash + Double.hashCode(z);
return hash;
}

@Override
public String toString() {
return "(" + x + ", " + z + ")";
Expand Down
Loading

0 comments on commit 0df2b6a

Please sign in to comment.