updated code/tests based on feedback (#676)
This commit is contained in:
parent
113afd40d4
commit
15d45cffd6
@ -5,63 +5,59 @@ import java.util.HashSet;
|
|||||||
|
|
||||||
public class ComplexClass {
|
public class ComplexClass {
|
||||||
|
|
||||||
private ArrayList<?> genericArrayList;
|
private ArrayList<?> genericArrayList;
|
||||||
private HashSet<Integer> integerHashSet;
|
private HashSet<Integer> integerHashSet;
|
||||||
|
|
||||||
public ComplexClass(ArrayList<?> genericArrayList,
|
public ComplexClass(ArrayList<?> genericArrayList, HashSet<Integer> integerHashSet) {
|
||||||
HashSet<Integer> integerHashSet) {
|
super();
|
||||||
super();
|
this.genericArrayList = genericArrayList;
|
||||||
this.genericArrayList = genericArrayList;
|
this.integerHashSet = integerHashSet;
|
||||||
this.integerHashSet = integerHashSet;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime
|
result = prime * result + ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
|
||||||
* result
|
result = prime * result + ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
|
||||||
+ ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
|
return result;
|
||||||
result = prime * result
|
}
|
||||||
+ ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj)
|
if (this == obj)
|
||||||
return true;
|
return true;
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
return false;
|
return false;
|
||||||
if (!(obj instanceof ComplexClass))
|
if (!(obj instanceof ComplexClass))
|
||||||
return false;
|
return false;
|
||||||
ComplexClass other = (ComplexClass) obj;
|
ComplexClass other = (ComplexClass) obj;
|
||||||
if (genericArrayList == null) {
|
if (genericArrayList == null) {
|
||||||
if (other.genericArrayList != null)
|
if (other.genericArrayList != null)
|
||||||
return false;
|
return false;
|
||||||
} else if (!genericArrayList.equals(other.genericArrayList))
|
} else if (!genericArrayList.equals(other.genericArrayList))
|
||||||
return false;
|
return false;
|
||||||
if (integerHashSet == null) {
|
if (integerHashSet == null) {
|
||||||
if (other.integerHashSet != null)
|
if (other.integerHashSet != null)
|
||||||
return false;
|
return false;
|
||||||
} else if (!integerHashSet.equals(other.integerHashSet))
|
} else if (!integerHashSet.equals(other.integerHashSet))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ArrayList<?> getGenericArrayList() {
|
protected ArrayList<?> getGenericArrayList() {
|
||||||
return genericArrayList;
|
return genericArrayList;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setGenericArrayList(ArrayList<?> genericArrayList) {
|
protected void setGenericArrayList(ArrayList<?> genericArrayList) {
|
||||||
this.genericArrayList = genericArrayList;
|
this.genericArrayList = genericArrayList;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected HashSet<Integer> getIntegerHashSet() {
|
protected HashSet<Integer> getIntegerHashSet() {
|
||||||
return integerHashSet;
|
return integerHashSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setIntegerHashSet(HashSet<Integer> integerHashSet) {
|
protected void setIntegerHashSet(HashSet<Integer> integerHashSet) {
|
||||||
this.integerHashSet = integerHashSet;
|
this.integerHashSet = integerHashSet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,53 +2,53 @@ package org.baeldung.equalshashcode.entities;
|
|||||||
|
|
||||||
public class PrimitiveClass {
|
public class PrimitiveClass {
|
||||||
|
|
||||||
private boolean primitiveBoolean;
|
private boolean primitiveBoolean;
|
||||||
private int primitiveInt;
|
private int primitiveInt;
|
||||||
|
|
||||||
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
|
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
|
||||||
super();
|
super();
|
||||||
this.primitiveBoolean = primitiveBoolean;
|
this.primitiveBoolean = primitiveBoolean;
|
||||||
this.primitiveInt = primitiveInt;
|
this.primitiveInt = primitiveInt;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isPrimitiveBoolean() {
|
protected boolean isPrimitiveBoolean() {
|
||||||
return primitiveBoolean;
|
return primitiveBoolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + (primitiveBoolean ? 1231 : 1237);
|
result = prime * result + (primitiveBoolean ? 1231 : 1237);
|
||||||
result = prime * result + primitiveInt;
|
result = prime * result + primitiveInt;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj)
|
if (this == obj)
|
||||||
return true;
|
return true;
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
return false;
|
return false;
|
||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
PrimitiveClass other = (PrimitiveClass) obj;
|
PrimitiveClass other = (PrimitiveClass) obj;
|
||||||
if (primitiveBoolean != other.primitiveBoolean)
|
if (primitiveBoolean != other.primitiveBoolean)
|
||||||
return false;
|
return false;
|
||||||
if (primitiveInt != other.primitiveInt)
|
if (primitiveInt != other.primitiveInt)
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
|
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
|
||||||
this.primitiveBoolean = primitiveBoolean;
|
this.primitiveBoolean = primitiveBoolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int getPrimitiveInt() {
|
protected int getPrimitiveInt() {
|
||||||
return primitiveInt;
|
return primitiveInt;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setPrimitiveInt(int primitiveInt) {
|
protected void setPrimitiveInt(int primitiveInt) {
|
||||||
this.primitiveInt = primitiveInt;
|
this.primitiveInt = primitiveInt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,60 @@
|
|||||||
package org.baeldung.equalshashcode.entities;
|
package org.baeldung.equalshashcode.entities;
|
||||||
|
|
||||||
public class Rectangle extends Shape {
|
public class Rectangle extends Shape {
|
||||||
private double width;
|
private double width;
|
||||||
private double length;
|
private double length;
|
||||||
|
|
||||||
public Rectangle(double width, double length) {
|
public Rectangle(double width, double length) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.length = length;
|
this.length = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double area() {
|
public double area() {
|
||||||
// A = w * l
|
// A = w * l
|
||||||
return width * length;
|
return width * length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double perimeter() {
|
public double perimeter() {
|
||||||
// P = 2(w + l)
|
// P = 2(w + l)
|
||||||
return 2 * (width + length);
|
return 2 * (width + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
long temp;
|
long temp;
|
||||||
temp = Double.doubleToLongBits(length);
|
temp = Double.doubleToLongBits(length);
|
||||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||||
temp = Double.doubleToLongBits(width);
|
temp = Double.doubleToLongBits(width);
|
||||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj)
|
if (this == obj)
|
||||||
return true;
|
return true;
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
return false;
|
return false;
|
||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
Rectangle other = (Rectangle) obj;
|
Rectangle other = (Rectangle) obj;
|
||||||
if (Double.doubleToLongBits(length) != Double
|
if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length))
|
||||||
.doubleToLongBits(other.length))
|
return false;
|
||||||
return false;
|
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
|
||||||
if (Double.doubleToLongBits(width) != Double
|
return false;
|
||||||
.doubleToLongBits(other.width))
|
return true;
|
||||||
return false;
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected double getWidth() {
|
protected double getWidth() {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected double getLength() {
|
protected double getLength() {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package org.baeldung.equalshashcode.entities;
|
package org.baeldung.equalshashcode.entities;
|
||||||
|
|
||||||
public abstract class Shape {
|
public abstract class Shape {
|
||||||
public abstract double area();
|
public abstract double area();
|
||||||
|
|
||||||
public abstract double perimeter();
|
public abstract double perimeter();
|
||||||
}
|
}
|
||||||
|
@ -4,44 +4,55 @@ import java.awt.Color;
|
|||||||
|
|
||||||
public class Square extends Rectangle {
|
public class Square extends Rectangle {
|
||||||
|
|
||||||
Color color;
|
Color color;
|
||||||
|
|
||||||
public Square(double width, Color color) {
|
public Square(double width, Color color) {
|
||||||
super(width, width);
|
super(width, width);
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/* (non-Javadoc)
|
||||||
public int hashCode() {
|
* @see java.lang.Object#hashCode()
|
||||||
final int prime = 31;
|
*/
|
||||||
int result = super.hashCode();
|
@Override
|
||||||
result = prime * result + ((color == null) ? 0 : color.hashCode());
|
public int hashCode() {
|
||||||
return result;
|
final int prime = 31;
|
||||||
}
|
int result = super.hashCode();
|
||||||
|
result = prime * result + ((color == null) ? 0 : color.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
/* (non-Javadoc)
|
||||||
public boolean equals(Object obj) {
|
* @see java.lang.Object#equals(java.lang.Object)
|
||||||
if (this == obj)
|
*/
|
||||||
return true;
|
@Override
|
||||||
if (!super.equals(obj))
|
public boolean equals(Object obj) {
|
||||||
return false;
|
if (this == obj) {
|
||||||
if (getClass() != obj.getClass())
|
return true;
|
||||||
return false;
|
}
|
||||||
Square other = (Square) obj;
|
if (!super.equals(obj)) {
|
||||||
if (color == null) {
|
return false;
|
||||||
if (other.color != null)
|
}
|
||||||
return false;
|
if (!(obj instanceof Square)) {
|
||||||
} else if (!color.equals(other.color))
|
return false;
|
||||||
return false;
|
}
|
||||||
return true;
|
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() {
|
protected Color getColor() {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setColor(Color color) {
|
protected void setColor(Color color) {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,32 +8,27 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class ComplexClassTest {
|
public class ComplexClassTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEqualsAndHashcodes() {
|
public void testEqualsAndHashcodes() {
|
||||||
|
|
||||||
ArrayList<String> strArrayList = new ArrayList<String>();
|
ArrayList<String> strArrayList = new ArrayList<String>();
|
||||||
strArrayList.add("abc");
|
strArrayList.add("abc");
|
||||||
strArrayList.add("def");
|
strArrayList.add("def");
|
||||||
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
|
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45, 67));
|
||||||
ComplexClass bObject = 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>();
|
ArrayList<String> strArrayListD = new ArrayList<String>();
|
||||||
strArrayListD.add("lmn");
|
strArrayListD.add("lmn");
|
||||||
strArrayListD.add("pqr");
|
strArrayListD.add("pqr");
|
||||||
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45,67));
|
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
|
||||||
|
|
||||||
// equals()
|
// equals()
|
||||||
Assert.assertTrue(aObject.equals(aObject));
|
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
// hashCode()
|
||||||
Assert.assertTrue(aObject.equals(bObject));
|
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||||
Assert.assertTrue(bObject.equals(cObject));
|
// non-equal objects are not equals() and have different hashCode()
|
||||||
Assert.assertTrue(aObject.equals(cObject));
|
Assert.assertFalse(aObject.equals(dObject));
|
||||||
// hashCode()
|
Assert.assertFalse(aObject.hashCode() == dObject.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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,25 +5,20 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class PrimitiveClassTest {
|
public class PrimitiveClassTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTwoEqualsObjects() {
|
public void testTwoEqualsObjects() {
|
||||||
|
|
||||||
PrimitiveClass aObject = new PrimitiveClass(false, 2);
|
PrimitiveClass aObject = new PrimitiveClass(false, 2);
|
||||||
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
||||||
PrimitiveClass cObject = new PrimitiveClass(false, 2);
|
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
||||||
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
|
||||||
|
|
||||||
// equals()
|
// equals()
|
||||||
Assert.assertTrue(aObject.equals(aObject));
|
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
// hashCode()
|
||||||
Assert.assertTrue(aObject.equals(bObject));
|
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||||
Assert.assertTrue(bObject.equals(cObject));
|
// non-equal objects are not equals() and have different hashCode()
|
||||||
Assert.assertTrue(aObject.equals(cObject));
|
Assert.assertFalse(aObject.equals(dObject));
|
||||||
// hashCode()
|
Assert.assertFalse(aObject.hashCode() == dObject.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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,26 +7,21 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class SquareClassTest {
|
public class SquareClassTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEqualsAndHashcodes() {
|
public void testEqualsAndHashcodes() {
|
||||||
|
|
||||||
Square aObject = new Square(10, Color.BLUE);
|
Square aObject = new Square(10, Color.BLUE);
|
||||||
Square bObject = 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);
|
Square dObject = new Square(20, Color.BLUE);
|
||||||
|
|
||||||
// equals()
|
// equals()
|
||||||
Assert.assertTrue(aObject.equals(aObject));
|
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
// hashCode()
|
||||||
Assert.assertTrue(aObject.equals(bObject));
|
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||||
Assert.assertTrue(bObject.equals(cObject));
|
// non-equal objects are not equals() and have different hashCode()
|
||||||
Assert.assertTrue(aObject.equals(cObject));
|
Assert.assertFalse(aObject.equals(dObject));
|
||||||
// hashCode()
|
Assert.assertFalse(aObject.hashCode() == dObject.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…
x
Reference in New Issue
Block a user