2017-01-10 14:11:16 -08:00
|
|
|
// equalshashcode/ComposedEquality.java
|
2017-01-08 22:55:49 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://OnJava8.com for more book information.
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
class Part {
|
|
|
|
String ss;
|
|
|
|
double dd;
|
2017-05-01 14:33:10 -06:00
|
|
|
Part(String ss, double dd) {
|
2017-01-08 22:55:49 -08:00
|
|
|
this.ss = ss;
|
|
|
|
this.dd = dd;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object rval) {
|
|
|
|
return rval instanceof Part &&
|
|
|
|
Objects.equals(ss, ((Part)rval).ss) &&
|
|
|
|
Objects.equals(dd, ((Part)rval).dd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ComposedEquality extends SuccinctEquality {
|
|
|
|
Part part;
|
|
|
|
public ComposedEquality(int i, String s, double d) {
|
|
|
|
super(i, s, d);
|
|
|
|
part = new Part(s, d);
|
|
|
|
System.out.println("made 'ComposedEquality'");
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object rval) {
|
|
|
|
return rval instanceof ComposedEquality &&
|
|
|
|
super.equals(rval) &&
|
2017-05-10 11:45:39 -06:00
|
|
|
Objects.equals(part,
|
|
|
|
((ComposedEquality)rval).part);
|
2017-01-08 22:55:49 -08:00
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Equality.testAll( (i, s, d) ->
|
|
|
|
new ComposedEquality(i, s, d));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
|
|
|
made 'Equality'
|
|
|
|
made 'SuccinctEquality'
|
|
|
|
made 'ComposedEquality'
|
|
|
|
made 'Equality'
|
|
|
|
made 'SuccinctEquality'
|
|
|
|
made 'ComposedEquality'
|
|
|
|
made 'Equality'
|
|
|
|
made 'SuccinctEquality'
|
|
|
|
made 'ComposedEquality'
|
|
|
|
-- Testing null --
|
|
|
|
null instanceof Equality: false
|
|
|
|
Expected false, got false
|
|
|
|
-- Testing same object --
|
|
|
|
same object instanceof Equality: true
|
|
|
|
Expected true, got true
|
|
|
|
-- Testing different type --
|
|
|
|
different type instanceof Equality: false
|
|
|
|
Expected false, got false
|
|
|
|
-- Testing same values --
|
|
|
|
same values instanceof Equality: true
|
|
|
|
Expected true, got true
|
|
|
|
-- Testing different values --
|
|
|
|
different values instanceof Equality: true
|
|
|
|
Expected false, got false
|
|
|
|
*/
|