More Related Content
Similar to こんなEqualsは嫌だ (20)
More from Oda Shinsuke (20)
こんなEqualsは嫌だ
- 11. 反射的 でない! final class反射的{ publicint id; @Override publicboolean equals(Object obj) { if (!(objinstanceof反射的)) { returnfalse; } returnthis.id == ((反射的)obj).id + 1; } } 反射的 x = new反射的(); x.id = 1; System.out.println("x.equals(x):" + x.equals(x)); // false
- 12. 対称的 でない! final class対称的{ publicint value; @Override publicboolean equals(Object obj) { if (this == obj) { returntrue; } if (obj == null) { returnfalse; } try { Field f = obj.getClass().getDeclaredField("value"); f.setAccessible(true); Object objId = f.get(obj); returnthis.value == (Integer)objId; } catch (Throwable e) { // ガン無視! returnfalse; } } }
- 13. 対称的 でない! 対称的 x = new対称的(); x.value= 1; Integer y = Integer.valueOf(1); System.out.println("x.equals(y):" + x.equals(y)); // true System.out.println("y.equals(x):" + y.equals(x)); // false
- 14. 推移的 でない! classSuper推移的{ publicint id; @Override publicboolean equals(Object obj) { if (this == obj) { returntrue; } if (!(objinstanceofSuper推移的)) { returnfalse; } returnthis.id == ((Super推移的)obj).id; } } final classSub推移的 extends Super推移的{ publicintseq; @Override publicboolean equals(Object obj) { if (this == obj) { returntrue; } if (!(objinstanceofSuper推移的)) { returnfalse; } if (objinstanceofSub推移的) { returnthis.id == ((Sub推移的)obj).id && this.seq == ((Sub推移的)obj).seq; } returnsuper.equals(obj); } }
- 15. 推移的 でない! Sub推移的 x = new Sub推移的(); x.id = 1; x.seq= 1; Super推移的 y = new Super推移的(); y.id = 1; Sub推移的 z = new Sub推移的(); z.id = 1; z.seq= 2; System.out.println(“x.equals(y):”+ x.equals(y));// true System.out.println("y.equals(z):" + y.equals(z)); // true System.out.println("x.equals(z):" + x.equals(z)); // false
- 16. 整合的でない! final class整合的{ publicint id; @Override publicboolean equals(Object obj) { if (this == obj) { returntrue; } if (!(objinstanceof整合的)) { returnfalse; } returnthis.id++ == ((整合的)obj).id; } } 整合的 x = new整合的(); x.id = 1; 整合的 y = new整合的(); y.id = 1; System.out.println("x.equals(y):" + x.equals(y)); // true System.out.println("x.equals(y):" + x.equals(y)); // false
- 17. 例外を吐く final class例外{ publicint id; @Override publicboolean equals(Object obj) { returnthis.id == ((例外)obj).id; } } 例外 x= new例外(); x.id = 1; try{ System.out.println(x.equals(null)); } catch (Throwable e) { System.out.println(“equals で例外とかありえなーい”); // こっち通る }
- 18. まとも final classまとも{ publicint id; @Override publicboolean equals(Object obj) { if (this == obj) { returntrue; } if (!(objinstanceofまとも)) { returnfalse; } returnthis.id == ((まとも)obj).id; } @Override publicinthashCode() { returnthis.id; } }
- 19. まとも まとも x= new まとも(); x.id = 1; まとも y = new まとも(); y.id = 1; まとも z = new まとも(); z.id = 1; System.out.println("x.equals(x):" + x.equals(x)); // true System.out.println("x.equals(y):" + x.equals(y)); // true System.out.println("y.equals(x):" + y.equals(x)); // true System.out.println("x.equals(y):" + x.equals(y)); // true System.out.println("y.equals(z):" + y.equals(z)); // true System.out.println("x.equals(z):" + x.equals(z)); // true System.out.println("x.equals(y):" + x.equals(y)); // true System.out.println("x.equals(y):" + x.equals(y)); // true
- 27. HashMapで利用すると… まとも x= new まとも(); x.id = 1; HashMap<まとも, String> map = newHashMap<まとも, String>(); map.put(x, "A"); System.out.println(map.get(x)); // A x.id = 3; System.out.println(map.get(x)); // null