Moved project to core-java from eclipse folder (#703)

This commit is contained in:
Nancy Bosecker 2016-09-26 22:40:46 -07:00 committed by Grzegorz Piwowarek
parent 302ac3a5d4
commit 7aaae1d143
9 changed files with 71 additions and 74 deletions

View File

@ -0,0 +1,65 @@
package org.baeldung.equalshashcode.entities;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ComplexClass {
private List<?> genericList;
private Set<Integer> integerSet;
public ComplexClass(ArrayList<?> genericArrayList, HashSet<Integer> integerHashSet) {
super();
this.genericList = genericArrayList;
this.integerSet = integerHashSet;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((genericList == null) ? 0 : genericList.hashCode());
result = prime * result + ((integerSet == null) ? 0 : integerSet.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 (genericList == null) {
if (other.genericList != null)
return false;
} else if (!genericList.equals(other.genericList))
return false;
if (integerSet == null) {
if (other.integerSet != null)
return false;
} else if (!integerSet.equals(other.integerSet))
return false;
return true;
}
protected List<?> getGenericList() {
return genericList;
}
protected void setGenericArrayList(List<?> genericList) {
this.genericList = genericList;
}
protected Set<Integer> getIntegerSet() {
return integerSet;
}
protected void setIntegerSet(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
}

View File

@ -11,13 +11,11 @@ public class Rectangle extends Shape {
@Override
public double area() {
// A = w * l
return width * length;
}
@Override
public double perimeter() {
// P = 2(w + l)
return 2 * (width + length);
}

View File

@ -22,11 +22,10 @@ public class ComplexClassTest {
strArrayListD.add("pqr");
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
// equals()
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
// 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());
}

View File

@ -12,11 +12,10 @@ public class PrimitiveClassTest {
PrimitiveClass bObject = new PrimitiveClass(false, 2);
PrimitiveClass dObject = new PrimitiveClass(true, 2);
// equals()
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
// 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());
}

View File

@ -15,11 +15,10 @@ public class SquareClassTest {
Square dObject = new Square(20, Color.BLUE);
// equals()
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
// 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());
}

View File

@ -1,63 +0,0 @@
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;
}
}