diff --git a/eclipse/src/main/java/org/baeldung/equalshashcode/entities/ComplexClass.java b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/ComplexClass.java new file mode 100644 index 0000000000..f8c85ab8b5 --- /dev/null +++ b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/ComplexClass.java @@ -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 integerHashSet; + + public ComplexClass(ArrayList genericArrayList, + HashSet 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 getIntegerHashSet() { + return integerHashSet; + } + + protected void setIntegerHashSet(HashSet integerHashSet) { + this.integerHashSet = integerHashSet; + } +} diff --git a/eclipse/src/main/java/org/baeldung/equalshashcode/entities/PrimitiveClass.java b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/PrimitiveClass.java new file mode 100644 index 0000000000..6cd4af5fdb --- /dev/null +++ b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/PrimitiveClass.java @@ -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; + } +} diff --git a/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Rectangle.java b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Rectangle.java new file mode 100644 index 0000000000..61d20cbb05 --- /dev/null +++ b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Rectangle.java @@ -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; + } + +} \ No newline at end of file diff --git a/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Shape.java b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Shape.java new file mode 100644 index 0000000000..7f779e6ef2 --- /dev/null +++ b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Shape.java @@ -0,0 +1,7 @@ +package org.baeldung.equalshashcode.entities; + +public abstract class Shape { + public abstract double area(); + + public abstract double perimeter(); +} diff --git a/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Square.java b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Square.java new file mode 100644 index 0000000000..0bebc1e380 --- /dev/null +++ b/eclipse/src/main/java/org/baeldung/equalshashcode/entities/Square.java @@ -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; + } + +} diff --git a/eclipse/src/test/java/org/baeldung/equalshashcode/entities/ComplexClassTest.java b/eclipse/src/test/java/org/baeldung/equalshashcode/entities/ComplexClassTest.java new file mode 100644 index 0000000000..2cca44bb9e --- /dev/null +++ b/eclipse/src/test/java/org/baeldung/equalshashcode/entities/ComplexClassTest.java @@ -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 strArrayList = new ArrayList(); + strArrayList.add("abc"); + strArrayList.add("def"); + ComplexClass aObject = new ComplexClass(strArrayList, new HashSet(45,67)); + ComplexClass bObject = new ComplexClass(strArrayList, new HashSet(45,67)); + ComplexClass cObject = new ComplexClass(strArrayList, new HashSet(45,67)); + + ArrayList strArrayListD = new ArrayList(); + strArrayListD.add("lmn"); + strArrayListD.add("pqr"); + ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet(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()); + } + +} diff --git a/eclipse/src/test/java/org/baeldung/equalshashcode/entities/PrimitiveClassTest.java b/eclipse/src/test/java/org/baeldung/equalshashcode/entities/PrimitiveClassTest.java new file mode 100644 index 0000000000..009753d1ae --- /dev/null +++ b/eclipse/src/test/java/org/baeldung/equalshashcode/entities/PrimitiveClassTest.java @@ -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()); + } + +} diff --git a/eclipse/src/test/java/org/baeldung/equalshashcode/entities/SquareClassTest.java b/eclipse/src/test/java/org/baeldung/equalshashcode/entities/SquareClassTest.java new file mode 100644 index 0000000000..1290f57c6d --- /dev/null +++ b/eclipse/src/test/java/org/baeldung/equalshashcode/entities/SquareClassTest.java @@ -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()); + } + +}