I need an another eye which needs to put me in the right direction. I simply miss a simple point but have not yet found it.
package com.codegym.task.task21.task2106;
import java.util.Date;
import java.util.Objects;
/*
Bug in equals/hashCode
Correct the implementation errors in the Solution's classes equals and hashCode methods.
Requirements:
1. The hashcodes of identical objects must be equal.
2. The equals method should check whether the passed object is equal to the current object (comparison using ==).
3. The equals method should check whether the passed object is a Solution object.
4. The equals method must check all of the fields of the passed object and the current object (note that some of them may be null).
5. You must ensure that the HashSet of Solution elements behaves correctly.
6. The Solution class must implement the hashCode method.
*/
public class Solution {
private int anInt;
private String string;
private double aDouble;
private Date date;
private Solution solution;
public Solution(int anInt, String string, double aDouble, Date date, Solution solution) {
this.anInt = anInt;
this.string = string;
this.aDouble = aDouble;
this.date = date;
this.solution = solution;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof Solution) return true;
Solution solution1 = (Solution) o;
if (Double.compare(solution1.aDouble, aDouble) != 0) return false;
if (anInt != solution1.anInt) return false;
// if (date != null ? !date.equals(solution1.date) : solution1.date == null) return false;
// if (solution != null ? !solution.equals(solution1.solution) : solution1.solution == null) return false;
// if (string != null ? !string.equals(solution1.string) : solution1.string == null) return false;
return Objects.equals(date, solution1.date)
&& Objects.equals(solution, solution1.solution)
&& Objects.equals(string, solution1.string);
}
@Override
public int hashCode() {
int result;
long temp;
result = anInt;
temp = aDouble != +0.0d ? Double.doubleToLongBits(aDouble) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (solution != null ? solution.hashCode() : 0);
return result;
}
public static void main(String[] args) {
}
}