diff --git a/collectiontopics/CanonicalMapping.java b/collectiontopics/CanonicalMapping.java index 330b381c..15ffcb06 100644 --- a/collectiontopics/CanonicalMapping.java +++ b/collectiontopics/CanonicalMapping.java @@ -15,7 +15,7 @@ class Element { @Override public boolean equals(Object r) { return r instanceof Element && - ident.equals(((Element)r).ident); + Objects.equals(ident, ((Element)r).ident); } @Override protected void finalize() { diff --git a/collectiontopics/ComposedEquality.java b/collectiontopics/ComposedEquality.java new file mode 100644 index 00000000..246f190b --- /dev/null +++ b/collectiontopics/ComposedEquality.java @@ -0,0 +1,65 @@ +// collectiontopics/ComposedEquality.java +// (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; + public Part(String ss, double dd) { + 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) && + Objects.equals(part, ((ComposedEquality)rval).part); + } + 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 +*/ diff --git a/collectiontopics/CountedString.java b/collectiontopics/CountedString.java index 409bc98c..3d3d370f 100644 --- a/collectiontopics/CountedString.java +++ b/collectiontopics/CountedString.java @@ -37,8 +37,8 @@ public class CountedString { @Override public boolean equals(Object o) { return o instanceof CountedString && - s.equals(((CountedString)o).s) && - id == ((CountedString)o).id; + Objects.equals(s, ((CountedString)o).s) && + Objects.equals(id, ((CountedString)o).id); } public static void main(String[] args) { Map map = diff --git a/collectiontopics/Equality.java b/collectiontopics/Equality.java new file mode 100644 index 00000000..b4e9f7fe --- /dev/null +++ b/collectiontopics/Equality.java @@ -0,0 +1,76 @@ +// collectiontopics/Equality.java +// (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.*; + +public class Equality { + protected int i; + protected String s; + protected double d; + public Equality(int i, String s, double d) { + this.i = i; + this.s = s; + this.d = d; + System.out.println("made 'Equality'"); + } + @Override + public boolean equals(Object rval) { + if(rval == null) + return false; + if(rval == this) + return true; + if(!(rval instanceof Equality)) + return false; + Equality other = (Equality)rval; + if(!Objects.equals(i, other.i)) + return false; + if(!Objects.equals(s, other.s)) + return false; + if(!Objects.equals(d, other.d)) + return false; + return true; + } + public void + test(String descr, String expected, Object rval) { + System.out.format("-- Testing %s --%n" + + "%s instanceof Equality: %s%n" + + "Expected %s, got %s%n", + descr, descr, rval instanceof Equality, + expected, equals(rval)); + } + public static void testAll(EqualityFactory eqf) { + Equality + e = eqf.make(1, "Monty", 3.14), + eq = eqf.make(1, "Monty", 3.14), + neq = eqf.make(99, "Bob", 1.618); + e.test("null", "false", null); + e.test("same object", "true", e); + e.test("different type", "false", new Integer(99)); + e.test("same values", "true", eq); + e.test("different values", "false", neq); + } + public static void main(String[] args) { + testAll( (i, s, d) -> new Equality(i, s, d)); + } +} +/* Output: +made 'Equality' +made 'Equality' +made 'Equality' +-- 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 +*/ diff --git a/collectiontopics/EqualityFactory.java b/collectiontopics/EqualityFactory.java new file mode 100644 index 00000000..0fe779b2 --- /dev/null +++ b/collectiontopics/EqualityFactory.java @@ -0,0 +1,9 @@ +// collectiontopics/EqualityFactory.java +// (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.*; + +interface EqualityFactory { + Equality make(int i, String s, double d); +} diff --git a/collectiontopics/Groundhog2.java b/collectiontopics/Groundhog2.java index e79e581d..a642c706 100644 --- a/collectiontopics/Groundhog2.java +++ b/collectiontopics/Groundhog2.java @@ -4,6 +4,7 @@ // Visit http://OnJava8.com for more book information. // A class that's used as a key in a HashMap // must override hashCode() and equals() +import java.util.*; public class Groundhog2 extends Groundhog { public Groundhog2(int n) { super(n); } @@ -12,6 +13,7 @@ public class Groundhog2 extends Groundhog { @Override public boolean equals(Object o) { return o instanceof Groundhog2 && - (number == ((Groundhog2)o).number); + Objects.equals( + number, ((Groundhog2)o).number); } } diff --git a/collectiontopics/MapEntry.java b/collectiontopics/MapEntry.java index 66ef9af3..80979cb0 100644 --- a/collectiontopics/MapEntry.java +++ b/collectiontopics/MapEntry.java @@ -34,10 +34,10 @@ public class MapEntry implements Map.Entry { MapEntry me = (MapEntry)o; return (key == null ? me.getKey() == null : - key.equals(me.getKey())) + key.equals(me.getKey())) && (value == null ? me.getValue() == null : - value.equals(me.getValue())); + value.equals(me.getValue())); } @Override public String toString() { return key + "=" + value; } diff --git a/collectiontopics/SuccinctEquality.java b/collectiontopics/SuccinctEquality.java new file mode 100644 index 00000000..dbb3c349 --- /dev/null +++ b/collectiontopics/SuccinctEquality.java @@ -0,0 +1,46 @@ +// collectiontopics/SuccinctEquality.java +// (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.*; + +public class SuccinctEquality extends Equality { + public SuccinctEquality(int i, String s, double d) { + super(i, s, d); + System.out.println("made 'SuccinctEquality'"); + } + @Override + public boolean equals(Object rval) { + return rval instanceof SuccinctEquality && + Objects.equals(i, ((SuccinctEquality)rval).i) && + Objects.equals(s, ((SuccinctEquality)rval).s) && + Objects.equals(d, ((SuccinctEquality)rval).d); + } + public static void main(String[] args) { + Equality.testAll( (i, s, d) -> + new SuccinctEquality(i, s, d)); + } +} +/* Output: +made 'Equality' +made 'SuccinctEquality' +made 'Equality' +made 'SuccinctEquality' +made 'Equality' +made 'SuccinctEquality' +-- 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 +*/ diff --git a/collectiontopics/TypesForSets.java b/collectiontopics/TypesForSets.java index 50140330..49929500 100644 --- a/collectiontopics/TypesForSets.java +++ b/collectiontopics/TypesForSets.java @@ -12,7 +12,6 @@ class SetType { public SetType(int n) { i = n; } @Override public boolean equals(Object o) { - if (o == this) return true; return o instanceof SetType && Objects.equals(i, ((SetType)o).i); } diff --git a/collectiontopics/Unsupported.java b/collectiontopics/Unsupported.java index 0cbb418d..0eb3c93c 100644 --- a/collectiontopics/Unsupported.java +++ b/collectiontopics/Unsupported.java @@ -6,37 +6,28 @@ import java.util.*; public class Unsupported { + static void check(String description, Runnable tst) { + try { + tst.run(); + } catch(Exception e) { + System.out.println(description + "(): " + e); + } + } static void test(String msg, List list) { System.out.println("--- " + msg + " ---"); Collection c = list; Collection subList = list.subList(1,8); // Copy of the sublist: Collection c2 = new ArrayList<>(subList); - try { c.retainAll(c2); } catch(Exception e) { - System.out.println("retainAll(): " + e); - } - try { c.removeAll(c2); } catch(Exception e) { - System.out.println("removeAll(): " + e); - } - try { c.clear(); } catch(Exception e) { - System.out.println("clear(): " + e); - } - try { c.add("X"); } catch(Exception e) { - System.out.println("add(): " + e); - } - try { c.addAll(c2); } catch(Exception e) { - System.out.println("addAll(): " + e); - } - try { c.remove("C"); } catch(Exception e) { - System.out.println("remove(): " + e); - } + check("retainAll", () -> c.retainAll(c2)); + check("removeAll", () -> c.removeAll(c2)); + check("clear", () -> c.clear()); + check("add", () -> c.add("X")); + check("addAll", () -> c.addAll(c2)); + check("remove", () -> c.remove("C")); // The List.set() method modifies the value but // doesn't change the size of the data structure: - try { - list.set(0, "X"); - } catch(Exception e) { - System.out.println("List.set(): " + e); - } + check("List.set", () -> list.set(0, "X")); } public static void main(String[] args) { List list = diff --git a/generics/Holder.java b/generics/Holder.java index dec557cb..6e17cb1e 100644 --- a/generics/Holder.java +++ b/generics/Holder.java @@ -12,7 +12,6 @@ public class Holder { public T get() { return value; } @Override public boolean equals(Object o) { - if (o == this) return true; return o instanceof Holder && Objects.equals(value, ((Holder)o).value); } diff --git a/onjava/CountMap.java b/onjava/CountMap.java index bd653f88..31bc8632 100644 --- a/onjava/CountMap.java +++ b/onjava/CountMap.java @@ -6,6 +6,7 @@ // {java onjava.CountMap} package onjava; import java.util.*; +import java.util.stream.*; public class CountMap extends AbstractMap { @@ -31,7 +32,8 @@ extends AbstractMap { Entry(int index) { this.index = index; } @Override public boolean equals(Object o) { - return Integer.valueOf(index).equals(o); + return o instanceof Entry && + Objects.equals(index, ((Entry)o).index); } @Override public Integer getKey() { return index; } @@ -49,11 +51,10 @@ extends AbstractMap { @Override public Set> entrySet() { // LinkedHashSet retains initialization order: - Set> entries = - new LinkedHashSet<>(); - for(int i = 0; i < size; i++) - entries.add(new Entry(i)); - return entries; + return IntStream.range(0, size) + .mapToObj(Entry::new) + .collect( + Collectors.toCollection(LinkedHashSet::new)); } public static void main(String[] args) { CountMap cm = new CountMap(60); diff --git a/onjava/Countries.java b/onjava/Countries.java index 4cd97634..467bf085 100644 --- a/onjava/Countries.java +++ b/onjava/Countries.java @@ -220,7 +220,12 @@ public class Countries { Entry(int index) { this.index = index; } @Override public boolean equals(Object o) { - return DATA[index][0].equals(o); + return o instanceof FlyweightMap && + Objects.equals(DATA[index][0], o); + } + @Override + public int hashCode() { + return DATA[index][0].hashCode(); } @Override public String getKey() { return DATA[index][0]; } @@ -230,10 +235,6 @@ public class Countries { public String setValue(String value) { throw new UnsupportedOperationException(); } - @Override - public int hashCode() { - return DATA[index][0].hashCode(); - } } // Implement size() & iterator() for AbstractSet: static class EntrySet diff --git a/patterns/ShapeFactory1.java b/patterns/ShapeFactory1.java new file mode 100644 index 00000000..55403315 --- /dev/null +++ b/patterns/ShapeFactory1.java @@ -0,0 +1,47 @@ +// patterns/ShapeFactory1.java +// (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. +// A simple static factory method +import java.util.*; +import java.util.stream.*; +import patterns.shapes.*; + +class StaticFactory { + static Shape create(String type) { + switch(type) { + case "Circle": return new Circle(); + case "Square": return new Square(); + case "Triangle": return new Triangle(); + default: + throw new BadShapeCreation(type); + } + } +} + +public class ShapeFactory1 { + public static void main(String[] args) { + Stream.of("Circle", "Square", "Triangle", + "Square", "Circle", "Circle", "Triangle") + .map(StaticFactory::create) + .peek(Shape::draw) + .peek(Shape::erase) + .count(); // Terminal operation + } +} +/* Output: +Circle[0] draw +Circle[0] erase +Square[1] draw +Square[1] erase +Triangle[2] draw +Triangle[2] erase +Square[3] draw +Square[3] erase +Circle[4] draw +Circle[4] erase +Circle[5] draw +Circle[5] erase +Triangle[6] draw +Triangle[6] erase +*/ diff --git a/patterns/ShapeFactory2.java b/patterns/ShapeFactory2.java index 67633a3a..6670c0d3 100644 --- a/patterns/ShapeFactory2.java +++ b/patterns/ShapeFactory2.java @@ -2,87 +2,60 @@ // (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. -// Polymorphic factory methods import java.util.*; -import java.util.function.*; +import java.lang.reflect.*; import java.util.stream.*; +import patterns.shapes.*; -class BadShapeCreation extends RuntimeException { - BadShapeCreation(String msg) { - super(msg); - } -} - -interface Shape { - void draw(); - void erase(); -} - -abstract class ShapeFactory { - static Map> factories = +class DynamicFactory { + static Map factories = new HashMap<>(); - static Shape createShape(String id) { - if(!factories.containsKey(id)) { - try { - Class.forName(id); // Load dynamically - } catch(ClassNotFoundException e) { - throw new BadShapeCreation(id); - } - // See if it was put in: - if(!factories.containsKey(id)) - throw new BadShapeCreation(id); + static Constructor load(String id) { + System.out.println("loading " + id); + try { + return Class.forName("patterns.shapes." + id) + .getConstructor(); + } catch(Exception e) { + throw new BadShapeCreation(id); } - return factories.get(id).get(); } -} - -final class Circle implements Shape { - private Circle() {} - public void draw() { - System.out.println("Circle.draw"); - } - public void erase() { - System.out.println("Circle.erase"); - } - static { - ShapeFactory.factories.put("Circle", Circle::new); - } -} - -final class Square implements Shape { - private Square() {} - public void draw() { - System.out.println("Square.draw"); - } - public void erase() { - System.out.println("Square.erase"); - } - static { - ShapeFactory.factories.put("Square", Square::new); + static Shape create(String id) { + try { + return (Shape)factories + .computeIfAbsent(id, DynamicFactory::load) + .newInstance(); + } catch(Exception e) { + throw new BadShapeCreation(id); + } } } public class ShapeFactory2 { public static void main(String[] args) { - List shapes = Stream.of("Circle", "Square", - "Square", "Circle", "Circle", "Square") - .map(ShapeFactory::createShape) - .collect(Collectors.toList()); - shapes.forEach(Shape::draw); - shapes.forEach(Shape::erase); + Stream.of("Circle", "Square", "Triangle", + "Square", "Circle", "Circle", "Triangle") + .map(DynamicFactory::create) + .peek(Shape::draw) + .peek(Shape::erase) + .count(); } } /* Output: -Circle.draw -Square.draw -Square.draw -Circle.draw -Circle.draw -Square.draw -Circle.erase -Square.erase -Square.erase -Circle.erase -Circle.erase -Square.erase +loading Circle +Circle[0] draw +Circle[0] erase +loading Square +Square[1] draw +Square[1] erase +loading Triangle +Triangle[2] draw +Triangle[2] erase +Square[3] draw +Square[3] erase +Circle[4] draw +Circle[4] erase +Circle[5] draw +Circle[5] erase +Triangle[6] draw +Triangle[6] erase */ diff --git a/patterns/ShapeFactory3.java b/patterns/ShapeFactory3.java new file mode 100644 index 00000000..c9c8a79a --- /dev/null +++ b/patterns/ShapeFactory3.java @@ -0,0 +1,53 @@ +// patterns/ShapeFactory3.java +// (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. +// Polymorphic factory methods +import java.util.*; +import java.util.function.*; +import java.util.stream.*; +import patterns.shapes.*; + +interface PolymorphicFactory { + Shape create(); +} + +class RandomShapes implements Supplier { + private final PolymorphicFactory[] factories; + private Random rand = new Random(42); + public + RandomShapes(PolymorphicFactory... factories) { + this.factories = factories; + } + public Shape get() { + return factories[ + rand.nextInt(factories.length)].create(); + } +} + +public class ShapeFactory3 { + public static void main(String[] args) { + RandomShapes rs = new RandomShapes( + Circle::new, Square::new, Triangle::new + ); + Stream.generate(rs) + .limit(6) + .peek(Shape::draw) + .peek(Shape::erase) + .count(); + } +} +/* Output: +Triangle[0] draw +Triangle[0] erase +Circle[1] draw +Circle[1] erase +Circle[2] draw +Circle[2] erase +Triangle[3] draw +Triangle[3] erase +Circle[4] draw +Circle[4] erase +Square[5] draw +Square[5] erase +*/ diff --git a/patterns/absfactory/GameEnvironment.java b/patterns/abstractfactory/GameEnvironment.java similarity index 93% rename from patterns/absfactory/GameEnvironment.java rename to patterns/abstractfactory/GameEnvironment.java index c583796d..16acdc68 100644 --- a/patterns/absfactory/GameEnvironment.java +++ b/patterns/abstractfactory/GameEnvironment.java @@ -1,10 +1,10 @@ -// patterns/absfactory/GameEnvironment.java +// patterns/abstractfactory/GameEnvironment.java // (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. // An example of the Abstract Factory pattern -// {java patterns.absfactory.GameEnvironment} -package patterns.absfactory; +// {java patterns.abstractfactory.GameEnvironment} +package patterns.abstractfactory; import java.util.function.*; interface Obstacle { diff --git a/patterns/factory/ShapeFactory1.java b/patterns/factory/ShapeFactory1.java deleted file mode 100644 index 109c1ead..00000000 --- a/patterns/factory/ShapeFactory1.java +++ /dev/null @@ -1,79 +0,0 @@ -// patterns/factory/ShapeFactory1.java -// (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. -// A simple static factory method -// {java patterns.factory.ShapeFactory1} -package patterns.factory; -import java.util.*; -import java.util.stream.*; - -class BadShapeCreation extends RuntimeException { - BadShapeCreation(String msg) { - super(msg); - } -} - -abstract class Shape { - public abstract void draw(); - public abstract void erase(); - static Shape factory(String type) { - switch(type) { - case "Circle": return new Circle(); - case "Square": return new Square(); - default: - throw new BadShapeCreation(type); - } - } -} - -class Circle extends Shape { - Circle() {} // Friendly constructor - @Override - public void draw() { - System.out.println("Circle.draw"); - } - @Override - public void erase() { - System.out.println("Circle.erase"); - } -} - -class Square extends Shape { - Square() {} // Friendly constructor - @Override - public void draw() { - System.out.println("Square.draw"); - } - @Override - public void erase() { - System.out.println("Square.erase"); - } -} - -public class ShapeFactory1 { - public static void main(String[] args) { - List shapes = Stream.of( - "Circle", "Square", - "Square", "Circle", - "Circle", "Square") - .map(Shape::factory) - .collect(Collectors.toList()); - shapes.forEach(Shape::draw); - shapes.forEach(Shape::erase); - } -} -/* Output: -Circle.draw -Square.draw -Square.draw -Circle.draw -Circle.draw -Square.draw -Circle.erase -Square.erase -Square.erase -Circle.erase -Circle.erase -Square.erase -*/ diff --git a/patterns/recyclea/RecycleA.java b/patterns/recyclea/RecycleA.java index 9dc54a75..702fdb97 100644 --- a/patterns/recyclea/RecycleA.java +++ b/patterns/recyclea/RecycleA.java @@ -66,7 +66,8 @@ class TrashFactory { Arrays.asList( Aluminum::new, Paper::new, Glass::new); static final int SZ = ttypes.size(); - private static SplittableRandom rand = new SplittableRandom(47); + private static SplittableRandom rand = + new SplittableRandom(47); public static Trash newTrash() { return ttypes .get(rand.nextInt(SZ)) diff --git a/patterns/shapes/BadShapeCreation.java b/patterns/shapes/BadShapeCreation.java new file mode 100644 index 00000000..daa3f7f5 --- /dev/null +++ b/patterns/shapes/BadShapeCreation.java @@ -0,0 +1,12 @@ +// patterns/shapes/BadShapeCreation.java +// (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. +package patterns.shapes; + +public class BadShapeCreation +extends RuntimeException { + public BadShapeCreation(String msg) { + super(msg); + } +} diff --git a/patterns/shapes/Circle.java b/patterns/shapes/Circle.java new file mode 100644 index 00000000..b50d5abf --- /dev/null +++ b/patterns/shapes/Circle.java @@ -0,0 +1,7 @@ +// patterns/shapes/Circle.java +// (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. +package patterns.shapes; + +public class Circle extends Shape {} diff --git a/patterns/shapes/Shape.java b/patterns/shapes/Shape.java new file mode 100644 index 00000000..dfff783c --- /dev/null +++ b/patterns/shapes/Shape.java @@ -0,0 +1,21 @@ +// patterns/shapes/Shape.java +// (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. +package patterns.shapes; + +public class Shape { + private static int counter = 0; + private int id = counter++; + @Override + public String toString() { + return + getClass().getSimpleName() + "[" + id + "]"; + } + public void draw() { + System.out.println(this + " draw"); + } + public void erase() { + System.out.println(this + " erase"); + } +} diff --git a/patterns/shapes/Square.java b/patterns/shapes/Square.java new file mode 100644 index 00000000..602b3282 --- /dev/null +++ b/patterns/shapes/Square.java @@ -0,0 +1,7 @@ +// patterns/shapes/Square.java +// (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. +package patterns.shapes; + +public class Square extends Shape {} diff --git a/patterns/shapes/Triangle.java b/patterns/shapes/Triangle.java new file mode 100644 index 00000000..11559f89 --- /dev/null +++ b/patterns/shapes/Triangle.java @@ -0,0 +1,7 @@ +// patterns/shapes/Triangle.java +// (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. +package patterns.shapes; + +public class Triangle extends Shape {} diff --git a/typeinfo/pets/Individual.java b/typeinfo/pets/Individual.java index fd54f787..e30bce87 100644 --- a/typeinfo/pets/Individual.java +++ b/typeinfo/pets/Individual.java @@ -3,6 +3,7 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. package typeinfo.pets; +import java.util.*; public class Individual implements Comparable { @@ -21,7 +22,7 @@ Individual implements Comparable { @Override public boolean equals(Object o) { return o instanceof Individual && - id == ((Individual)o).id; + Objects.equals(id, ((Individual)o).id); } @Override public int hashCode() {