Skip to content

Commit 5db1280

Browse files
committed
Refactor equals methods
1 parent ae63991 commit 5db1280

11 files changed

Lines changed: 37 additions & 107 deletions

File tree

src/main/java/com/github/refhumbold/algolib/geometry/dim2/Point2D.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,8 @@ public static Point2D of(double x, double y)
3131
@Override
3232
public boolean equals(Object obj)
3333
{
34-
if(this == obj)
35-
return true;
36-
37-
if(!(obj instanceof Point2D other))
38-
return false;
39-
40-
return areEqual(x, other.x) && areEqual(y, other.y);
34+
return this == obj || obj instanceof Point2D other && areEqual(x, other.x) && areEqual(y,
35+
other.y);
4136
}
4237

4338
@Override

src/main/java/com/github/refhumbold/algolib/geometry/dim2/Vector2D.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,8 @@ public static double area(Vector2D v1, Vector2D v2)
4646
@Override
4747
public boolean equals(Object obj)
4848
{
49-
if(this == obj)
50-
return true;
51-
52-
if(!(obj instanceof Vector2D other))
53-
return false;
54-
55-
return areEqual(x, other.x) && areEqual(y, other.y);
49+
return this == obj || obj instanceof Vector2D other && areEqual(x, other.x) && areEqual(y,
50+
other.y);
5651
}
5752

5853
@Override

src/main/java/com/github/refhumbold/algolib/geometry/dim3/Point3D.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ public static Point3D of(double x, double y, double z)
3333
@Override
3434
public boolean equals(Object obj)
3535
{
36-
if(this == obj)
37-
return true;
38-
39-
if(!(obj instanceof Point3D other))
40-
return false;
41-
42-
return areEqual(x, other.x) && areEqual(y, other.y) && areEqual(z, other.z);
36+
return this == obj || obj instanceof Point3D other && areEqual(x, other.x) && areEqual(y,
37+
other.y) && areEqual(z, other.z);
4338
}
4439

4540
@Override

src/main/java/com/github/refhumbold/algolib/geometry/dim3/Vector3D.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,8 @@ public static double volume(Vector3D v1, Vector3D v2, Vector3D v3)
5959
@Override
6060
public boolean equals(Object obj)
6161
{
62-
if(this == obj)
63-
return true;
64-
65-
if(!(obj instanceof Vector3D other))
66-
return false;
67-
68-
return areEqual(x, other.x) && areEqual(y, other.y) && areEqual(z, other.z);
62+
return this == obj || obj instanceof Vector3D other && areEqual(x, other.x) && areEqual(y,
63+
other.y) && areEqual(z, other.z);
6964
}
7065

7166
@Override

src/main/java/com/github/refhumbold/algolib/maths/EquationSystem.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,6 @@ public static EquationSystem of(Equation... equations)
2121
return new EquationSystem(equations);
2222
}
2323

24-
@Override
25-
public boolean equals(Object obj)
26-
{
27-
if(this == obj)
28-
return true;
29-
30-
if(!(obj instanceof EquationSystem other))
31-
return false;
32-
33-
return Arrays.equals(equations, other.equations);
34-
}
35-
36-
@Override
37-
public int hashCode()
38-
{
39-
return Arrays.hashCode(equations);
40-
}
41-
4224
@Override
4325
public String toString()
4426
{

src/main/java/com/github/refhumbold/algolib/maths/Fraction.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,8 @@ public static Fraction fromLong(long value)
5555
@Override
5656
public boolean equals(Object obj)
5757
{
58-
if(this == obj)
59-
return true;
60-
61-
if(!(obj instanceof Fraction other))
62-
return false;
63-
64-
return Objects.equals(numerator, other.numerator) && Objects.equals(denominator,
65-
other.denominator);
58+
return this == obj || obj instanceof Fraction other && Objects.equals(numerator,
59+
other.numerator) && Objects.equals(denominator, other.denominator);
6660
}
6761

6862
@Override
@@ -254,16 +248,14 @@ public double doubleValue()
254248
@Override
255249
public int compareTo(Number number)
256250
{
257-
if(number instanceof Fraction)
258-
return compare((Fraction)number);
259-
260-
return Double.compare(doubleValue(), number.doubleValue());
251+
return number instanceof Fraction f
252+
? compare(f)
253+
: Double.compare(doubleValue(), number.doubleValue());
261254
}
262255

263256
private int compare(Fraction f)
264257
{
265258
long commonDenominator = Integers.lcm(denominator, f.denominator);
266-
267259
long thisNumerator = commonDenominator / denominator * numerator;
268260
long otherNumerator = commonDenominator / f.denominator * f.numerator;
269261

src/main/java/com/github/refhumbold/algolib/structures/AvlTree.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,8 @@ private void setRoot(AvlNode<E> node)
5555
@Override
5656
public boolean equals(Object obj)
5757
{
58-
if(this == obj)
59-
return true;
60-
61-
if(!(obj instanceof AvlTree<?> other))
62-
return false;
63-
64-
return size_ == other.size_ && containsAll(other);
58+
return this == obj
59+
|| obj instanceof AvlTree<?> other && size_ == other.size_ && containsAll(other);
6560
}
6661

6762
@Override

src/main/java/com/github/refhumbold/algolib/text/SuffixArray.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ public String getText()
3030
@Override
3131
public boolean equals(Object obj)
3232
{
33-
if(this == obj)
34-
return true;
35-
36-
if(!(obj instanceof SuffixArray other))
37-
return false;
38-
39-
return size_ == other.size_ && Objects.equals(text, other.text) && Objects.equals(
40-
suffixArray, other.suffixArray);
33+
return this == obj
34+
|| obj instanceof SuffixArray other && size_ == other.size_ && Objects.equals(text,
35+
other.text) && Objects.equals(suffixArray, other.suffixArray);
4136
}
4237

4338
@Override

src/main/java/com/github/refhumbold/algolib/tuples/Pair.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,8 @@ public static <F, S> Pair<F, S> of(F first, S second)
2222
@Override
2323
public boolean equals(Object obj)
2424
{
25-
if(this == obj)
26-
return true;
27-
28-
if(!(obj instanceof Pair))
29-
return false;
30-
31-
Pair<?, ?> other = (Pair<?, ?>)obj;
32-
33-
return Objects.equals(first, other.first) && Objects.equals(second, other.second);
25+
return this == obj || obj instanceof Pair<?, ?> other && Objects.equals(first, other.first)
26+
&& Objects.equals(second, other.second);
3427
}
3528

3629
@Override

src/main/java/com/github/refhumbold/algolib/tuples/Triple.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,9 @@ public static <F, S, T> Triple<F, S, T> of(F first, S second, T third)
2424
@Override
2525
public boolean equals(Object obj)
2626
{
27-
if(this == obj)
28-
return true;
29-
30-
if(!(obj instanceof Triple))
31-
return false;
32-
33-
Triple<?, ?, ?> other = (Triple<?, ?, ?>)obj;
34-
35-
return Objects.equals(first, other.first) && Objects.equals(second, other.second)
36-
&& Objects.equals(third, other.third);
27+
return this == obj || obj instanceof Triple<?, ?, ?> other && Objects.equals(first,
28+
other.first) && Objects.equals(second, other.second) && Objects.equals(third,
29+
other.third);
3730
}
3831

3932
@Override

0 commit comments

Comments
 (0)