Доброго времени суток!
Прошу помощи с прохождением последнего условия - валидатор не пропускает.
Вроде бы все уже учел: и проверку на null объекта, и проверку полей, однако не помогает ничего. Вводил и проверку начального объекта на null, но тоже не пропускает.
Сравнение с автоматической генерацией методов тоже ничего не дало - все сравнения оттуда учтены были у меня итак.
Хотелось бы все же разобраться, где именно у меня в голове беспорядок.
package com.javarush.task.task21.task2104;
import java.util.HashSet;
import java.util.Set;
/*
Equals and HashCode
*/
public class Solution {
private final String first, last;
public Solution(String first, String last) {
this.first = first;
this.last = last;
}
public boolean equals(Object n) {
if (this == null) return false;
if (n == null) return false;
if (n.getClass() != this.getClass()) return false;
if (this == n) return true;
Solution other = (Solution) n;
if (((this.first == null) && (this.last != null) && (other.first == null) && (other.last != null)) && (other.last.equals(this.last)))
return true;
if (((this.first != null) && (this.last == null) && (other.first != null) && (other.last == null)) && (other.first.equals(this.first)))
return true;
if (((this.first != null) && (this.last != null) && (other.first != null) && (other.last != null)) && (other.first.equals(this.first) && other.last.equals(this.last)))
return true;
return false;
}
public int hashCode() {
int result = 1;
int a, b;
if (this.first == null) a = -1;
else a = this.first.length();
if (this.last == null) b = -1;
else b = this.last.length();
result = 31 * result + a;
result = 31 * result + b;
return result;
}
public static void main(String[] args) {
Set<Solution> s = new HashSet<>();
s.add(new Solution("Donald", "Duck"));
System.out.println(s.contains(new Solution("Donald", "Duck")));
}
}