From ded2d52c66609d165b4c6e5c429caae71fb0b681 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Wed, 3 Jun 2015 11:41:10 -0700 Subject: [PATCH] edits --- Checklist.txt | 2 +- Copyright.txt | 9 +-- concurrency/CarBuilder.java | 3 +- concurrency/CountDownLatchDemo.java | 3 +- concurrency/DaemonFromFactory.java | 2 +- .../DeadlockingDiningPhilosophers.java | 3 +- concurrency/FastSimulation.java | 3 +- concurrency/FixedDiningPhilosophers.java | 3 +- concurrency/GreenhouseScheduler.java | 3 +- concurrency/HorseRace.java | 4 +- concurrency/PriorityBlockingQueueDemo.java | 3 +- concurrency/ReaderWriterList.java | 3 +- concurrency/SemaphoreDemo.java | 3 +- concurrency/ThreadVariations.java | 2 +- concurrency/ToastOMatic.java | 2 +- concurrency/build.xml | 1 + containers/CanonicalMapping.java | 3 +- containers/Lists.java | 3 +- containers/References.java | 3 +- control/WhileTest.java | 3 +- enums/ConstantSpecificMethod.java | 2 +- generics/Apply.java | 3 +- gui/LookAndFeel.java | 73 +++++++++++-------- holding/ApplesAndOrangesWithoutGenerics.java | 4 +- io/BufferedInputFile.java | 6 +- io/FileOutputShortcut.java | 8 +- network/ChatterClient.java | 6 +- network/ChatterServer.java | 2 +- patterns/ShapeFactory2.java | 3 +- patterns/factory/ShapeFactory1.java | 3 +- patterns/trash/ParseTrash.java | 2 +- references/DeepCopy.java | 6 +- typeinfo/PetCount2.java | 2 +- 33 files changed, 102 insertions(+), 79 deletions(-) diff --git a/Checklist.txt b/Checklist.txt index e5b961cc..45c52529 100644 --- a/Checklist.txt +++ b/Checklist.txt @@ -32,7 +32,7 @@ [ ] Appendix: Passing & Returning Objects [X] Appendix: Network Programming [X] Appendix: Remote Methods -[ ] Appendix: Logging +[X] Appendix: Logging [X] Appendix: Debugging [X] Appendix: Profiling and Optimizing [ ] Appendix: The Benefits and Costs of Static Type Checking diff --git a/Copyright.txt b/Copyright.txt index d30c4a4c..90ff16e6 100644 --- a/Copyright.txt +++ b/Copyright.txt @@ -64,11 +64,10 @@ ENHANCEMENTS, OR MODIFICATIONS. Please note that MindView LLC maintains a Web site which is the sole distribution point for electronic copies of the -Source Code, http://www.MindView.net (and official mirror -sites), where it is freely available under the terms stated -above. +Source Code, https://github.com/BruceEckel/TIJ-Directors-Cut/, +where it is freely available under the terms stated above. If you think you've found an error in the Source Code, -please submit a correction using the feedback system that -you will find at http://www.MindView.net. +please submit a correction at: +https://github.com/BruceEckel/TIJ-Directors-Cut/issues ///:~ diff --git a/concurrency/CarBuilder.java b/concurrency/CarBuilder.java index 54d503c7..5a67c035 100644 --- a/concurrency/CarBuilder.java +++ b/concurrency/CarBuilder.java @@ -1,6 +1,7 @@ //: concurrency/CarBuilder.java // ©2015 MindView LLC: see Copyright.txt // A complex example of tasks working together. +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; import java.util.*; import static net.mindview.util.Print.*; @@ -209,4 +210,4 @@ public class CarBuilder { TimeUnit.SECONDS.sleep(7); exec.shutdownNow(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/CountDownLatchDemo.java b/concurrency/CountDownLatchDemo.java index 11e635c1..0e0c405e 100644 --- a/concurrency/CountDownLatchDemo.java +++ b/concurrency/CountDownLatchDemo.java @@ -1,5 +1,6 @@ //: concurrency/CountDownLatchDemo.java // ©2015 MindView LLC: see Copyright.txt +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; import java.util.*; import static net.mindview.util.Print.*; @@ -68,4 +69,4 @@ public class CountDownLatchDemo { print("Launched all tasks"); exec.shutdown(); // Quit when all tasks complete } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/DaemonFromFactory.java b/concurrency/DaemonFromFactory.java index abd2639c..c281114d 100644 --- a/concurrency/DaemonFromFactory.java +++ b/concurrency/DaemonFromFactory.java @@ -25,4 +25,4 @@ public class DaemonFromFactory implements Runnable { print("All daemons started"); TimeUnit.MILLISECONDS.sleep(500); // Run for a while } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/DeadlockingDiningPhilosophers.java b/concurrency/DeadlockingDiningPhilosophers.java index f5f0c3cb..ae94145f 100644 --- a/concurrency/DeadlockingDiningPhilosophers.java +++ b/concurrency/DeadlockingDiningPhilosophers.java @@ -2,6 +2,7 @@ // ©2015 MindView LLC: see Copyright.txt // Demonstrates how deadlock can be hidden in a program. // {Args: 0 5 timeout} +// {FirstAndLast: 10 Lines} import java.util.concurrent.*; public class DeadlockingDiningPhilosophers { @@ -27,4 +28,4 @@ public class DeadlockingDiningPhilosophers { } exec.shutdownNow(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/FastSimulation.java b/concurrency/FastSimulation.java index 770a3b25..d8fb3fbe 100644 --- a/concurrency/FastSimulation.java +++ b/concurrency/FastSimulation.java @@ -1,5 +1,6 @@ //: concurrency/FastSimulation.java // ©2015 MindView LLC: see Copyright.txt +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.*; @@ -49,4 +50,4 @@ public class FastSimulation { TimeUnit.SECONDS.sleep(5); exec.shutdownNow(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/FixedDiningPhilosophers.java b/concurrency/FixedDiningPhilosophers.java index 73c060a1..a5909d50 100644 --- a/concurrency/FixedDiningPhilosophers.java +++ b/concurrency/FixedDiningPhilosophers.java @@ -2,6 +2,7 @@ // ©2015 MindView LLC: see Copyright.txt // Dining philosophers without deadlock. // {Args: 5 5 timeout} +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; public class FixedDiningPhilosophers { @@ -31,4 +32,4 @@ public class FixedDiningPhilosophers { } exec.shutdownNow(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/GreenhouseScheduler.java b/concurrency/GreenhouseScheduler.java index 7acf877a..2d7d7f19 100644 --- a/concurrency/GreenhouseScheduler.java +++ b/concurrency/GreenhouseScheduler.java @@ -3,6 +3,7 @@ // Rewriting innerclasses/GreenhouseController.java // to use a ScheduledThreadPoolExecutor. // {Args: 5000} +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; import java.util.*; @@ -163,4 +164,4 @@ public class GreenhouseScheduler { gh.repeat(gh.new ThermostatDay(), 0, 1400); gh.repeat(gh.new CollectData(), 500, 500); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/HorseRace.java b/concurrency/HorseRace.java index f8f46049..0a0e2c36 100644 --- a/concurrency/HorseRace.java +++ b/concurrency/HorseRace.java @@ -1,7 +1,7 @@ //: concurrency/HorseRace.java // ©2015 MindView LLC: see Copyright.txt // Using CyclicBarriers. -// {ValidateByHand} +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; import java.util.*; import static net.mindview.util.Print.*; @@ -86,4 +86,4 @@ public class HorseRace { } new HorseRace(nHorses, pause); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/PriorityBlockingQueueDemo.java b/concurrency/PriorityBlockingQueueDemo.java index 6756e87a..4e405874 100644 --- a/concurrency/PriorityBlockingQueueDemo.java +++ b/concurrency/PriorityBlockingQueueDemo.java @@ -1,5 +1,6 @@ //: concurrency/PriorityBlockingQueueDemo.java // ©2015 MindView LLC: see Copyright.txt +// {OutputFirstAndLast: 12 Lines} import java.util.concurrent.*; import java.util.*; import static net.mindview.util.Print.*; @@ -121,4 +122,4 @@ public class PriorityBlockingQueueDemo { exec.execute(new PrioritizedTaskProducer(queue, exec)); exec.execute(new PrioritizedTaskConsumer(queue)); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/ReaderWriterList.java b/concurrency/ReaderWriterList.java index f4ca1424..9b7e2d1d 100644 --- a/concurrency/ReaderWriterList.java +++ b/concurrency/ReaderWriterList.java @@ -1,5 +1,6 @@ //: concurrency/ReaderWriterList.java // ©2015 MindView LLC: see Copyright.txt +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; import java.util.concurrent.locks.*; import java.util.*; @@ -83,4 +84,4 @@ class ReaderWriterListTest { for(int i = 0; i < writers; i++) exec.execute(new Writer()); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/SemaphoreDemo.java b/concurrency/SemaphoreDemo.java index b02530eb..fd96f5eb 100644 --- a/concurrency/SemaphoreDemo.java +++ b/concurrency/SemaphoreDemo.java @@ -1,6 +1,7 @@ //: concurrency/SemaphoreDemo.java // ©2015 MindView LLC: see Copyright.txt // Testing the Pool class +// {OutputFirstAndLast: 10 Lines} import java.util.concurrent.*; import java.util.*; import static net.mindview.util.Print.*; @@ -65,4 +66,4 @@ public class SemaphoreDemo { pool.checkIn(f); // Second checkIn ignored exec.shutdown(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/ThreadVariations.java b/concurrency/ThreadVariations.java index 36a8e9d3..566cc575 100644 --- a/concurrency/ThreadVariations.java +++ b/concurrency/ThreadVariations.java @@ -161,4 +161,4 @@ public class ThreadVariations { new InnerRunnable2("InnerRunnable2"); new ThreadMethod("ThreadMethod").runTask(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/ToastOMatic.java b/concurrency/ToastOMatic.java index c9110c66..36efc0fb 100644 --- a/concurrency/ToastOMatic.java +++ b/concurrency/ToastOMatic.java @@ -136,4 +136,4 @@ public class ToastOMatic { TimeUnit.SECONDS.sleep(5); exec.shutdownNow(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/concurrency/build.xml b/concurrency/build.xml index 8f1cfbf5..af619e9b 100644 --- a/concurrency/build.xml +++ b/concurrency/build.xml @@ -33,6 +33,7 @@ + diff --git a/containers/CanonicalMapping.java b/containers/CanonicalMapping.java index e00e48ce..e107b113 100644 --- a/containers/CanonicalMapping.java +++ b/containers/CanonicalMapping.java @@ -1,6 +1,7 @@ //: containers/CanonicalMapping.java // ©2015 MindView LLC: see Copyright.txt // Demonstrates WeakHashMap. +// {NoOutput} import java.util.*; class Element { @@ -47,4 +48,4 @@ public class CanonicalMapping { } System.gc(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/containers/Lists.java b/containers/Lists.java index a12bd658..c6e2f2ec 100644 --- a/containers/Lists.java +++ b/containers/Lists.java @@ -1,6 +1,7 @@ //: containers/Lists.java // ©2015 MindView LLC: see Copyright.txt // Things you can do with Lists. +// {OutputFirstAndLast: 10 Lines} import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; @@ -123,4 +124,4 @@ public class Lists { new LinkedList<>(Countries.names(25))); testLinkedList(); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/containers/References.java b/containers/References.java index 5d673501..bde7daac 100644 --- a/containers/References.java +++ b/containers/References.java @@ -1,6 +1,7 @@ //: containers/References.java // ©2015 MindView LLC: see Copyright.txt // Demonstrates Reference objects +// {OutputFirstAndLast: 10 Lines} import java.lang.ref.*; import java.util.*; @@ -60,4 +61,4 @@ public class References { checkQueue(); } } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/control/WhileTest.java b/control/WhileTest.java index 7cb433bf..f5568e75 100644 --- a/control/WhileTest.java +++ b/control/WhileTest.java @@ -1,6 +1,7 @@ //: control/WhileTest.java // ©2015 MindView LLC: see Copyright.txt // Demonstrates the while loop. +// {OutputFirstAndLast: 5 Lines} public class WhileTest { static boolean condition() { @@ -13,4 +14,4 @@ public class WhileTest { System.out.println("Inside 'while'"); System.out.println("Exited 'while'"); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/enums/ConstantSpecificMethod.java b/enums/ConstantSpecificMethod.java index e855173b..40fb92a9 100644 --- a/enums/ConstantSpecificMethod.java +++ b/enums/ConstantSpecificMethod.java @@ -28,4 +28,4 @@ public enum ConstantSpecificMethod { for(ConstantSpecificMethod csm : values()) System.out.println(csm.getInfo()); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/generics/Apply.java b/generics/Apply.java index 22967f61..d7a597be 100644 --- a/generics/Apply.java +++ b/generics/Apply.java @@ -1,6 +1,7 @@ //: generics/Apply.java // ©2015 MindView LLC: see Copyright.txt // {main: ApplyTest} +// {OutputFirstAndLast: 10 Lines} import java.lang.reflect.*; import java.util.*; import static net.mindview.util.Print.*; @@ -69,4 +70,4 @@ class ApplyTest { } Apply.apply(shapeQ, Shape.class.getMethod("rotate")); } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/gui/LookAndFeel.java b/gui/LookAndFeel.java index 9b61dd0a..93386aec 100644 --- a/gui/LookAndFeel.java +++ b/gui/LookAndFeel.java @@ -4,11 +4,13 @@ // {Args: motif} import javax.swing.*; import java.awt.*; +import java.util.*; import static net.mindview.util.SwingConsole.*; public class LookAndFeel extends JFrame { private String[] choices = - "Eeny Meeny Minnie Mickey Moe Larry Curly".split(" "); + "Eeny Meeny Minnie Mickey Moe Larry Curly" + .split(" "); private Component[] samples = { new JButton("JButton"), new JTextField("JTextField"), @@ -18,55 +20,66 @@ public class LookAndFeel extends JFrame { new JComboBox<>(choices), new JList<>(choices), }; + private static + Map optionMap = new HashMap<>(); + private static String options = ""; public LookAndFeel() { super("Look And Feel"); setLayout(new FlowLayout()); for(Component component : samples) add(component); } + public static void initOptions() { + if(optionMap.isEmpty()) { + UIManager.LookAndFeelInfo[] lafi = + UIManager.getInstalledLookAndFeels(); + for(UIManager.LookAndFeelInfo lf : lafi) { + String classname = lf.getClassName(); + String[] parts = classname.split("\\."); + String option = parts[parts.length - 2]; + optionMap.put(option, classname); + } + for(String option : optionMap.keySet()) + options += option + " | "; + options = + options.substring(0, options.length() - 3); + options = "cross | system | " + options; + } + } private static void usageError() { System.out.println( - "Usage:LookAndFeel [cross|system|motif]"); + "Usage:LookAndFeel [ " + options + " ]"); System.exit(1); } public static void main(String[] args) { + initOptions(); if(args.length == 0) usageError(); - switch(args[0]) { - case "cross": - try { + try { + switch(args[0]) { + case "cross": UIManager.setLookAndFeel(UIManager. getCrossPlatformLookAndFeelClassName()); - } catch(ClassNotFoundException | - InstantiationException | - IllegalAccessException | - UnsupportedLookAndFeelException e) { - e.printStackTrace(); - } - break; + break; case "system": - try { UIManager.setLookAndFeel(UIManager. getSystemLookAndFeelClassName()); - } catch(ClassNotFoundException | - InstantiationException | - IllegalAccessException | - UnsupportedLookAndFeelException e) { - e.printStackTrace(); - } - break; + break; case "motif": - try { - UIManager.setLookAndFeel("com.sun.java."+ - "swing.plaf.motif.MotifLookAndFeel"); - } catch(ClassNotFoundException | - InstantiationException | - IllegalAccessException | - UnsupportedLookAndFeelException e) { - e.printStackTrace(); - } + UIManager.setLookAndFeel("com.sun.java."+ + "swing.plaf.motif.MotifLookAndFeel"); break; default: - usageError(); + if(optionMap.containsKey(args[0])) + UIManager.setLookAndFeel( + optionMap.get(args[0])); + else + usageError(); + } + } catch(ClassNotFoundException | + InstantiationException | + IllegalAccessException | + UnsupportedLookAndFeelException e) { + usageError(); } // Note the look & feel must be set before // any components are created. diff --git a/holding/ApplesAndOrangesWithoutGenerics.java b/holding/ApplesAndOrangesWithoutGenerics.java index cbb02f20..8f6a2a80 100644 --- a/holding/ApplesAndOrangesWithoutGenerics.java +++ b/holding/ApplesAndOrangesWithoutGenerics.java @@ -1,6 +1,6 @@ //: holding/ApplesAndOrangesWithoutGenerics.java // ©2015 MindView LLC: see Copyright.txt -// Simple container example (produces compiler warnings). +// Simple container use (suppressing compiler warnings) // {ThrowsException} import java.util.*; @@ -25,4 +25,4 @@ public class ApplesAndOrangesWithoutGenerics { // Orange is detected only at run time } } -} /* (Execute to see output) *///:~ +} ///:~ diff --git a/io/BufferedInputFile.java b/io/BufferedInputFile.java index 2b45034a..b4f6ee12 100644 --- a/io/BufferedInputFile.java +++ b/io/BufferedInputFile.java @@ -7,9 +7,9 @@ public class BufferedInputFile { public static String read(String filename) throws IOException { StringBuilder sb; - try ( // Reading input by lines: - BufferedReader in = new BufferedReader( - new FileReader(filename))) { + // Reading input by lines: + try(BufferedReader in = new BufferedReader( + new FileReader(filename))) { String s; sb = new StringBuilder(); while((s = in.readLine())!= null) diff --git a/io/FileOutputShortcut.java b/io/FileOutputShortcut.java index e47bd39d..9cf5b73e 100644 --- a/io/FileOutputShortcut.java +++ b/io/FileOutputShortcut.java @@ -7,10 +7,10 @@ public class FileOutputShortcut { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( - new StringReader( - BufferedInputFile.read("FileOutputShortcut.java"))); - try ( // Here's the shortcut: - PrintWriter out = new PrintWriter(file)) { + new StringReader(BufferedInputFile.read( + "FileOutputShortcut.java"))); + // Here's the shortcut: + try(PrintWriter out = new PrintWriter(file)) { int lineCount = 1; String s; while((s = in.readLine()) != null ) diff --git a/network/ChatterClient.java b/network/ChatterClient.java index a32ddeaf..001dbce6 100644 --- a/network/ChatterClient.java +++ b/network/ChatterClient.java @@ -28,8 +28,7 @@ public class ChatterClient extends Thread { System.exit(1); } catch(SocketException e) { System.err.println("Can't open socket"); - e.printStackTrace(); - System.exit(1); + throw new RuntimeException(e); } System.out.println("ChatterClient starting"); } @@ -49,8 +48,7 @@ public class ChatterClient extends Thread { Dgram.toString(dp); System.out.println(rcvd); } catch(IOException e) { - e.printStackTrace(); - System.exit(1); + throw new RuntimeException(e); } } @Override diff --git a/network/ChatterServer.java b/network/ChatterServer.java index 6edbe52e..b6e6adee 100644 --- a/network/ChatterServer.java +++ b/network/ChatterServer.java @@ -40,7 +40,7 @@ public class ChatterServer { System.exit(1); } catch(IOException e) { System.out.println("Communication error"); - e.printStackTrace(); + throw new RuntimeException(e); } } public static void main(String[] args) { diff --git a/patterns/ShapeFactory2.java b/patterns/ShapeFactory2.java index d1fa3df0..32913af9 100644 --- a/patterns/ShapeFactory2.java +++ b/patterns/ShapeFactory2.java @@ -64,8 +64,7 @@ public class ShapeFactory2 { ShapeFactory.createShape(shlist1)); } } catch(BadShapeCreation e) { - e.printStackTrace(); - return; + throw new RuntimeException(e); } shapes.forEach(Shape::draw); shapes.forEach(Shape::erase); diff --git a/patterns/factory/ShapeFactory1.java b/patterns/factory/ShapeFactory1.java index efd05a34..67027e03 100644 --- a/patterns/factory/ShapeFactory1.java +++ b/patterns/factory/ShapeFactory1.java @@ -51,8 +51,7 @@ public class ShapeFactory1 { shapes.add(Shape.factory(shlist1)); } } catch(BadShapeCreation e) { - e.printStackTrace(); - return; + throw new RuntimeException(e); } shapes.forEach(Shape::draw); shapes.forEach(Shape::erase); diff --git a/patterns/trash/ParseTrash.java b/patterns/trash/ParseTrash.java index 81cb85e5..08d64799 100644 --- a/patterns/trash/ParseTrash.java +++ b/patterns/trash/ParseTrash.java @@ -29,7 +29,7 @@ public class ParseTrash { NumberFormatException | Trash.PrototypeNotFoundException | Trash.CannotCreateTrashException e) { - e.printStackTrace(); + throw new RuntimeException(e); } } // Special case to handle ArrayList: diff --git a/references/DeepCopy.java b/references/DeepCopy.java index 5615ba42..7eeefc57 100644 --- a/references/DeepCopy.java +++ b/references/DeepCopy.java @@ -16,7 +16,7 @@ class DepthReading implements Cloneable { try { o = super.clone(); } catch(CloneNotSupportedException e) { - e.printStackTrace(); + throw new RuntimeException(e); } return o; } @@ -43,7 +43,7 @@ class TemperatureReading implements Cloneable { try { o = super.clone(); } catch(CloneNotSupportedException e) { - e.printStackTrace(); + throw new RuntimeException(e); } return o; } @@ -73,7 +73,7 @@ class OceanReading implements Cloneable { try { o = (OceanReading)super.clone(); } catch(CloneNotSupportedException e) { - e.printStackTrace(); + throw new RuntimeException(e); } // Must clone references: o.depth = (DepthReading)o.depth.clone(); diff --git a/typeinfo/PetCount2.java b/typeinfo/PetCount2.java index f6d23677..42c21870 100644 --- a/typeinfo/PetCount2.java +++ b/typeinfo/PetCount2.java @@ -6,4 +6,4 @@ public class PetCount2 { public static void main(String[] args) { PetCount.countPets(Pets.creator); } -} /* (Execute to see output) *///:~ +} ///:~