Added equals()/hashcode() (#646)
This commit is contained in:
parent
e67584d9a7
commit
ae907114c7
|
@ -0,0 +1,67 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class ComplexClass {
|
||||
|
||||
private ArrayList<?> genericArrayList;
|
||||
private HashSet<Integer> integerHashSet;
|
||||
|
||||
public ComplexClass(ArrayList<?> genericArrayList,
|
||||
HashSet<Integer> integerHashSet) {
|
||||
super();
|
||||
this.genericArrayList = genericArrayList;
|
||||
this.integerHashSet = integerHashSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime
|
||||
* result
|
||||
+ ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
|
||||
result = prime * result
|
||||
+ ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof ComplexClass))
|
||||
return false;
|
||||
ComplexClass other = (ComplexClass) obj;
|
||||
if (genericArrayList == null) {
|
||||
if (other.genericArrayList != null)
|
||||
return false;
|
||||
} else if (!genericArrayList.equals(other.genericArrayList))
|
||||
return false;
|
||||
if (integerHashSet == null) {
|
||||
if (other.integerHashSet != null)
|
||||
return false;
|
||||
} else if (!integerHashSet.equals(other.integerHashSet))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected ArrayList<?> getGenericArrayList() {
|
||||
return genericArrayList;
|
||||
}
|
||||
|
||||
protected void setGenericArrayList(ArrayList<?> genericArrayList) {
|
||||
this.genericArrayList = genericArrayList;
|
||||
}
|
||||
|
||||
protected HashSet<Integer> getIntegerHashSet() {
|
||||
return integerHashSet;
|
||||
}
|
||||
|
||||
protected void setIntegerHashSet(HashSet<Integer> integerHashSet) {
|
||||
this.integerHashSet = integerHashSet;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
public class PrimitiveClass {
|
||||
|
||||
private boolean primitiveBoolean;
|
||||
private int primitiveInt;
|
||||
|
||||
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
|
||||
super();
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
|
||||
protected boolean isPrimitiveBoolean() {
|
||||
return primitiveBoolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (primitiveBoolean ? 1231 : 1237);
|
||||
result = prime * result + primitiveInt;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
PrimitiveClass other = (PrimitiveClass) obj;
|
||||
if (primitiveBoolean != other.primitiveBoolean)
|
||||
return false;
|
||||
if (primitiveInt != other.primitiveInt)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
}
|
||||
|
||||
protected int getPrimitiveInt() {
|
||||
return primitiveInt;
|
||||
}
|
||||
|
||||
protected void setPrimitiveInt(int primitiveInt) {
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
private double width;
|
||||
private double length;
|
||||
|
||||
public Rectangle(double width, double length) {
|
||||
this.width = width;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double area() {
|
||||
// A = w * l
|
||||
return width * length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double perimeter() {
|
||||
// P = 2(w + l)
|
||||
return 2 * (width + length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(length);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(width);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Rectangle other = (Rectangle) obj;
|
||||
if (Double.doubleToLongBits(length) != Double
|
||||
.doubleToLongBits(other.length))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(width) != Double
|
||||
.doubleToLongBits(other.width))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected double getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
protected double getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
public abstract class Shape {
|
||||
public abstract double area();
|
||||
|
||||
public abstract double perimeter();
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public class Square extends Rectangle {
|
||||
|
||||
Color color;
|
||||
|
||||
public Square(double width, Color color) {
|
||||
super(width, width);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
result = prime * result + ((color == null) ? 0 : color.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!super.equals(obj))
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Square other = (Square) obj;
|
||||
if (color == null) {
|
||||
if (other.color != null)
|
||||
return false;
|
||||
} else if (!color.equals(other.color))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
protected void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ComplexClassTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
|
||||
ArrayList<String> strArrayList = new ArrayList<String>();
|
||||
strArrayList.add("abc");
|
||||
strArrayList.add("def");
|
||||
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
|
||||
ComplexClass bObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
|
||||
ComplexClass cObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
|
||||
|
||||
ArrayList<String> strArrayListD = new ArrayList<String>();
|
||||
strArrayListD.add("lmn");
|
||||
strArrayListD.add("pqr");
|
||||
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45,67));
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject));
|
||||
Assert.assertTrue(bObject.equals(cObject));
|
||||
Assert.assertTrue(aObject.equals(cObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PrimitiveClassTest {
|
||||
|
||||
@Test
|
||||
public void testTwoEqualsObjects() {
|
||||
|
||||
PrimitiveClass aObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass cObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject));
|
||||
Assert.assertTrue(bObject.equals(cObject));
|
||||
Assert.assertTrue(aObject.equals(cObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package org.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SquareClassTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
|
||||
Square aObject = new Square(10, Color.BLUE);
|
||||
Square bObject = new Square(10, Color.BLUE);
|
||||
Square cObject = new Square(10, Color.BLUE);
|
||||
|
||||
Square dObject = new Square(20, Color.BLUE);
|
||||
|
||||
// equals()
|
||||
Assert.assertTrue(aObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
Assert.assertTrue(aObject.equals(bObject));
|
||||
Assert.assertTrue(bObject.equals(cObject));
|
||||
Assert.assertTrue(aObject.equals(cObject));
|
||||
// hashCode()
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
// non-equal objects are not equals() and have different hashCode()
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue