Refactor code to use pattern matching for instanceof and simplify hashCode and equals methods
This commit is contained in:
@@ -38,8 +38,8 @@ public class EnhancedWindow<T> extends Window {
|
||||
|
||||
synchronized (WINDOWS) {
|
||||
for (final Window window : WINDOWS) {
|
||||
if (window instanceof EnhancedWindow) {
|
||||
window.reset();
|
||||
if (window instanceof final EnhancedWindow<?> enhancedWindow) {
|
||||
enhancedWindow.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,13 +206,7 @@ public class G2DDrawImage extends G2DAbstractRenderingBo implements DrawImageRen
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final var builder = new StringBuilder();
|
||||
builder.append("CachedImage [name=");
|
||||
builder.append(this.name);
|
||||
builder.append(", used=");
|
||||
builder.append(this.used);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
return "CachedImage [name=" + this.name + ", used=" + this.used + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.extio.game_engine.renderer.model;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.Objects;
|
||||
|
||||
public class RgbaColor {
|
||||
|
||||
@@ -166,13 +167,7 @@ public class RgbaColor {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
result = prime * result + this.a;
|
||||
result = prime * result + this.b;
|
||||
result = prime * result + this.g;
|
||||
result = prime * result + this.r;
|
||||
return result;
|
||||
return Objects.hash(this.r, this.g, this.b, this.a);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -180,38 +175,15 @@ public class RgbaColor {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if ((obj == null) || (this.getClass() != obj.getClass())) {
|
||||
if (!(obj instanceof final RgbaColor other)) {
|
||||
return false;
|
||||
}
|
||||
final var other = (RgbaColor) obj;
|
||||
if (this.a != other.a) {
|
||||
return false;
|
||||
}
|
||||
if (this.b != other.b) {
|
||||
return false;
|
||||
}
|
||||
if (this.g != other.g) {
|
||||
return false;
|
||||
}
|
||||
if (this.r != other.r) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return this.r == other.r && this.g == other.g && this.b == other.b && this.a == other.a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final var builder = new StringBuilder();
|
||||
builder.append("RgbaColor [r=");
|
||||
builder.append(this.r);
|
||||
builder.append(", g=");
|
||||
builder.append(this.g);
|
||||
builder.append(", b=");
|
||||
builder.append(this.b);
|
||||
builder.append(", a=");
|
||||
builder.append(this.a);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
return "RgbaColor [r=" + this.r + ", g=" + this.g + ", b=" + this.b + ", a=" + this.a + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.extio.game_engine.spatial2.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A 2D area, having a position (top left corner) and a dimension
|
||||
*/
|
||||
@@ -44,10 +46,7 @@ public class Area2 extends Point2 implements HasPositionAndDimension2, SpatialIn
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = super.hashCode();
|
||||
result = prime * result + ((this.dimension == null) ? 0 : this.dimension.hashCode());
|
||||
return result;
|
||||
return Objects.hash(super.hashCode(), this.dimension);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,30 +54,15 @@ public class Area2 extends Point2 implements HasPositionAndDimension2, SpatialIn
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!super.equals(obj)) {
|
||||
if (!super.equals(obj) || !(obj instanceof final Area2 other)) {
|
||||
return false;
|
||||
}
|
||||
final var other = (Area2) obj;
|
||||
if (this.dimension == null) {
|
||||
if (other.dimension != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!this.dimension.equals(other.dimension)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return Objects.equals(this.dimension, other.dimension);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final var builder = new StringBuilder();
|
||||
builder.append("Area2 [getPosition()=");
|
||||
builder.append(this.getPosition());
|
||||
builder.append(", dimension=");
|
||||
builder.append(this.dimension);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
return "Area2 [getPosition()=" + this.getPosition() + ", dimension=" + this.dimension + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import de.extio.game_engine.spatial2.WorldUtils2;
|
||||
public interface Coord2Conversion {
|
||||
|
||||
default CoordI2 toMutableCoordI2() {
|
||||
if (this instanceof CoordI2) {
|
||||
return MutableCoordI2.create((CoordI2) this);
|
||||
if (this instanceof final CoordI2 coordI2) {
|
||||
return MutableCoordI2.create(coordI2);
|
||||
}
|
||||
else {
|
||||
return MutableCoordI2.create((CoordD2) this);
|
||||
@@ -17,8 +17,8 @@ public interface Coord2Conversion {
|
||||
}
|
||||
|
||||
default CoordI2 toImmutableCoordI2() {
|
||||
if (this instanceof CoordI2) {
|
||||
return ImmutableCoordI2.create((CoordI2) this);
|
||||
if (this instanceof final CoordI2 coordI2) {
|
||||
return ImmutableCoordI2.create(coordI2);
|
||||
}
|
||||
else {
|
||||
return ImmutableCoordI2.create((CoordD2) this);
|
||||
@@ -26,8 +26,8 @@ public interface Coord2Conversion {
|
||||
}
|
||||
|
||||
default CoordD2 toMutableCoordD2() {
|
||||
if (this instanceof CoordI2) {
|
||||
return MutableCoordD2.create((CoordI2) this);
|
||||
if (this instanceof final CoordI2 coordI2) {
|
||||
return MutableCoordD2.create(coordI2);
|
||||
}
|
||||
else {
|
||||
return MutableCoordD2.create((CoordD2) this);
|
||||
@@ -35,8 +35,8 @@ public interface Coord2Conversion {
|
||||
}
|
||||
|
||||
default CoordD2 toImmutableCoordD2() {
|
||||
if (this instanceof CoordI2) {
|
||||
return ImmutableCoordD2.create((CoordI2) this);
|
||||
if (this instanceof final CoordI2 coordI2) {
|
||||
return ImmutableCoordD2.create(coordI2);
|
||||
}
|
||||
else {
|
||||
return ImmutableCoordD2.create((CoordD2) this);
|
||||
@@ -49,11 +49,11 @@ public interface Coord2Conversion {
|
||||
default CoordD2 toVNorm() {
|
||||
CoordD2 vNorm;
|
||||
|
||||
if (this instanceof ImmutableCoordI2) {
|
||||
vNorm = ImmutableCoordD2.create((ImmutableCoordI2) this);
|
||||
if (this instanceof final ImmutableCoordI2 immutableCoordI2) {
|
||||
vNorm = ImmutableCoordD2.create(immutableCoordI2);
|
||||
}
|
||||
else if (this instanceof MutableCoordI2) {
|
||||
vNorm = MutableCoordD2.create((MutableCoordI2) this);
|
||||
else if (this instanceof final MutableCoordI2 mutableCoordI2) {
|
||||
vNorm = MutableCoordD2.create(mutableCoordI2);
|
||||
}
|
||||
else {
|
||||
vNorm = (CoordD2) this;
|
||||
|
||||
@@ -86,18 +86,12 @@ public enum Edge2 {
|
||||
* @return Vector
|
||||
*/
|
||||
public static CoordD2 toVectorD(final Edge2 edge2) {
|
||||
switch (edge2) {
|
||||
case BOTTOM:
|
||||
return VD_BOTTOM;
|
||||
case LEFT:
|
||||
return VD_LEFT;
|
||||
case RIGHT:
|
||||
return VD_RIGHT;
|
||||
case TOP:
|
||||
return VD_TOP;
|
||||
}
|
||||
|
||||
return null;
|
||||
return switch (edge2) {
|
||||
case BOTTOM -> VD_BOTTOM;
|
||||
case LEFT -> VD_LEFT;
|
||||
case RIGHT -> VD_RIGHT;
|
||||
case TOP -> VD_TOP;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,18 +100,12 @@ public enum Edge2 {
|
||||
* @return Vector
|
||||
*/
|
||||
public static CoordI2 toVectorI(final Edge2 edge2) {
|
||||
switch (edge2) {
|
||||
case BOTTOM:
|
||||
return VI_BOTTOM;
|
||||
case LEFT:
|
||||
return VI_LEFT;
|
||||
case RIGHT:
|
||||
return VI_RIGHT;
|
||||
case TOP:
|
||||
return VI_TOP;
|
||||
}
|
||||
|
||||
return null;
|
||||
return switch (edge2) {
|
||||
case BOTTOM -> VI_BOTTOM;
|
||||
case LEFT -> VI_LEFT;
|
||||
case RIGHT -> VI_RIGHT;
|
||||
case TOP -> VI_TOP;
|
||||
};
|
||||
}
|
||||
|
||||
public static String toString(final int edges) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.extio.game_engine.spatial2.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Immutable implementation of CoordD2
|
||||
*/
|
||||
@@ -25,8 +27,8 @@ public final class ImmutableCoordD2 implements CoordD2 {
|
||||
if (other == null) {
|
||||
return null;
|
||||
}
|
||||
if (other instanceof ImmutableCoordD2) {
|
||||
return (ImmutableCoordD2) other;
|
||||
if (other instanceof final ImmutableCoordD2 immutableCoordD2) {
|
||||
return immutableCoordD2;
|
||||
}
|
||||
return new ImmutableCoordD2(other.getX(), other.getY());
|
||||
}
|
||||
@@ -149,14 +151,7 @@ public final class ImmutableCoordD2 implements CoordD2 {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(this.x);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(this.y);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
return Objects.hash(this.x, this.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.extio.game_engine.spatial2.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Immutable implementation of CoordI2
|
||||
*/
|
||||
@@ -180,11 +182,7 @@ public final class ImmutableCoordI2 implements CoordI2 {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
result = prime * result + this.x;
|
||||
result = prime * result + this.y;
|
||||
return result;
|
||||
return Objects.hash(this.x, this.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.extio.game_engine.spatial2.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Immutable implementation of CoordD2
|
||||
*/
|
||||
@@ -178,14 +180,7 @@ public final class MutableCoordD2 implements CoordD2 {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(this.x);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(this.y);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
return Objects.hash(this.x, this.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.extio.game_engine.spatial2.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Immutable implementation of CoordI2
|
||||
*/
|
||||
@@ -178,11 +180,7 @@ public final class MutableCoordI2 implements CoordI2 {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
result = prime * result + this.x;
|
||||
result = prime * result + this.y;
|
||||
return result;
|
||||
return Objects.hash(this.x, this.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.extio.game_engine.spatial2.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A point in 2D space
|
||||
*/
|
||||
@@ -26,10 +28,7 @@ public class Point2 implements HasPosition2 {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
result = prime * result + ((this.coord == null) ? 0 : this.coord.hashCode());
|
||||
return result;
|
||||
return Objects.hash(this.coord);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,28 +36,15 @@ public class Point2 implements HasPosition2 {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if ((obj == null) || (this.getClass() != obj.getClass())) {
|
||||
if (!(obj instanceof final Point2 other)) {
|
||||
return false;
|
||||
}
|
||||
final var other = (Point2) obj;
|
||||
if (this.coord == null) {
|
||||
if (other.coord != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!this.coord.equals(other.coord)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return Objects.equals(this.coord, other.coord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final var builder = new StringBuilder();
|
||||
builder.append("Point2 [coord=");
|
||||
builder.append(this.coord);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
return "Point2 [coord=" + this.coord + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.extio.game_engine.spatial2.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A rectangle represented by top left (c0) and bottom right (c1) vertices
|
||||
*/
|
||||
@@ -23,16 +25,16 @@ public class RectI2 {
|
||||
return;
|
||||
}
|
||||
if (other.c0 != null) {
|
||||
if (other.c0 instanceof ImmutableCoordI2) {
|
||||
this.c0 = ImmutableCoordI2.create(other.c0);
|
||||
if (other.c0 instanceof final ImmutableCoordI2 immutableC0) {
|
||||
this.c0 = ImmutableCoordI2.create(immutableC0);
|
||||
}
|
||||
else {
|
||||
this.c0 = MutableCoordI2.create(other.c0);
|
||||
}
|
||||
}
|
||||
if (other.c1 != null) {
|
||||
if (other.c1 instanceof ImmutableCoordI2) {
|
||||
this.c1 = ImmutableCoordI2.create(other.c1);
|
||||
if (other.c1 instanceof final ImmutableCoordI2 immutableC1) {
|
||||
this.c1 = ImmutableCoordI2.create(immutableC1);
|
||||
}
|
||||
else {
|
||||
this.c1 = MutableCoordI2.create(other.c1);
|
||||
@@ -58,11 +60,7 @@ public class RectI2 {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
result = prime * result + ((this.c0 == null) ? 0 : this.c0.hashCode());
|
||||
result = prime * result + ((this.c1 == null) ? 0 : this.c1.hashCode());
|
||||
return result;
|
||||
return Objects.hash(this.c0, this.c1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,27 +68,10 @@ public class RectI2 {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if ((obj == null) || (this.getClass() != obj.getClass())) {
|
||||
if (!(obj instanceof final RectI2 other)) {
|
||||
return false;
|
||||
}
|
||||
final var other = (RectI2) obj;
|
||||
if (this.c0 == null) {
|
||||
if (other.c0 != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!this.c0.equals(other.c0)) {
|
||||
return false;
|
||||
}
|
||||
if (this.c1 == null) {
|
||||
if (other.c1 != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!this.c1.equals(other.c1)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return Objects.equals(this.c0, other.c0) && Objects.equals(this.c1, other.c1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user