From a8a175c398fdeae372b8c6ed2e2b784099aa1fee Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Wed, 18 Jan 2023 14:23:14 +0100 Subject: [PATCH] Using instanceOf enhancements --- Doc/Sd1/Collections/collections.xml | 7 +- Doc/Sd1/CoreClasses/coreClasses.xml | 15 ++--- Doc/Sd1/Inheritance/inheritance.xml | 101 +++++----------------------- 3 files changed, 29 insertions(+), 94 deletions(-) diff --git a/Doc/Sd1/Collections/collections.xml b/Doc/Sd1/Collections/collections.xml index 0e607adea..70044ed57 100644 --- a/Doc/Sd1/Collections/collections.xml +++ b/Doc/Sd1/Collections/collections.xml @@ -2,6 +2,7 @@ <chapter version="5.1" xml:id="sd1Collection1" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns="http://docbook.org/ns/transclusion" @@ -279,9 +280,9 @@ c12.equals("dummy"):false @Override public boolean equals(Object obj) { - if (obj instanceof Coordinate) { - return x == ((Coordinate)obj).x && - y == ((Coordinate)obj).y; + if (obj instanceof Coordinate <emphasis role="red">c</emphasis>) { + return x == <emphasis role="red">c</emphasis>.x && + y == <emphasis role="red">c</emphasis>.y; } else { return false; } diff --git a/Doc/Sd1/CoreClasses/coreClasses.xml b/Doc/Sd1/CoreClasses/coreClasses.xml index 0aa815184..9ae0e8da4 100644 --- a/Doc/Sd1/CoreClasses/coreClasses.xml +++ b/Doc/Sd1/CoreClasses/coreClasses.xml @@ -358,9 +358,9 @@ public boolean equals(Object anObject) { <programlisting language="java">public class Rectangle { int width, height; @Override public boolean equals(Object o) { - if (o instanceof Rectangle) { - return width == ((Rectangle) o).width - && height == ((Rectangle) o).height; + if (o instanceof Rectangle <emphasis role="red">r</emphasis>) { + return width == <emphasis role="red">r</emphasis>.width + && height == <emphasis role="red">r</emphasis>.height; } else { return false; } @@ -464,11 +464,10 @@ public boolean equals(Object anObject) { @Override public boolean equals(final Object o) { - if (o instanceof TimePeriod) { - final TimePeriod other = (TimePeriod) o; - return hours == other.hours && - minutes == other.minutes && - seconds == other.seconds; + if (o instanceof TimePeriod <emphasis role="red">t</emphasis>) { + return hours == <emphasis role="red">t</emphasis>.hours && + minutes == <emphasis role="red">t</emphasis>.minutes && + seconds == <emphasis role="red">t</emphasis>.seconds; } else { return false; } diff --git a/Doc/Sd1/Inheritance/inheritance.xml b/Doc/Sd1/Inheritance/inheritance.xml index b2067bb3d..8d09d39fc 100644 --- a/Doc/Sd1/Inheritance/inheritance.xml +++ b/Doc/Sd1/Inheritance/inheritance.xml @@ -351,12 +351,11 @@ public <link ... @Override <co linkends="sda_inherit_fig_ShapeEquals-1" xml:id="sda_inherit_fig_ShapeEquals-1-co"/> public boolean equals(final Object o) { - if (o instanceof Shape <co linkends="sda_inherit_fig_ShapeEquals-2" - xml:id="sda_inherit_fig_ShapeEquals-2-co"/>) { - final Shape other = (Shape) o; <co - linkends="sda_inherit_fig_ShapeEquals-3" - xml:id="sda_inherit_fig_ShapeEquals-3-co"/> - return x == other.x && y == other.y; <co + if (o instanceof Shape <emphasis role="red">s</emphasis> <co + linkends="sda_inherit_fig_ShapeEquals-2" + xml:id="sda_inherit_fig_ShapeEquals-2-co"/>) { + return x == <emphasis role="red">s</emphasis>.x && y == <emphasis + role="red">s</emphasis>.y; <co linkends="sda_inherit_fig_ShapeEquals-4" xml:id="sda_inherit_fig_ShapeEquals-4-co"/> } else { @@ -377,12 +376,6 @@ public <link object?</para> </callout> - <callout arearefs="sda_inherit_fig_ShapeEquals-3-co" - xml:id="sda_inherit_fig_ShapeEquals-3"> - <para>Casting other object to class - <classname>Shape</classname>.</para> - </callout> - <callout arearefs="sda_inherit_fig_ShapeEquals-4-co" xml:id="sda_inherit_fig_ShapeEquals-4"> <para>Return <code language="java">true</code> if and only if both @@ -403,11 +396,11 @@ public <link <programlisting language="java">public class Rectangle extends Shape { ... @Override public boolean equals(final Object o) { - if (o instanceof Rectangle) { - final Rectangle other = (Rectangle) o; + if (o instanceof Rectangle <emphasis role="red">r</emphasis>) { return super.equals(o) <co linkends="sda_inherit_fig_RectangleEquals-1" xml:id="sda_inherit_fig_RectangleEquals-1-co"/> && - width == other.width && height == other.height <co + width == <emphasis role="red">r</emphasis> && height == <emphasis + role="red">r</emphasis> <co linkends="sda_inherit_fig_RectangleEquals-2" xml:id="sda_inherit_fig_RectangleEquals-2-co"/>; } else { @@ -590,7 +583,7 @@ if (day.equals(Day.FRIDAY)) ... // Oops: NPE approaching ...</programlisting> <title>Overriding toString()</title> <figure xml:id="sda_inherit_fig_shapeInstanceLog"> - <title> Printing a <classname>Shape</classname>'s info</title> + <title>Printing a <classname>Shape</classname>'s info</title> <informaltable border="0"> <tr> @@ -1820,12 +1813,13 @@ System.out.print(c.equals(c)); //<emphasis role="red">true</emphasis>: Object <programlisting language="java">public class Rectangle extends Shape { @Override public boolean equals(Object o) { if (o <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2">instanceof</link> Rectangle) { + xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2">instanceof</link> Rectangle <emphasis + role="red">r</emphasis>) { final Rectangle oRectangle = (Rectangle) o; // Cast is «legal» - return super.equalCenter(oRectangle) && - Math.abs(oRectangle.width- width) + - Math.abs(oRectangle.height- height) < 1.E-15; + return super.equalCenter(<emphasis role="red">r</emphasis>) && + Math.abs(<emphasis role="red">r</emphasis>.width- width) + + Math.abs(<emphasis role="red">r</emphasis>.height- height) < 1.E-15; } return false; } @@ -1837,65 +1831,6 @@ System.out.print(c.equals(c)); //<emphasis role="red">true</emphasis>: Object Rectangle</code> evaluates to <code language="java">false</code>.</para> </figure> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_whyLegalCast"> - <title>Why is <code language="java">oRectangle = (Rectangle) o</code> a - legal cast?</title> - - <qandadiv> - <qandaentry> - <question> - <para>We consider:</para> - - <programlisting language="java">Object o = new Scanner(System.in); -String s = (String) o;</programlisting> - - <para>On execution we receive a <classname - xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ClassCastException.html">java.lang.ClassCastException</classname>:</para> - - <screen>Exception in thread "main" java.lang.ClassCastException: - class java.util.Scanner cannot be cast to class java.lang.String - at q.App.main(App.java:20)</screen> - - <para>The comment in <xref - linkend="sd1_inherit_fig_rectangleEquals"/> states the following - cast to be legal:</para> - - <programlisting language="java">final Rectangle oRectangle = (Rectangle) o; // Cast is «legal»</programlisting> - - <para>Why is this claim indeed true?</para> - - <para>Don't try digging too deep: The answer is quite - simple.</para> - </question> - - <answer> - <para>The cast in question appears within the following <code - language="java">if</code> block:</para> - - <programlisting language="java">if (o <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2">instanceof</link> Rectangle) { - final Rectangle oRectangle = (Rectangle) o; - ... -}</programlisting> - - <para>Thus the cast will only be executed if our variable <code - language="java">o</code> actually refers to an instance of class - <classname>Rectangle</classname>. In other words: The <code - language="java">if</code> block guards our code against illegal - cast attempts.</para> - - <para>Rewriting our faulty code accordingly also removes the - initial example's runtime exception:</para> - - <programlisting language="java">Object o = new Scanner(System.in); -if (o <link xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2">instanceof</link> String) { - String s = (String) o; // Unreachable code -}</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - <figure xml:id="sd1_inherit_fig_circleEquals"> <title>Implementing <classname>Circle</classname>.<methodname xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)">equals()</methodname></title> @@ -1903,10 +1838,10 @@ if (o <link xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15 <programlisting language="java">public class Circle extends Shape { @Override public boolean equals(final Object o) { if (o <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2">instanceof</link> Circle){ - final Circle oCircle = (Circle) o; - return super.equalCenter(o) && - Math.abs(oCircle.radius - radius) < 1.E-15; + xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2">instanceof</link> Circle <emphasis + role="red">c</emphasis>){ + return super.equalCenter(<emphasis role="red">c</emphasis>) && + Math.abs(<emphasis role="red">c</emphasis>.radius - radius) < 1.E-15; } return false; } ...</programlisting> -- GitLab