Created
February 13, 2016 17:56
-
-
Save FaAway/e30f38dc7d5f94d30e9b to your computer and use it in GitHub Desktop.
javarush level21.lesson05.task03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level21.lesson05.task03; | |
import java.io.*; | |
import java.util.Date; | |
/* Ошибка в equals/hashCode | |
Исправьте ошибки реализаций методов equals и hashCode для класса Solution | |
*/ | |
public class Solution implements Serializable{ | |
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 == null) return false; | |
if (!(o instanceof Solution)) return false; | |
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 true; | |
} | |
@Override | |
public int hashCode() { | |
int result; | |
long temp; | |
result = anInt; | |
result = 31 * result + (string != null ? string.hashCode() : 0); | |
temp = aDouble != +0.0d ? Double.doubleToLongBits(aDouble) : 0L; | |
result = 31 * result + (int) (temp ^ (temp >>> 32)); | |
result = 31 * result + (date != null ? date.hashCode() : 0); | |
result = 31 * result + (solution != null ? solution.hashCode() : 0); | |
return result; | |
} | |
public static void main(String[] args) throws Exception{ | |
Solution solution = new Solution(5, "String", 6.0d, new Date(2015, 01, 13), null); | |
Solution solutionWrapper = new Solution(7, "StringOfWrapper", 8.0d, new Date(2015,01,14), solution); | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
ObjectOutputStream oos = new ObjectOutputStream(out); | |
oos.writeObject(solutionWrapper); | |
oos.close(); | |
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); | |
Solution loadedSolution = (Solution)ois.readObject(); | |
System.out.println(solutionWrapper.equals(loadedSolution)); | |
// возвращает false, но тест на сервере проходит. What the fuck! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment