Skip to content

Commit 79bb072

Browse files
committed
Improvements during editing
1 parent ca066ef commit 79bb072

25 files changed

Lines changed: 434 additions & 195 deletions

collectiontopics/CanonicalMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Element {
1515
@Override
1616
public boolean equals(Object r) {
1717
return r instanceof Element &&
18-
ident.equals(((Element)r).ident);
18+
Objects.equals(ident, ((Element)r).ident);
1919
}
2020
@Override
2121
protected void finalize() {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// collectiontopics/ComposedEquality.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.*;
6+
7+
class Part {
8+
String ss;
9+
double dd;
10+
public Part(String ss, double dd) {
11+
this.ss = ss;
12+
this.dd = dd;
13+
}
14+
@Override
15+
public boolean equals(Object rval) {
16+
return rval instanceof Part &&
17+
Objects.equals(ss, ((Part)rval).ss) &&
18+
Objects.equals(dd, ((Part)rval).dd);
19+
}
20+
}
21+
22+
public class ComposedEquality extends SuccinctEquality {
23+
Part part;
24+
public ComposedEquality(int i, String s, double d) {
25+
super(i, s, d);
26+
part = new Part(s, d);
27+
System.out.println("made 'ComposedEquality'");
28+
}
29+
@Override
30+
public boolean equals(Object rval) {
31+
return rval instanceof ComposedEquality &&
32+
super.equals(rval) &&
33+
Objects.equals(part, ((ComposedEquality)rval).part);
34+
}
35+
public static void main(String[] args) {
36+
Equality.testAll( (i, s, d) ->
37+
new ComposedEquality(i, s, d));
38+
}
39+
}
40+
/* Output:
41+
made 'Equality'
42+
made 'SuccinctEquality'
43+
made 'ComposedEquality'
44+
made 'Equality'
45+
made 'SuccinctEquality'
46+
made 'ComposedEquality'
47+
made 'Equality'
48+
made 'SuccinctEquality'
49+
made 'ComposedEquality'
50+
-- Testing null --
51+
null instanceof Equality: false
52+
Expected false, got false
53+
-- Testing same object --
54+
same object instanceof Equality: true
55+
Expected true, got true
56+
-- Testing different type --
57+
different type instanceof Equality: false
58+
Expected false, got false
59+
-- Testing same values --
60+
same values instanceof Equality: true
61+
Expected true, got true
62+
-- Testing different values --
63+
different values instanceof Equality: true
64+
Expected false, got false
65+
*/

collectiontopics/CountedString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public int hashCode() {
3737
@Override
3838
public boolean equals(Object o) {
3939
return o instanceof CountedString &&
40-
s.equals(((CountedString)o).s) &&
41-
id == ((CountedString)o).id;
40+
Objects.equals(s, ((CountedString)o).s) &&
41+
Objects.equals(id, ((CountedString)o).id);
4242
}
4343
public static void main(String[] args) {
4444
Map<CountedString,Integer> map =

collectiontopics/Equality.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// collectiontopics/Equality.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.*;
6+
7+
public class Equality {
8+
protected int i;
9+
protected String s;
10+
protected double d;
11+
public Equality(int i, String s, double d) {
12+
this.i = i;
13+
this.s = s;
14+
this.d = d;
15+
System.out.println("made 'Equality'");
16+
}
17+
@Override
18+
public boolean equals(Object rval) {
19+
if(rval == null)
20+
return false;
21+
if(rval == this)
22+
return true;
23+
if(!(rval instanceof Equality))
24+
return false;
25+
Equality other = (Equality)rval;
26+
if(!Objects.equals(i, other.i))
27+
return false;
28+
if(!Objects.equals(s, other.s))
29+
return false;
30+
if(!Objects.equals(d, other.d))
31+
return false;
32+
return true;
33+
}
34+
public void
35+
test(String descr, String expected, Object rval) {
36+
System.out.format("-- Testing %s --%n" +
37+
"%s instanceof Equality: %s%n" +
38+
"Expected %s, got %s%n",
39+
descr, descr, rval instanceof Equality,
40+
expected, equals(rval));
41+
}
42+
public static void testAll(EqualityFactory eqf) {
43+
Equality
44+
e = eqf.make(1, "Monty", 3.14),
45+
eq = eqf.make(1, "Monty", 3.14),
46+
neq = eqf.make(99, "Bob", 1.618);
47+
e.test("null", "false", null);
48+
e.test("same object", "true", e);
49+
e.test("different type", "false", new Integer(99));
50+
e.test("same values", "true", eq);
51+
e.test("different values", "false", neq);
52+
}
53+
public static void main(String[] args) {
54+
testAll( (i, s, d) -> new Equality(i, s, d));
55+
}
56+
}
57+
/* Output:
58+
made 'Equality'
59+
made 'Equality'
60+
made 'Equality'
61+
-- Testing null --
62+
null instanceof Equality: false
63+
Expected false, got false
64+
-- Testing same object --
65+
same object instanceof Equality: true
66+
Expected true, got true
67+
-- Testing different type --
68+
different type instanceof Equality: false
69+
Expected false, got false
70+
-- Testing same values --
71+
same values instanceof Equality: true
72+
Expected true, got true
73+
-- Testing different values --
74+
different values instanceof Equality: true
75+
Expected false, got false
76+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// collectiontopics/EqualityFactory.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.*;
6+
7+
interface EqualityFactory {
8+
Equality make(int i, String s, double d);
9+
}

collectiontopics/Groundhog2.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Visit http://OnJava8.com for more book information.
55
// A class that's used as a key in a HashMap
66
// must override hashCode() and equals()
7+
import java.util.*;
78

89
public class Groundhog2 extends Groundhog {
910
public Groundhog2(int n) { super(n); }
@@ -12,6 +13,7 @@ public class Groundhog2 extends Groundhog {
1213
@Override
1314
public boolean equals(Object o) {
1415
return o instanceof Groundhog2 &&
15-
(number == ((Groundhog2)o).number);
16+
Objects.equals(
17+
number, ((Groundhog2)o).number);
1618
}
1719
}

collectiontopics/MapEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public boolean equals(Object o) {
3434
MapEntry<K, V> me = (MapEntry<K, V>)o;
3535
return
3636
(key == null ? me.getKey() == null :
37-
key.equals(me.getKey()))
37+
key.equals(me.getKey()))
3838
&&
3939
(value == null ? me.getValue() == null :
40-
value.equals(me.getValue()));
40+
value.equals(me.getValue()));
4141
}
4242
@Override
4343
public String toString() { return key + "=" + value; }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// collectiontopics/SuccinctEquality.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.*;
6+
7+
public class SuccinctEquality extends Equality {
8+
public SuccinctEquality(int i, String s, double d) {
9+
super(i, s, d);
10+
System.out.println("made 'SuccinctEquality'");
11+
}
12+
@Override
13+
public boolean equals(Object rval) {
14+
return rval instanceof SuccinctEquality &&
15+
Objects.equals(i, ((SuccinctEquality)rval).i) &&
16+
Objects.equals(s, ((SuccinctEquality)rval).s) &&
17+
Objects.equals(d, ((SuccinctEquality)rval).d);
18+
}
19+
public static void main(String[] args) {
20+
Equality.testAll( (i, s, d) ->
21+
new SuccinctEquality(i, s, d));
22+
}
23+
}
24+
/* Output:
25+
made 'Equality'
26+
made 'SuccinctEquality'
27+
made 'Equality'
28+
made 'SuccinctEquality'
29+
made 'Equality'
30+
made 'SuccinctEquality'
31+
-- Testing null --
32+
null instanceof Equality: false
33+
Expected false, got false
34+
-- Testing same object --
35+
same object instanceof Equality: true
36+
Expected true, got true
37+
-- Testing different type --
38+
different type instanceof Equality: false
39+
Expected false, got false
40+
-- Testing same values --
41+
same values instanceof Equality: true
42+
Expected true, got true
43+
-- Testing different values --
44+
different values instanceof Equality: true
45+
Expected false, got false
46+
*/

collectiontopics/TypesForSets.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class SetType {
1212
public SetType(int n) { i = n; }
1313
@Override
1414
public boolean equals(Object o) {
15-
if (o == this) return true;
1615
return o instanceof SetType &&
1716
Objects.equals(i, ((SetType)o).i);
1817
}

collectiontopics/Unsupported.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,28 @@
66
import java.util.*;
77

88
public class Unsupported {
9+
static void check(String description, Runnable tst) {
10+
try {
11+
tst.run();
12+
} catch(Exception e) {
13+
System.out.println(description + "(): " + e);
14+
}
15+
}
916
static void test(String msg, List<String> list) {
1017
System.out.println("--- " + msg + " ---");
1118
Collection<String> c = list;
1219
Collection<String> subList = list.subList(1,8);
1320
// Copy of the sublist:
1421
Collection<String> c2 = new ArrayList<>(subList);
15-
try { c.retainAll(c2); } catch(Exception e) {
16-
System.out.println("retainAll(): " + e);
17-
}
18-
try { c.removeAll(c2); } catch(Exception e) {
19-
System.out.println("removeAll(): " + e);
20-
}
21-
try { c.clear(); } catch(Exception e) {
22-
System.out.println("clear(): " + e);
23-
}
24-
try { c.add("X"); } catch(Exception e) {
25-
System.out.println("add(): " + e);
26-
}
27-
try { c.addAll(c2); } catch(Exception e) {
28-
System.out.println("addAll(): " + e);
29-
}
30-
try { c.remove("C"); } catch(Exception e) {
31-
System.out.println("remove(): " + e);
32-
}
22+
check("retainAll", () -> c.retainAll(c2));
23+
check("removeAll", () -> c.removeAll(c2));
24+
check("clear", () -> c.clear());
25+
check("add", () -> c.add("X"));
26+
check("addAll", () -> c.addAll(c2));
27+
check("remove", () -> c.remove("C"));
3328
// The List.set() method modifies the value but
3429
// doesn't change the size of the data structure:
35-
try {
36-
list.set(0, "X");
37-
} catch(Exception e) {
38-
System.out.println("List.set(): " + e);
39-
}
30+
check("List.set", () -> list.set(0, "X"));
4031
}
4132
public static void main(String[] args) {
4233
List<String> list =

0 commit comments

Comments
 (0)