From ede3954d8680e09467b1be5baf96fa1ee2989cc5 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Thu, 4 Mar 2021 16:15:04 -0700 Subject: [PATCH] March 2021 Book Update See notes in "Foreword to the Leanpub Edition" --- collections/AdapterMethodIdiom.java | 2 +- collections/CollectionSequence.java | 2 +- collections/CrossCollectionIteration.java | 2 +- collections/CrossCollectionIteration2.java | 2 +- collections/InterfaceVsIterator.java | 2 +- collections/LinkedListFeatures.java | 2 +- collections/ListFeatures.java | 2 +- collections/ListIteration.java | 2 +- collections/MapOfList.java | 2 +- collections/MultiIterableClass.java | 2 +- collections/NonCollectionSequence.java | 2 +- collections/PetMap.java | 2 +- collections/SimpleIteration.java | 2 +- collectiontopics/CollectionMethods.java | 3 +- concurrent/PSP2.txt | 518 +++++++++++++++++- enums/PostOffice.java | 22 +- enums/menu/Food.java | 2 +- enums/menu/Meal2.java | 2 +- equalshashcode/IndividualTest.java | 2 +- generics/CheckedList.java | 6 +- generics/DogsAndRobotMethodReferences.java | 2 +- generics/DogsAndRobots.java | 2 +- generics/GenericHolder.java | 2 +- gradle/subprojects.gradle | 6 +- housekeeping/Flower.java | 4 +- housekeeping/OverloadingOrder.java | 2 +- housekeeping/SimpleConstructor2.java | 2 +- javadoc/Documentation3.java | 2 + onjava/BasicSupplier.java | 2 +- onjava/Countries.java | 2 +- onjava/MouseClick.java | 2 +- onjava/atunit/AtUnit.java | 6 +- patterns/BoxObserver.java | 77 ++- patterns/CleanTheFloor.java | 42 ++ patterns/CommandPattern.java | 21 +- patterns/Macro.java | 25 + patterns/PaperScissorsRock.java | 2 +- patterns/ProxyDemo.java | 8 +- patterns/Resource.java | 9 + patterns/ShapeFactory1.java | 2 +- patterns/ShapeFactory2.java | 4 +- patterns/ShapeFactory3.java | 9 +- patterns/Single.java | 18 + patterns/SingletonPattern.java | 75 ++- patterns/SingletonPattern2.java | 16 + patterns/SingletonPattern3.java | 33 ++ patterns/StateDemo.java | 51 +- patterns/TemplateMethod.java | 28 +- patterns/TestSingle.java | 20 + patterns/TypeMap.java | 21 + patterns/abstractfactory/GameEnvironment.java | 24 +- patterns/adapt/Adapter.java | 2 +- patterns/chain/ChainOfResponsibility.java | 53 +- patterns/chain/Result.java | 21 + patterns/doubledispatch/Aluminum.java | 4 +- patterns/doubledispatch/Cardboard.java | 4 +- patterns/doubledispatch/DoubleDispatch.java | 86 +-- patterns/doubledispatch/Glass.java | 4 +- patterns/doubledispatch/Paper.java | 4 +- patterns/doubledispatch/TypedBin.java | 15 +- patterns/doubledispatch/TypedBinMember.java | 7 +- patterns/dynatrash/DynaTrash.java | 74 --- patterns/observer/ObservedFlower.java | 130 ++--- patterns/recyclea/RecycleA.java | 140 ++--- patterns/recycleb/RecycleB.java | 72 ++- patterns/recyclec/RecycleC.java | 126 +++-- patterns/state/StateMachineDemo.java | 26 +- patterns/strategy/StrategyPattern.java | 12 +- patterns/strategy/StrategyPattern2.java | 4 + patterns/trash/Aluminum.java | 10 +- patterns/trash/Bins.java | 35 ++ patterns/trash/Cardboard.java | 10 +- patterns/trash/ClassToListOfTrashMap.java | 20 + patterns/trash/DynaFactory.java | 44 ++ patterns/trash/Fillable.java | 2 +- patterns/trash/FillableList.java | 12 +- patterns/trash/Glass.java | 10 +- patterns/trash/GroupingBy.java | 41 ++ patterns/trash/Paper.java | 10 +- patterns/trash/ParseTrash.java | 89 ++- patterns/trash/Price.java | 13 + patterns/trash/Trash.dat | 46 +- patterns/trash/Trash.java | 91 +-- patterns/trash/TrashInfo.java | 18 + patterns/trash/TrashValue.java | 21 + patterns/trash/TrashVisitor.java | 104 ++++ patterns/trash/TypeMapTrash.java | 52 ++ patterns/trash/Visitor.java | 31 ++ patterns/trashvisitor/Aluminum.java | 15 - patterns/trashvisitor/Cardboard.java | 15 - patterns/trashvisitor/Glass.java | 15 - patterns/trashvisitor/Paper.java | 15 - patterns/trashvisitor/TrashVisitor.java | 117 ---- patterns/trashvisitor/Visitable.java | 12 - patterns/trashvisitor/Visitor.java | 14 - patterns/visitor/BeeAndFlowers.java | 30 +- polymorphism/{RTTI.java => Reflect.java} | 9 +- references/CopyConstructor.java | 8 +- .../AnonymousImplementation.java | 4 +- .../BoundedClassReferences.java | 2 +- {typeinfo => reflection}/ClassCasts.java | 2 +- .../ClassInitialization.java | 2 +- {typeinfo => reflection}/DynamicSupplier.java | 2 +- .../FamilyVsExactType.java | 10 +- .../GenericClassReferences.java | 2 +- .../HiddenImplementation.java | 8 +- {typeinfo => reflection}/ID2.java | 2 +- .../InnerImplementation.java | 6 +- .../InterfaceViolation.java | 4 +- .../ModifyingPrivateFields.java | 2 +- {typeinfo => reflection}/NullRobot.java | 2 +- {typeinfo => reflection}/Operation.java | 2 +- {typeinfo => reflection}/Person.java | 2 +- {typeinfo => reflection}/PetCounter.java | 4 +- {typeinfo => reflection}/PetCounter2.java | 4 +- {typeinfo => reflection}/PetCounter3.java | 4 +- {typeinfo => reflection}/PetCounter4.java | 4 +- {typeinfo => reflection}/Position.java | 2 +- .../RegisteredFactories.java | 2 +- {typeinfo => reflection}/Robot.java | 2 +- .../SelectingMethods.java | 2 +- {typeinfo => reflection}/Shapes.java | 2 +- {typeinfo => reflection}/ShowMethods.java | 2 +- .../SimpleDynamicProxy.java | 2 +- {typeinfo => reflection}/SimpleProxyDemo.java | 2 +- {typeinfo => reflection}/SnowRobot.java | 2 +- {typeinfo => reflection}/Staff.java | 2 +- {typeinfo => reflection}/SweetShop.java | 2 +- .../WildcardClassReferences.java | 2 +- {typeinfo => reflection}/interfacea/A.java | 4 +- .../packageaccess/HiddenC.java | 6 +- {typeinfo => reflection}/pets/Cat.java | 4 +- {typeinfo => reflection}/pets/Creator.java | 4 +- {typeinfo => reflection}/pets/Cymric.java | 4 +- {typeinfo => reflection}/pets/Dog.java | 4 +- .../pets/EgyptianMau.java | 4 +- .../pets/ForNamePetCreator.java | 20 +- {typeinfo => reflection}/pets/Hamster.java | 4 +- {typeinfo => reflection}/pets/Individual.java | 4 +- {typeinfo => reflection}/pets/Manx.java | 4 +- {typeinfo => reflection}/pets/Mouse.java | 4 +- {typeinfo => reflection}/pets/Mutt.java | 4 +- {typeinfo => reflection}/pets/Person.java | 4 +- {typeinfo => reflection}/pets/Pet.java | 4 +- {typeinfo => reflection}/pets/PetCreator.java | 16 +- {typeinfo => reflection}/pets/Pug.java | 4 +- {typeinfo => reflection}/pets/Rat.java | 4 +- {typeinfo => reflection}/pets/Rodent.java | 4 +- .../toys/GenericToyTest.java | 6 +- {typeinfo => reflection}/toys/ToyTest.java | 32 +- reuse/Bath.java | 8 +- serialization/AStoreCADState.java | 22 +- serialization/Blip3.java | 2 +- serialization/RecoverCADState.java | 20 +- serialization/Worm.java | 2 +- 155 files changed, 1861 insertions(+), 1222 deletions(-) create mode 100644 patterns/CleanTheFloor.java create mode 100644 patterns/Macro.java create mode 100644 patterns/Resource.java create mode 100644 patterns/Single.java create mode 100644 patterns/SingletonPattern2.java create mode 100644 patterns/SingletonPattern3.java create mode 100644 patterns/TestSingle.java create mode 100644 patterns/TypeMap.java create mode 100644 patterns/chain/Result.java delete mode 100644 patterns/dynatrash/DynaTrash.java create mode 100644 patterns/trash/Bins.java create mode 100644 patterns/trash/ClassToListOfTrashMap.java create mode 100644 patterns/trash/DynaFactory.java create mode 100644 patterns/trash/GroupingBy.java create mode 100644 patterns/trash/Price.java create mode 100644 patterns/trash/TrashInfo.java create mode 100644 patterns/trash/TrashValue.java create mode 100644 patterns/trash/TrashVisitor.java create mode 100644 patterns/trash/TypeMapTrash.java create mode 100644 patterns/trash/Visitor.java delete mode 100644 patterns/trashvisitor/Aluminum.java delete mode 100644 patterns/trashvisitor/Cardboard.java delete mode 100644 patterns/trashvisitor/Glass.java delete mode 100644 patterns/trashvisitor/Paper.java delete mode 100644 patterns/trashvisitor/TrashVisitor.java delete mode 100644 patterns/trashvisitor/Visitable.java delete mode 100644 patterns/trashvisitor/Visitor.java rename polymorphism/{RTTI.java => Reflect.java} (81%) rename {typeinfo => reflection}/AnonymousImplementation.java (94%) rename {typeinfo => reflection}/BoundedClassReferences.java (90%) rename {typeinfo => reflection}/ClassCasts.java (93%) rename {typeinfo => reflection}/ClassInitialization.java (97%) rename {typeinfo => reflection}/DynamicSupplier.java (96%) rename {typeinfo => reflection}/FamilyVsExactType.java (90%) rename {typeinfo => reflection}/GenericClassReferences.java (91%) rename {typeinfo => reflection}/HiddenImplementation.java (88%) rename {typeinfo => reflection}/ID2.java (96%) rename {typeinfo => reflection}/InnerImplementation.java (90%) rename {typeinfo => reflection}/InterfaceViolation.java (88%) rename {typeinfo => reflection}/ModifyingPrivateFields.java (97%) rename {typeinfo => reflection}/NullRobot.java (98%) rename {typeinfo => reflection}/Operation.java (93%) rename {typeinfo => reflection}/Person.java (98%) rename {typeinfo => reflection}/PetCounter.java (97%) rename {typeinfo => reflection}/PetCounter2.java (90%) rename {typeinfo => reflection}/PetCounter3.java (96%) rename {typeinfo => reflection}/PetCounter4.java (92%) rename {typeinfo => reflection}/Position.java (98%) rename {typeinfo => reflection}/RegisteredFactories.java (98%) rename {typeinfo => reflection}/Robot.java (96%) rename {typeinfo => reflection}/SelectingMethods.java (97%) rename {typeinfo => reflection}/Shapes.java (96%) rename {typeinfo => reflection}/ShowMethods.java (98%) rename {typeinfo => reflection}/SimpleDynamicProxy.java (97%) rename {typeinfo => reflection}/SimpleProxyDemo.java (97%) rename {typeinfo => reflection}/SnowRobot.java (97%) rename {typeinfo => reflection}/Staff.java (98%) rename {typeinfo => reflection}/SweetShop.java (97%) rename {typeinfo => reflection}/WildcardClassReferences.java (87%) rename {typeinfo => reflection}/interfacea/A.java (75%) rename {typeinfo => reflection}/packageaccess/HiddenC.java (84%) rename {typeinfo => reflection}/pets/Cat.java (83%) rename {typeinfo => reflection}/pets/Creator.java (95%) rename {typeinfo => reflection}/pets/Cymric.java (83%) rename {typeinfo => reflection}/pets/Dog.java (83%) rename {typeinfo => reflection}/pets/EgyptianMau.java (82%) rename {typeinfo => reflection}/pets/ForNamePetCreator.java (71%) rename {typeinfo => reflection}/pets/Hamster.java (83%) rename {typeinfo => reflection}/pets/Individual.java (95%) rename {typeinfo => reflection}/pets/Manx.java (83%) rename {typeinfo => reflection}/pets/Mouse.java (83%) rename {typeinfo => reflection}/pets/Mutt.java (83%) rename {typeinfo => reflection}/pets/Person.java (81%) rename {typeinfo => reflection}/pets/Pet.java (83%) rename {typeinfo => reflection}/pets/PetCreator.java (75%) rename {typeinfo => reflection}/pets/Pug.java (83%) rename {typeinfo => reflection}/pets/Rat.java (83%) rename {typeinfo => reflection}/pets/Rodent.java (83%) rename {typeinfo => reflection}/toys/GenericToyTest.java (85%) rename {typeinfo => reflection}/toys/ToyTest.java (67%) diff --git a/collections/AdapterMethodIdiom.java b/collections/AdapterMethodIdiom.java index 47fe3e8b..ffb8fbf5 100644 --- a/collections/AdapterMethodIdiom.java +++ b/collections/AdapterMethodIdiom.java @@ -33,7 +33,7 @@ class ReversibleArrayList extends ArrayList { public class AdapterMethodIdiom { public static void main(String[] args) { ReversibleArrayList ral = - new ReversibleArrayList( + new ReversibleArrayList<>( Arrays.asList("To be or not to be".split(" "))); // Grabs the ordinary iterator via iterator(): for(String s : ral) diff --git a/collections/CollectionSequence.java b/collections/CollectionSequence.java index 65f93254..ea0e4574 100644 --- a/collections/CollectionSequence.java +++ b/collections/CollectionSequence.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class CollectionSequence diff --git a/collections/CrossCollectionIteration.java b/collections/CrossCollectionIteration.java index 6be88766..2aaf7e95 100644 --- a/collections/CrossCollectionIteration.java +++ b/collections/CrossCollectionIteration.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class CrossCollectionIteration { diff --git a/collections/CrossCollectionIteration2.java b/collections/CrossCollectionIteration2.java index de7fe280..e8aa755d 100644 --- a/collections/CrossCollectionIteration2.java +++ b/collections/CrossCollectionIteration2.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class CrossCollectionIteration2 { diff --git a/collections/InterfaceVsIterator.java b/collections/InterfaceVsIterator.java index 5e3c147b..fdb50a58 100644 --- a/collections/InterfaceVsIterator.java +++ b/collections/InterfaceVsIterator.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class InterfaceVsIterator { diff --git a/collections/LinkedListFeatures.java b/collections/LinkedListFeatures.java index 3552fa10..489c699f 100644 --- a/collections/LinkedListFeatures.java +++ b/collections/LinkedListFeatures.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class LinkedListFeatures { diff --git a/collections/ListFeatures.java b/collections/ListFeatures.java index cf1e7b01..12fb6b4f 100644 --- a/collections/ListFeatures.java +++ b/collections/ListFeatures.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class ListFeatures { diff --git a/collections/ListIteration.java b/collections/ListIteration.java index ce49b529..4967f526 100644 --- a/collections/ListIteration.java +++ b/collections/ListIteration.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class ListIteration { diff --git a/collections/MapOfList.java b/collections/MapOfList.java index d3b385ad..d7357a6e 100644 --- a/collections/MapOfList.java +++ b/collections/MapOfList.java @@ -4,7 +4,7 @@ // Visit http://OnJava8.com for more book information. // {java collections.MapOfList} package collections; -import typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class MapOfList { diff --git a/collections/MultiIterableClass.java b/collections/MultiIterableClass.java index e55d811e..3c5e7ae2 100644 --- a/collections/MultiIterableClass.java +++ b/collections/MultiIterableClass.java @@ -29,7 +29,7 @@ public class MultiIterableClass extends IterableClass { return new Iterable() { public Iterator iterator() { List shuffled = - new ArrayList(Arrays.asList(words)); + new ArrayList<>(Arrays.asList(words)); Collections.shuffle(shuffled, new Random(47)); return shuffled.iterator(); } diff --git a/collections/NonCollectionSequence.java b/collections/NonCollectionSequence.java index 8a6c8bb5..dc867827 100644 --- a/collections/NonCollectionSequence.java +++ b/collections/NonCollectionSequence.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; class PetSequence { diff --git a/collections/PetMap.java b/collections/PetMap.java index 44885c80..dc64bf57 100644 --- a/collections/PetMap.java +++ b/collections/PetMap.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class PetMap { diff --git a/collections/SimpleIteration.java b/collections/SimpleIteration.java index 234a716b..08977496 100644 --- a/collections/SimpleIteration.java +++ b/collections/SimpleIteration.java @@ -2,7 +2,7 @@ // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class SimpleIteration { diff --git a/collectiontopics/CollectionMethods.java b/collectiontopics/CollectionMethods.java index 21c12bb5..08660e62 100644 --- a/collectiontopics/CollectionMethods.java +++ b/collectiontopics/CollectionMethods.java @@ -56,8 +56,7 @@ public class CollectionMethods { // c2 and c3 (an intersection of sets): c2.retainAll(c3); show(c2); - // Throw away all the elements - // in c2 that also appear in c3: + // Discard all c2 elements that also appear in c3: c2.removeAll(c3); System.out.println( "c2.isEmpty() = " + c2.isEmpty()); diff --git a/concurrent/PSP2.txt b/concurrent/PSP2.txt index 491bd153..f9ed8361 100644 --- a/concurrent/PSP2.txt +++ b/concurrent/PSP2.txt @@ -1,12 +1,512 @@ +0: ForkJoinPool.commonPool-worker-1 0: main -1: main -1: ForkJoinPool.commonPool-worker-3 -1: ForkJoinPool.commonPool-worker-5 -3: ForkJoinPool.commonPool-worker-5 +2: main +2: ForkJoinPool.commonPool-worker-1 3: main -5: ForkJoinPool.commonPool-worker-5 +4: ForkJoinPool.commonPool-worker-1 5: main -7: ForkJoinPool.commonPool-worker-5 -8: main -8: ForkJoinPool.commonPool-worker-3 -10: ForkJoinPool.commonPool-worker-5 +6: ForkJoinPool.commonPool-worker-1 +7: main +9: ForkJoinPool.commonPool-worker-1 +9: main +11: ForkJoinPool.commonPool-worker-1 +11: main +12: ForkJoinPool.commonPool-worker-1 +13: main +15: ForkJoinPool.commonPool-worker-1 +15: main +17: main +18: main +19: main +20: main +21: main +17: ForkJoinPool.commonPool-worker-1 +23: main +23: ForkJoinPool.commonPool-worker-1 +24: main +25: ForkJoinPool.commonPool-worker-1 +26: main +27: ForkJoinPool.commonPool-worker-1 +28: main +29: ForkJoinPool.commonPool-worker-1 +30: main +32: main +32: ForkJoinPool.commonPool-worker-1 +33: main +35: ForkJoinPool.commonPool-worker-1 +36: ForkJoinPool.commonPool-worker-1 +37: ForkJoinPool.commonPool-worker-1 +38: ForkJoinPool.commonPool-worker-1 +35: main +39: ForkJoinPool.commonPool-worker-1 +41: ForkJoinPool.commonPool-worker-1 +40: main +42: ForkJoinPool.commonPool-worker-1 +43: main +44: ForkJoinPool.commonPool-worker-1 +45: main +46: ForkJoinPool.commonPool-worker-1 +47: main +49: main +50: main +50: ForkJoinPool.commonPool-worker-1 +51: main +52: ForkJoinPool.commonPool-worker-1 +53: main +55: ForkJoinPool.commonPool-worker-1 +55: main +56: ForkJoinPool.commonPool-worker-1 +58: ForkJoinPool.commonPool-worker-1 +57: main +59: ForkJoinPool.commonPool-worker-1 +60: main +61: ForkJoinPool.commonPool-worker-1 +62: main +63: ForkJoinPool.commonPool-worker-1 +64: main +65: ForkJoinPool.commonPool-worker-1 +66: main +67: ForkJoinPool.commonPool-worker-1 +68: main +69: ForkJoinPool.commonPool-worker-1 +71: ForkJoinPool.commonPool-worker-1 +72: ForkJoinPool.commonPool-worker-1 +70: main +73: ForkJoinPool.commonPool-worker-1 +75: ForkJoinPool.commonPool-worker-1 +76: ForkJoinPool.commonPool-worker-1 +74: main +77: ForkJoinPool.commonPool-worker-1 +78: ForkJoinPool.commonPool-worker-1 +79: main +80: ForkJoinPool.commonPool-worker-1 +81: main +82: ForkJoinPool.commonPool-worker-1 +83: main +84: ForkJoinPool.commonPool-worker-1 +85: main +86: ForkJoinPool.commonPool-worker-1 +87: main +89: ForkJoinPool.commonPool-worker-1 +90: ForkJoinPool.commonPool-worker-1 +89: main +91: ForkJoinPool.commonPool-worker-1 +92: main +93: ForkJoinPool.commonPool-worker-1 +94: main +95: ForkJoinPool.commonPool-worker-1 +96: main +98: main +97: ForkJoinPool.commonPool-worker-1 +99: main +100: ForkJoinPool.commonPool-worker-1 +101: main +103: ForkJoinPool.commonPool-worker-1 +103: main +104: ForkJoinPool.commonPool-worker-1 +106: ForkJoinPool.commonPool-worker-1 +107: ForkJoinPool.commonPool-worker-1 +106: main +108: ForkJoinPool.commonPool-worker-1 +110: ForkJoinPool.commonPool-worker-1 +108: ForkJoinPool.commonPool-worker-2 +111: ForkJoinPool.commonPool-worker-1 +110: main +113: ForkJoinPool.commonPool-worker-1 +112: ForkJoinPool.commonPool-worker-2 +115: ForkJoinPool.commonPool-worker-1 +116: main +116: ForkJoinPool.commonPool-worker-2 +117: ForkJoinPool.commonPool-worker-1 +118: main +119: ForkJoinPool.commonPool-worker-2 +120: ForkJoinPool.commonPool-worker-1 +121: main +123: ForkJoinPool.commonPool-worker-1 +124: main +124: ForkJoinPool.commonPool-worker-2 +126: main +126: ForkJoinPool.commonPool-worker-1 +127: ForkJoinPool.commonPool-worker-2 +128: main +130: ForkJoinPool.commonPool-worker-1 +130: ForkJoinPool.commonPool-worker-2 +131: main +133: ForkJoinPool.commonPool-worker-1 +134: ForkJoinPool.commonPool-worker-2 +134: main +136: ForkJoinPool.commonPool-worker-1 +137: main +137: ForkJoinPool.commonPool-worker-2 +138: ForkJoinPool.commonPool-worker-1 +140: main +140: ForkJoinPool.commonPool-worker-2 +141: ForkJoinPool.commonPool-worker-1 +143: main +143: ForkJoinPool.commonPool-worker-2 +145: ForkJoinPool.commonPool-worker-1 +146: main +146: ForkJoinPool.commonPool-worker-2 +148: ForkJoinPool.commonPool-worker-1 +149: main +149: ForkJoinPool.commonPool-worker-2 +151: ForkJoinPool.commonPool-worker-1 +152: ForkJoinPool.commonPool-worker-2 +154: ForkJoinPool.commonPool-worker-1 +154: ForkJoinPool.commonPool-worker-2 +152: main +156: ForkJoinPool.commonPool-worker-1 +156: ForkJoinPool.commonPool-worker-2 +159: ForkJoinPool.commonPool-worker-1 +159: ForkJoinPool.commonPool-worker-2 +161: ForkJoinPool.commonPool-worker-1 +159: main +162: ForkJoinPool.commonPool-worker-1 +161: ForkJoinPool.commonPool-worker-2 +164: ForkJoinPool.commonPool-worker-1 +163: main +166: ForkJoinPool.commonPool-worker-1 +168: ForkJoinPool.commonPool-worker-1 +168: main +169: ForkJoinPool.commonPool-worker-1 +170: main +167: ForkJoinPool.commonPool-worker-2 +171: ForkJoinPool.commonPool-worker-1 +172: main +168: ForkJoinPool.commonPool-worker-3 +174: ForkJoinPool.commonPool-worker-1 +175: main +178: ForkJoinPool.commonPool-worker-1 +178: main +178: ForkJoinPool.commonPool-worker-3 +175: ForkJoinPool.commonPool-worker-2 +179: ForkJoinPool.commonPool-worker-1 +181: main +183: ForkJoinPool.commonPool-worker-1 +183: ForkJoinPool.commonPool-worker-2 +184: main +184: ForkJoinPool.commonPool-worker-3 +185: ForkJoinPool.commonPool-worker-1 +187: main +187: ForkJoinPool.commonPool-worker-2 +189: ForkJoinPool.commonPool-worker-1 +191: main +189: ForkJoinPool.commonPool-worker-3 +191: ForkJoinPool.commonPool-worker-2 +193: main +193: ForkJoinPool.commonPool-worker-1 +195: ForkJoinPool.commonPool-worker-3 +195: ForkJoinPool.commonPool-worker-2 +197: main +199: ForkJoinPool.commonPool-worker-3 +199: ForkJoinPool.commonPool-worker-2 +197: ForkJoinPool.commonPool-worker-1 +200: main +202: ForkJoinPool.commonPool-worker-3 +203: ForkJoinPool.commonPool-worker-2 +204: ForkJoinPool.commonPool-worker-1 +204: main +205: ForkJoinPool.commonPool-worker-3 +207: ForkJoinPool.commonPool-worker-2 +208: ForkJoinPool.commonPool-worker-1 +208: main +209: ForkJoinPool.commonPool-worker-3 +211: ForkJoinPool.commonPool-worker-2 +211: ForkJoinPool.commonPool-worker-1 +213: main +213: ForkJoinPool.commonPool-worker-3 +215: ForkJoinPool.commonPool-worker-2 +215: ForkJoinPool.commonPool-worker-1 +217: main +217: ForkJoinPool.commonPool-worker-3 +219: ForkJoinPool.commonPool-worker-2 +220: ForkJoinPool.commonPool-worker-1 +221: main +221: ForkJoinPool.commonPool-worker-3 +224: ForkJoinPool.commonPool-worker-1 +225: main +225: ForkJoinPool.commonPool-worker-3 +223: ForkJoinPool.commonPool-worker-2 +227: ForkJoinPool.commonPool-worker-1 +227: main +229: ForkJoinPool.commonPool-worker-3 +229: ForkJoinPool.commonPool-worker-2 +231: ForkJoinPool.commonPool-worker-1 +231: main +233: ForkJoinPool.commonPool-worker-3 +233: ForkJoinPool.commonPool-worker-2 +235: ForkJoinPool.commonPool-worker-1 +236: main +237: ForkJoinPool.commonPool-worker-3 +237: ForkJoinPool.commonPool-worker-2 +239: ForkJoinPool.commonPool-worker-1 +241: main +241: ForkJoinPool.commonPool-worker-2 +241: ForkJoinPool.commonPool-worker-3 +243: ForkJoinPool.commonPool-worker-1 +244: main +244: ForkJoinPool.commonPool-worker-2 +245: ForkJoinPool.commonPool-worker-3 +247: ForkJoinPool.commonPool-worker-1 +249: ForkJoinPool.commonPool-worker-3 +251: ForkJoinPool.commonPool-worker-3 +250: ForkJoinPool.commonPool-worker-1 +251: ForkJoinPool.commonPool-worker-2 +253: ForkJoinPool.commonPool-worker-3 +254: ForkJoinPool.commonPool-worker-1 +254: ForkJoinPool.commonPool-worker-2 +251: main +255: ForkJoinPool.commonPool-worker-3 +257: ForkJoinPool.commonPool-worker-1 +257: ForkJoinPool.commonPool-worker-2 +259: ForkJoinPool.commonPool-worker-3 +261: ForkJoinPool.commonPool-worker-1 +261: ForkJoinPool.commonPool-worker-2 +262: ForkJoinPool.commonPool-worker-3 +263: ForkJoinPool.commonPool-worker-1 +264: ForkJoinPool.commonPool-worker-2 +265: ForkJoinPool.commonPool-worker-3 +267: ForkJoinPool.commonPool-worker-1 +267: ForkJoinPool.commonPool-worker-2 +261: main +268: ForkJoinPool.commonPool-worker-3 +269: ForkJoinPool.commonPool-worker-1 +270: ForkJoinPool.commonPool-worker-2 +272: ForkJoinPool.commonPool-worker-3 +273: ForkJoinPool.commonPool-worker-1 +276: ForkJoinPool.commonPool-worker-3 +276: ForkJoinPool.commonPool-worker-1 +274: ForkJoinPool.commonPool-worker-2 +277: ForkJoinPool.commonPool-worker-3 +279: ForkJoinPool.commonPool-worker-1 +279: ForkJoinPool.commonPool-worker-2 +275: main +281: ForkJoinPool.commonPool-worker-1 +280: ForkJoinPool.commonPool-worker-3 +282: ForkJoinPool.commonPool-worker-2 +285: ForkJoinPool.commonPool-worker-1 +286: ForkJoinPool.commonPool-worker-3 +286: ForkJoinPool.commonPool-worker-2 +288: ForkJoinPool.commonPool-worker-1 +289: ForkJoinPool.commonPool-worker-3 +289: ForkJoinPool.commonPool-worker-2 +286: main +291: ForkJoinPool.commonPool-worker-1 +292: ForkJoinPool.commonPool-worker-3 +293: ForkJoinPool.commonPool-worker-2 +293: main +296: ForkJoinPool.commonPool-worker-3 +296: ForkJoinPool.commonPool-worker-2 +297: main +297: ForkJoinPool.commonPool-worker-1 +299: ForkJoinPool.commonPool-worker-3 +299: ForkJoinPool.commonPool-worker-2 +301: main +301: ForkJoinPool.commonPool-worker-1 +303: ForkJoinPool.commonPool-worker-3 +303: ForkJoinPool.commonPool-worker-2 +305: main +305: ForkJoinPool.commonPool-worker-1 +307: ForkJoinPool.commonPool-worker-3 +307: ForkJoinPool.commonPool-worker-2 +309: main +309: ForkJoinPool.commonPool-worker-1 +311: ForkJoinPool.commonPool-worker-3 +311: ForkJoinPool.commonPool-worker-2 +313: main +313: ForkJoinPool.commonPool-worker-1 +315: ForkJoinPool.commonPool-worker-3 +315: ForkJoinPool.commonPool-worker-2 +317: ForkJoinPool.commonPool-worker-1 +319: ForkJoinPool.commonPool-worker-3 +319: ForkJoinPool.commonPool-worker-2 +320: ForkJoinPool.commonPool-worker-1 +322: ForkJoinPool.commonPool-worker-3 +317: main +323: ForkJoinPool.commonPool-worker-1 +322: ForkJoinPool.commonPool-worker-2 +324: ForkJoinPool.commonPool-worker-3 +328: main +328: ForkJoinPool.commonPool-worker-3 +328: ForkJoinPool.commonPool-worker-2 +330: ForkJoinPool.commonPool-worker-3 +329: main +331: ForkJoinPool.commonPool-worker-2 +332: ForkJoinPool.commonPool-worker-3 +334: ForkJoinPool.commonPool-worker-2 +335: main +335: ForkJoinPool.commonPool-worker-3 +337: ForkJoinPool.commonPool-worker-2 +338: main +338: ForkJoinPool.commonPool-worker-3 +340: ForkJoinPool.commonPool-worker-2 +341: main +341: ForkJoinPool.commonPool-worker-3 +343: ForkJoinPool.commonPool-worker-2 +343: main +344: ForkJoinPool.commonPool-worker-3 +346: ForkJoinPool.commonPool-worker-2 +347: main +347: ForkJoinPool.commonPool-worker-3 +349: ForkJoinPool.commonPool-worker-2 +350: main +350: ForkJoinPool.commonPool-worker-3 +351: ForkJoinPool.commonPool-worker-2 +353: main +353: ForkJoinPool.commonPool-worker-3 +355: ForkJoinPool.commonPool-worker-2 +356: main +356: ForkJoinPool.commonPool-worker-3 +358: ForkJoinPool.commonPool-worker-2 +359: main +359: ForkJoinPool.commonPool-worker-3 +361: ForkJoinPool.commonPool-worker-2 +362: main +362: ForkJoinPool.commonPool-worker-3 +365: main +364: ForkJoinPool.commonPool-worker-2 +365: ForkJoinPool.commonPool-worker-3 +366: main +368: ForkJoinPool.commonPool-worker-2 +369: main +369: ForkJoinPool.commonPool-worker-3 +372: ForkJoinPool.commonPool-worker-3 +371: ForkJoinPool.commonPool-worker-2 +372: main +374: ForkJoinPool.commonPool-worker-3 +374: ForkJoinPool.commonPool-worker-2 +376: ForkJoinPool.commonPool-worker-3 +377: ForkJoinPool.commonPool-worker-2 +378: ForkJoinPool.commonPool-worker-3 +376: main +379: ForkJoinPool.commonPool-worker-2 +380: ForkJoinPool.commonPool-worker-3 +382: ForkJoinPool.commonPool-worker-2 +383: ForkJoinPool.commonPool-worker-3 +384: ForkJoinPool.commonPool-worker-2 +385: ForkJoinPool.commonPool-worker-3 +383: main +386: ForkJoinPool.commonPool-worker-2 +387: ForkJoinPool.commonPool-worker-3 +390: ForkJoinPool.commonPool-worker-2 +390: ForkJoinPool.commonPool-worker-3 +392: ForkJoinPool.commonPool-worker-2 +392: ForkJoinPool.commonPool-worker-3 +390: main +394: ForkJoinPool.commonPool-worker-2 +394: ForkJoinPool.commonPool-worker-3 +397: ForkJoinPool.commonPool-worker-2 +397: ForkJoinPool.commonPool-worker-3 +399: ForkJoinPool.commonPool-worker-2 +397: main +400: ForkJoinPool.commonPool-worker-3 +401: ForkJoinPool.commonPool-worker-3 +401: ForkJoinPool.commonPool-worker-2 +404: ForkJoinPool.commonPool-worker-3 +404: ForkJoinPool.commonPool-worker-2 +403: main +406: ForkJoinPool.commonPool-worker-3 +406: ForkJoinPool.commonPool-worker-2 +408: main +409: ForkJoinPool.commonPool-worker-3 +409: ForkJoinPool.commonPool-worker-2 +411: main +412: ForkJoinPool.commonPool-worker-3 +412: ForkJoinPool.commonPool-worker-2 +414: main +414: ForkJoinPool.commonPool-worker-3 +415: ForkJoinPool.commonPool-worker-2 +418: ForkJoinPool.commonPool-worker-3 +418: ForkJoinPool.commonPool-worker-2 +419: ForkJoinPool.commonPool-worker-3 +420: ForkJoinPool.commonPool-worker-2 +421: ForkJoinPool.commonPool-worker-3 +422: ForkJoinPool.commonPool-worker-2 +423: ForkJoinPool.commonPool-worker-3 +424: ForkJoinPool.commonPool-worker-2 +425: ForkJoinPool.commonPool-worker-3 +426: ForkJoinPool.commonPool-worker-2 +427: ForkJoinPool.commonPool-worker-3 +428: ForkJoinPool.commonPool-worker-2 +430: ForkJoinPool.commonPool-worker-2 +430: ForkJoinPool.commonPool-worker-3 +432: ForkJoinPool.commonPool-worker-3 +432: ForkJoinPool.commonPool-worker-2 +433: ForkJoinPool.commonPool-worker-3 +434: ForkJoinPool.commonPool-worker-2 +435: ForkJoinPool.commonPool-worker-3 +436: ForkJoinPool.commonPool-worker-2 +437: ForkJoinPool.commonPool-worker-3 +438: ForkJoinPool.commonPool-worker-2 +439: ForkJoinPool.commonPool-worker-3 +440: ForkJoinPool.commonPool-worker-2 +441: ForkJoinPool.commonPool-worker-3 +442: ForkJoinPool.commonPool-worker-2 +443: ForkJoinPool.commonPool-worker-3 +444: ForkJoinPool.commonPool-worker-2 +445: ForkJoinPool.commonPool-worker-3 +446: ForkJoinPool.commonPool-worker-2 +447: ForkJoinPool.commonPool-worker-3 +448: ForkJoinPool.commonPool-worker-2 +449: ForkJoinPool.commonPool-worker-3 +450: ForkJoinPool.commonPool-worker-2 +451: ForkJoinPool.commonPool-worker-3 +452: ForkJoinPool.commonPool-worker-2 +453: ForkJoinPool.commonPool-worker-3 +454: ForkJoinPool.commonPool-worker-2 +456: ForkJoinPool.commonPool-worker-2 +455: ForkJoinPool.commonPool-worker-3 +458: ForkJoinPool.commonPool-worker-3 +457: ForkJoinPool.commonPool-worker-2 +459: ForkJoinPool.commonPool-worker-3 +460: ForkJoinPool.commonPool-worker-2 +461: ForkJoinPool.commonPool-worker-3 +462: ForkJoinPool.commonPool-worker-2 +463: ForkJoinPool.commonPool-worker-3 +464: ForkJoinPool.commonPool-worker-2 +465: ForkJoinPool.commonPool-worker-3 +466: ForkJoinPool.commonPool-worker-2 +467: ForkJoinPool.commonPool-worker-3 +468: ForkJoinPool.commonPool-worker-2 +469: ForkJoinPool.commonPool-worker-3 +470: ForkJoinPool.commonPool-worker-2 +471: ForkJoinPool.commonPool-worker-3 +472: ForkJoinPool.commonPool-worker-2 +473: ForkJoinPool.commonPool-worker-3 +474: ForkJoinPool.commonPool-worker-2 +475: ForkJoinPool.commonPool-worker-3 +476: ForkJoinPool.commonPool-worker-2 +477: ForkJoinPool.commonPool-worker-3 +478: ForkJoinPool.commonPool-worker-2 +479: ForkJoinPool.commonPool-worker-3 +480: ForkJoinPool.commonPool-worker-2 +482: ForkJoinPool.commonPool-worker-2 +481: ForkJoinPool.commonPool-worker-3 +484: ForkJoinPool.commonPool-worker-3 +484: ForkJoinPool.commonPool-worker-2 +327: ForkJoinPool.commonPool-worker-1 +485: ForkJoinPool.commonPool-worker-3 +486: ForkJoinPool.commonPool-worker-2 +488: ForkJoinPool.commonPool-worker-3 +488: ForkJoinPool.commonPool-worker-1 +489: ForkJoinPool.commonPool-worker-2 +490: ForkJoinPool.commonPool-worker-3 +491: ForkJoinPool.commonPool-worker-1 +492: ForkJoinPool.commonPool-worker-2 +495: ForkJoinPool.commonPool-worker-2 +493: ForkJoinPool.commonPool-worker-3 +496: ForkJoinPool.commonPool-worker-2 +497: ForkJoinPool.commonPool-worker-3 +499: ForkJoinPool.commonPool-worker-3 +500: ForkJoinPool.commonPool-worker-3 +501: ForkJoinPool.commonPool-worker-3 +502: ForkJoinPool.commonPool-worker-3 +503: ForkJoinPool.commonPool-worker-3 +504: ForkJoinPool.commonPool-worker-3 +505: ForkJoinPool.commonPool-worker-3 +506: ForkJoinPool.commonPool-worker-3 +507: ForkJoinPool.commonPool-worker-3 +508: ForkJoinPool.commonPool-worker-3 +509: ForkJoinPool.commonPool-worker-3 +510: ForkJoinPool.commonPool-worker-3 +511: ForkJoinPool.commonPool-worker-3 diff --git a/enums/PostOffice.java b/enums/PostOffice.java index c5faa8cb..2aef48f9 100644 --- a/enums/PostOffice.java +++ b/enums/PostOffice.java @@ -26,7 +26,7 @@ class Mail { public String details() { return toString() + ", General Delivery: " + generalDelivery + - ", Address Scanability: " + scannability + + ", Address Scannability: " + scannability + ", Address Readability: " + readability + ", Address Address: " + address + ", Return address: " + returnAddress; @@ -138,52 +138,52 @@ public class PostOffice { } } /* Output: -Mail 0, General Delivery: NO2, Address Scanability: +Mail 0, General Delivery: NO2, Address Scannability: UNSCANNABLE, Address Readability: YES3, Address Address: OK1, Return address: OK1 Delivering Mail 0 normally ***** -Mail 1, General Delivery: NO5, Address Scanability: +Mail 1, General Delivery: NO5, Address Scannability: YES3, Address Readability: ILLEGIBLE, Address Address: OK5, Return address: OK1 Delivering Mail 1 automatically ***** -Mail 2, General Delivery: YES, Address Scanability: +Mail 2, General Delivery: YES, Address Scannability: YES3, Address Readability: YES1, Address Address: OK1, Return address: OK5 Using general delivery for Mail 2 ***** -Mail 3, General Delivery: NO4, Address Scanability: +Mail 3, General Delivery: NO4, Address Scannability: YES3, Address Readability: YES1, Address Address: INCORRECT, Return address: OK4 Returning Mail 3 to sender ***** -Mail 4, General Delivery: NO4, Address Scanability: +Mail 4, General Delivery: NO4, Address Scannability: UNSCANNABLE, Address Readability: YES1, Address Address: INCORRECT, Return address: OK2 Returning Mail 4 to sender ***** -Mail 5, General Delivery: NO3, Address Scanability: +Mail 5, General Delivery: NO3, Address Scannability: YES1, Address Readability: ILLEGIBLE, Address Address: OK4, Return address: OK2 Delivering Mail 5 automatically ***** -Mail 6, General Delivery: YES, Address Scanability: +Mail 6, General Delivery: YES, Address Scannability: YES4, Address Readability: ILLEGIBLE, Address Address: OK4, Return address: OK4 Using general delivery for Mail 6 ***** -Mail 7, General Delivery: YES, Address Scanability: +Mail 7, General Delivery: YES, Address Scannability: YES3, Address Readability: YES4, Address Address: OK2, Return address: MISSING Using general delivery for Mail 7 ***** -Mail 8, General Delivery: NO3, Address Scanability: +Mail 8, General Delivery: NO3, Address Scannability: YES1, Address Readability: YES3, Address Address: INCORRECT, Return address: MISSING Mail 8 is a dead letter ***** -Mail 9, General Delivery: NO1, Address Scanability: +Mail 9, General Delivery: NO1, Address Scannability: UNSCANNABLE, Address Readability: YES2, Address Address: OK1, Return address: OK4 Delivering Mail 9 normally diff --git a/enums/menu/Food.java b/enums/menu/Food.java index 5592f912..51f4a8a0 100644 --- a/enums/menu/Food.java +++ b/enums/menu/Food.java @@ -11,7 +11,7 @@ public interface Food { } enum MainCourse implements Food { LASAGNE, BURRITO, PAD_THAI, - LENTILS, HUMMOUS, VINDALOO; + LENTILS, HUMMUS, VINDALOO; } enum Dessert implements Food { TIRAMISU, GELATO, BLACK_FOREST_CAKE, diff --git a/enums/menu/Meal2.java b/enums/menu/Meal2.java index d5aea179..0064926e 100644 --- a/enums/menu/Meal2.java +++ b/enums/menu/Meal2.java @@ -21,7 +21,7 @@ public enum Meal2 { } enum MainCourse implements Food { LASAGNE, BURRITO, PAD_THAI, - LENTILS, HUMMOUS, VINDALOO; + LENTILS, HUMMUS, VINDALOO; } enum Dessert implements Food { TIRAMISU, GELATO, BLACK_FOREST_CAKE, diff --git a/equalshashcode/IndividualTest.java b/equalshashcode/IndividualTest.java index 33bb9b96..0be8de2d 100644 --- a/equalshashcode/IndividualTest.java +++ b/equalshashcode/IndividualTest.java @@ -3,7 +3,7 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. import collections.MapOfList; -import typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class IndividualTest { diff --git a/generics/CheckedList.java b/generics/CheckedList.java index 26cae143..71ebe3d0 100644 --- a/generics/CheckedList.java +++ b/generics/CheckedList.java @@ -3,7 +3,7 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Using Collection.checkedList() -import typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class CheckedList { @@ -30,6 +30,6 @@ public class CheckedList { } /* Output: Expected: java.lang.ClassCastException: Attempt to -insert class typeinfo.pets.Cat element into collection -with element type class typeinfo.pets.Dog +insert class reflection.pets.Cat element into collection +with element type class reflection.pets.Dog */ diff --git a/generics/DogsAndRobotMethodReferences.java b/generics/DogsAndRobotMethodReferences.java index 612ef683..2e9d003e 100644 --- a/generics/DogsAndRobotMethodReferences.java +++ b/generics/DogsAndRobotMethodReferences.java @@ -3,7 +3,7 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // "Assisted Latent Typing" -import typeinfo.pets.*; +import reflection.pets.*; import java.util.function.*; class PerformingDogA extends Dog { diff --git a/generics/DogsAndRobots.java b/generics/DogsAndRobots.java index 4bbb9b44..228e363d 100644 --- a/generics/DogsAndRobots.java +++ b/generics/DogsAndRobots.java @@ -3,7 +3,7 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // No (direct) latent typing in Java -import typeinfo.pets.*; +import reflection.pets.*; class PerformingDog extends Dog implements Performs { @Override diff --git a/generics/GenericHolder.java b/generics/GenericHolder.java index 15cfbb9e..b04aa17a 100644 --- a/generics/GenericHolder.java +++ b/generics/GenericHolder.java @@ -10,7 +10,7 @@ public class GenericHolder { public T get() { return a; } public static void main(String[] args) { GenericHolder h3 = - new GenericHolder(); + new GenericHolder<>(); h3.set(new Automobile()); // type checked Automobile a = h3.get(); // No cast needed //- h3.set("Not an Automobile"); // Error diff --git a/gradle/subprojects.gradle b/gradle/subprojects.gradle index e9d857d8..e4fb9764 100644 --- a/gradle/subprojects.gradle +++ b/gradle/subprojects.gradle @@ -6,7 +6,7 @@ project(':validating') { project(':equalshashcode') { dependencies { - compile project(':typeinfo') + compile project(':reflection') compile project(':collections') } } @@ -48,13 +48,13 @@ project(':hiding') { project(':generics') { dependencies { - compile project(':typeinfo') + compile project(':reflection') } } project(':collections') { dependencies { - compile project(':typeinfo') + compile project(':reflection') } } diff --git a/housekeeping/Flower.java b/housekeeping/Flower.java index ad90463c..fa519d1d 100644 --- a/housekeeping/Flower.java +++ b/housekeeping/Flower.java @@ -26,7 +26,7 @@ public class Flower { } Flower() { this("hi", 47); - System.out.println("no-arg constructor"); + System.out.println("Zero-argument constructor"); } void printPetalCount() { //- this(11); // Not inside non-constructor! @@ -41,6 +41,6 @@ public class Flower { /* Output: Constructor w/ int arg only, petalCount= 47 String & int args -no-arg constructor +Zero-argument constructor petalCount = 47 s = hi */ diff --git a/housekeeping/OverloadingOrder.java b/housekeeping/OverloadingOrder.java index e2cef301..434768d7 100644 --- a/housekeeping/OverloadingOrder.java +++ b/housekeeping/OverloadingOrder.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Overloading based on the order of the arguments +// Overloading based on parameter order public class OverloadingOrder { static void f(String s, int i) { diff --git a/housekeeping/SimpleConstructor2.java b/housekeeping/SimpleConstructor2.java index ae6936ef..2a002daf 100644 --- a/housekeeping/SimpleConstructor2.java +++ b/housekeeping/SimpleConstructor2.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Constructors can have arguments +// Constructors can have parameters class Rock2 { Rock2(int i) { diff --git a/javadoc/Documentation3.java b/javadoc/Documentation3.java index df0c403b..c48351ea 100644 --- a/javadoc/Documentation3.java +++ b/javadoc/Documentation3.java @@ -2,6 +2,7 @@ // (c)2021 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. + /** You can even insert a list: *
    *
  1. Item one @@ -9,4 +10,5 @@ *
  2. Item three *
*/ + public class Documentation3 {} diff --git a/onjava/BasicSupplier.java b/onjava/BasicSupplier.java index 838d3786..45bb5811 100644 --- a/onjava/BasicSupplier.java +++ b/onjava/BasicSupplier.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Supplier from a class with a no-arg constructor +// Supplier from a class with a zero-argument constructor package onjava; import java.util.function.*; import java.lang.reflect.InvocationTargetException; diff --git a/onjava/Countries.java b/onjava/Countries.java index 13764b64..666dda29 100644 --- a/onjava/Countries.java +++ b/onjava/Countries.java @@ -130,7 +130,7 @@ public class Countries { {"MOLDOVA","Chisinau"}, {"RUSSIA","Moscow"}, {"TAJIKISTAN","Dushanbe"}, - {"TURKMENISTAN","Ashkabad"}, + {"TURKMENISTAN","Ashkhabad"}, {"UKRAINE","Kyiv"}, {"UZBEKISTAN","Tashkent"}, // Europe diff --git a/onjava/MouseClick.java b/onjava/MouseClick.java index 1bfa28e9..ea7e530d 100644 --- a/onjava/MouseClick.java +++ b/onjava/MouseClick.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Helper interface to allow lambda expressions +// Helper interface to allow lambda expressions. package onjava; import java.awt.event.*; diff --git a/onjava/atunit/AtUnit.java b/onjava/atunit/AtUnit.java index 7288c2f4..97601088 100644 --- a/onjava/atunit/AtUnit.java +++ b/onjava/atunit/AtUnit.java @@ -63,11 +63,11 @@ public class AtUnit implements ProcessFiles.Strategy { .getDeclaredConstructor() .getModifiers())) { System.out.println("Error: " + testClass + - " no-arg constructor must be public"); + " zero-argument constructor must be public"); System.exit(1); } } catch(NoSuchMethodException e) { - // Synthesized no-arg constructor; OK + // Synthesized zero-argument constructor; OK } System.out.println(testClass.getName()); } @@ -159,7 +159,7 @@ public class AtUnit implements ProcessFiles.Strategy { throw new RuntimeException("Couldn't run " + "@TestObject (creator) method."); } - } else { // Use the no-arg constructor: + } else { // Use the zero-argument constructor: try { return testClass .getConstructor().newInstance(); diff --git a/patterns/BoxObserver.java b/patterns/BoxObserver.java index db25c5ea..b48120bc 100644 --- a/patterns/BoxObserver.java +++ b/patterns/BoxObserver.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Demonstration of Observer pattern using +// Demonstration of the Observer pattern using // Java's built-in observer classes. // {ExcludeFromGradle} // Won't work under WSL2 import javax.swing.*; @@ -12,46 +12,34 @@ import java.util.*; import onjava.*; import onjava.MouseClick; -// You must inherit a new type of Observable: @SuppressWarnings("deprecation") -class BoxObservable extends Observable { - @Override - public void notifyObservers(Object b) { - // Otherwise it won't propagate changes: - setChanged(); - super.notifyObservers(b); - } -} - -@SuppressWarnings("deprecation") -public class BoxObserver extends JFrame { - Observable notifier = new BoxObservable(); - public BoxObserver(int grid) { +class Boxes extends JFrame { + Observable notifier = new Observable() { + @Override + public void notifyObservers(Object b) { + setChanged(); + super.notifyObservers(b); + } + }; + public Boxes(int grid) { setTitle("Demonstrates Observer pattern"); Container cp = getContentPane(); cp.setLayout(new GridLayout(grid, grid)); for(int x = 0; x < grid; x++) for(int y = 0; y < grid; y++) - cp.add(new OCBox(x, y, notifier)); - } - public static void main(String[] args) { - // For automated test runs: - // new TimedAbort(4); - int grid = 8; - if(args.length > 0) - grid = Integer.parseInt(args[0]); - JFrame f = new BoxObserver(grid); - f.setSize(500, 400); - f.setVisible(true); - f.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + cp.add(new Box(x, y, notifier)); + setSize(500, 400); + setVisible(true); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); } } @SuppressWarnings("deprecation") -class OCBox extends JPanel implements Observer { - Observable notifier; - int x, y; // Locations in grid - Color cColor = newColor(); +class Box extends JPanel implements Observer { + int x, y; // Location in grid + Color color = COLORS[ + (int)(Math.random() * COLORS.length) + ]; static final Color[] COLORS = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, @@ -59,35 +47,38 @@ class OCBox extends JPanel implements Observer { Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; - static Color newColor() { - return COLORS[ - (int)(Math.random() * COLORS.length) - ]; - } - OCBox(int x, int y, Observable notifier) { + Box(int x, int y, Observable notifier) { this.x = x; this.y = y; notifier.addObserver(this); - this.notifier = notifier; addMouseListener((MouseClick) - e -> notifier.notifyObservers(OCBox.this)); + e -> notifier.notifyObservers(Box.this)); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); - g.setColor(cColor); + g.setColor(color); Dimension s = getSize(); g.fillRect(0, 0, s.width, s.height); } @Override public void update(Observable o, Object arg) { - OCBox clicked = (OCBox)arg; + Box clicked = (Box)arg; if(nextTo(clicked)) { - cColor = clicked.cColor; + color = clicked.color; repaint(); } } - private boolean nextTo(OCBox b) { + private boolean nextTo(Box b) { return Math.abs(x - b.x) <= 1 && Math.abs(y - b.y) <= 1; } } + +public class BoxObserver { + public static void main(String[] args) { + int grid = 8; + if(args.length > 0) + grid = Integer.parseInt(args[0]); + new Boxes(grid); + } +} diff --git a/patterns/CleanTheFloor.java b/patterns/CleanTheFloor.java new file mode 100644 index 00000000..8196f44b --- /dev/null +++ b/patterns/CleanTheFloor.java @@ -0,0 +1,42 @@ +// patterns/CleanTheFloor.java +// (c)2021 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. +// State with different compositional interface. + +class FloorCleaner { + private Attachment attachment = new Vacuum(); + public void change(Attachment newAttachment) { + attachment = newAttachment; + } + public void clean() { attachment.action(); } +} + +interface Attachment { + void action(); +} + +class Vacuum implements Attachment { + @Override public void action() { + System.out.println("Vacuuming"); + } +} + +class Mop implements Attachment { + @Override public void action() { + System.out.println("Mopping"); + } +} + +public class CleanTheFloor { + public static void main(String[] args) { + FloorCleaner floorCleaner = new FloorCleaner(); + floorCleaner.clean(); + floorCleaner.change(new Mop()); + floorCleaner.clean(); + } +} +/* Output: +Vacuuming +Mopping +*/ diff --git a/patterns/CommandPattern.java b/patterns/CommandPattern.java index 1a961224..05bc6120 100644 --- a/patterns/CommandPattern.java +++ b/patterns/CommandPattern.java @@ -4,16 +4,23 @@ // Visit http://OnJava8.com for more book information. import java.util.*; +class Command { + public final String msg; + public Command(String msg) { + this.msg = msg; + } +} + public class CommandPattern { + public static void show(Command cmd) { + System.out.println(cmd.msg); + } public static void main(String[] args) { - List macro = Arrays.asList( - () -> System.out.print("Hello "), - () -> System.out.print("World! "), - () -> System.out.print("I'm the command pattern!") - ); - macro.forEach(Runnable::run); + show(new Command("First Command")); + show(new Command("Second Command")); } } /* Output: -Hello World! I'm the command pattern! +First Command +Second Command */ diff --git a/patterns/Macro.java b/patterns/Macro.java new file mode 100644 index 00000000..586b128e --- /dev/null +++ b/patterns/Macro.java @@ -0,0 +1,25 @@ +// patterns/Macro.java +// (c)2021 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 Macro { + public static void main(String[] args) { + List macro = new ArrayList<>( + Arrays.asList( + () -> System.out.print("Hello "), + () -> System.out.println("World! ") + )); + macro.forEach(Runnable::run); + macro.add( + () -> System.out.print("I'm the command pattern!") + ); + macro.forEach(Runnable::run); + } +} +/* Output: +Hello World! +Hello World! +I'm the command pattern! +*/ diff --git a/patterns/PaperScissorsRock.java b/patterns/PaperScissorsRock.java index 54063d6c..ce48dd4d 100644 --- a/patterns/PaperScissorsRock.java +++ b/patterns/PaperScissorsRock.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Demonstration of multiple dispatching +// Demonstration of multiple dispatching. import java.util.*; import java.util.function.*; import java.util.stream.*; diff --git a/patterns/ProxyDemo.java b/patterns/ProxyDemo.java index 011fbe7e..31f45aed 100644 --- a/patterns/ProxyDemo.java +++ b/patterns/ProxyDemo.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Simple demonstration of the Proxy pattern +// Basic demonstration of the Proxy pattern. interface ProxyBase { void f(); @@ -11,10 +11,8 @@ interface ProxyBase { } class Proxy implements ProxyBase { - private ProxyBase implementation; - Proxy() { - implementation = new Implementation(); - } + private ProxyBase implementation = + new Implementation(); // Pass method calls to the implementation: @Override public void f() { implementation.f(); } diff --git a/patterns/Resource.java b/patterns/Resource.java new file mode 100644 index 00000000..1a8ceda5 --- /dev/null +++ b/patterns/Resource.java @@ -0,0 +1,9 @@ +// patterns/Resource.java +// (c)2021 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. + +public interface Resource { + T get(); + void set(T x); +} diff --git a/patterns/ShapeFactory1.java b/patterns/ShapeFactory1.java index 950c97c5..a47b75b7 100644 --- a/patterns/ShapeFactory1.java +++ b/patterns/ShapeFactory1.java @@ -2,7 +2,7 @@ // (c)2021 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 +// A basic static factory method. import java.util.*; import java.util.stream.*; import patterns.shapes.*; diff --git a/patterns/ShapeFactory2.java b/patterns/ShapeFactory2.java index e8206ba2..552e3630 100644 --- a/patterns/ShapeFactory2.java +++ b/patterns/ShapeFactory2.java @@ -8,9 +8,9 @@ import java.util.stream.*; import patterns.shapes.*; public class ShapeFactory2 implements FactoryMethod { - Map factories = + private Map factories = new HashMap<>(); - static Constructor load(String id) { + private static Constructor load(String id) { System.out.println("loading " + id); try { return Class.forName("patterns.shapes." + id) diff --git a/patterns/ShapeFactory3.java b/patterns/ShapeFactory3.java index fdf7b270..6571c536 100644 --- a/patterns/ShapeFactory3.java +++ b/patterns/ShapeFactory3.java @@ -2,7 +2,7 @@ // (c)2021 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 +// Polymorphic factory methods. import java.util.*; import java.util.function.*; import java.util.stream.*; @@ -19,14 +19,15 @@ class RandomShapes implements Supplier { this.factories = factories; } @Override public Shape get() { - return factories[ - rand.nextInt(factories.length)].create(); + return + factories[rand.nextInt(factories.length)] + .create(); } } public class ShapeFactory3 { public static void main(String[] args) { - RandomShapes rs = new RandomShapes( + RandomShapes rs = new RandomShapes( // [1] Circle::new, Square::new, Triangle::new ); Stream.generate(rs) diff --git a/patterns/Single.java b/patterns/Single.java new file mode 100644 index 00000000..6bdb2d6e --- /dev/null +++ b/patterns/Single.java @@ -0,0 +1,18 @@ +// patterns/Single.java +// (c)2021 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. + +@SuppressWarnings("unchecked") +public final class Single { + private static Object single; // [1] + public Single(T val) { + if(single != null) + throw new RuntimeException( + "Attempt to reassign Single<" + + val.getClass().getSimpleName() + ">" + ); + single = val; + } + public T get() { return (T)single; } +} diff --git a/patterns/SingletonPattern.java b/patterns/SingletonPattern.java index 2ff8de0d..3ed313e6 100644 --- a/patterns/SingletonPattern.java +++ b/patterns/SingletonPattern.java @@ -3,58 +3,45 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. -interface Resource { - int getValue(); - void setValue(int x); -} - -// This isn't inherited from a Cloneable -// base class and cloneability isn't added so -// making it final prevents cloneability from -// being added through inheritance. This also -// implements thread-safe lazy initialization: - -final class Singleton { - private static final class - ResourceImpl implements Resource { - private int i; - private ResourceImpl(int i) { - this.i = i; - } - @Override - public synchronized int getValue() { - return i; - } - @Override - public synchronized void setValue(int x) { - i = x; - } +final class IntegerSingleton + implements Resource { + private static IntegerSingleton value = + new IntegerSingleton(); + private Integer i = Integer.valueOf(0); + private IntegerSingleton() { + System.out.println("IntegerSingleton()"); } - private static class ResourceHolder { - private static Resource resource = - new ResourceImpl(47); - } - public static Resource getResource() { - return ResourceHolder.resource; + public static IntegerSingleton instance() { + return value; } + @Override public synchronized + Integer get() { return i; } + @Override public synchronized + void set(Integer x) { i = x; } } public class SingletonPattern { + public static void show(Resource r) { + T val = r.get(); + System.out.println(val); + } + public static void put(Resource r, T val) { + r.set(val); + } public static void main(String[] args) { - Resource r = Singleton.getResource(); - System.out.println(r.getValue()); - Resource s2 = Singleton.getResource(); - s2.setValue(9); - System.out.println(r.getValue()); - try { - // Can't do this: compile-time error. - // Singleton s3 = (Singleton)s2.clone(); - } catch(Exception e) { - throw new RuntimeException(e); - } + System.out.println("Inside main()"); + Resource ir = + IntegerSingleton.instance(); + Resource ir2 = + IntegerSingleton.instance(); + show(ir); + put(ir2, Integer.valueOf(9)); + show(ir); } } /* Output: -47 +Inside main() +IntegerSingleton() +0 9 */ diff --git a/patterns/SingletonPattern2.java b/patterns/SingletonPattern2.java new file mode 100644 index 00000000..c02fa9f2 --- /dev/null +++ b/patterns/SingletonPattern2.java @@ -0,0 +1,16 @@ +// patterns/SingletonPattern2.java +// (c)2021 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. + +public class SingletonPattern2 { + public static void main(String[] args) { + Single pi = + new Single<>(Double.valueOf(3.14159)); + Double x = pi.get(); + System.out.println(x); + } +} +/* Output: +3.14159 +*/ diff --git a/patterns/SingletonPattern3.java b/patterns/SingletonPattern3.java new file mode 100644 index 00000000..7aee59f0 --- /dev/null +++ b/patterns/SingletonPattern3.java @@ -0,0 +1,33 @@ +// patterns/SingletonPattern3.java +// (c)2021 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. + +class MyString { + private String s; + public MyString(String s) { + this.s = s; + } + public synchronized + void change(String s) { + this.s = s; + } + @Override public synchronized + String toString() { + return s; + } +} + +public class SingletonPattern3 { + public static void main(String[] args) { + Single x = + new Single<>(new MyString("Hello")); + System.out.println(x.get()); + x.get().change("World!"); + System.out.println(x.get()); + } +} +/* Output: +Hello +World! +*/ diff --git a/patterns/StateDemo.java b/patterns/StateDemo.java index 0a564bfa..e96bac6f 100644 --- a/patterns/StateDemo.java +++ b/patterns/StateDemo.java @@ -2,34 +2,24 @@ // (c)2021 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. -// Simple demonstration of the State pattern +// Basic demonstration of the State pattern. -interface StateBase { - void f(); - void g(); - void h(); - void changeImp(StateBase newImp); -} - -class State implements StateBase { - private StateBase implementation; - State(StateBase imp) { +class State { + private State implementation; + protected State() {} + public State(State imp) { implementation = imp; } - @Override - public void changeImp(StateBase newImp) { + public void change(State newImp) { implementation = newImp; } - // Pass method calls to the implementation: - @Override + // Forward method calls to the implementation: public void f() { implementation.f(); } - @Override public void g() { implementation.g(); } - @Override public void h() { implementation.h(); } } -class Implementation1 implements StateBase { +class Implementation1 extends State { @Override public void f() { System.out.println("Implementation1.f()"); } @@ -39,11 +29,9 @@ class Implementation1 implements StateBase { @Override public void h() { System.out.println("Implementation1.h()"); } - @Override - public void changeImp(StateBase newImp) {} } -class Implementation2 implements StateBase { +class Implementation2 extends State { @Override public void f() { System.out.println("Implementation2.f()"); } @@ -53,28 +41,27 @@ class Implementation2 implements StateBase { @Override public void h() { System.out.println("Implementation2.h()"); } - @Override - public void changeImp(StateBase newImp) {} } public class StateDemo { - static void test(StateBase b) { - b.f(); - b.g(); - b.h(); + static void test(State s) { + s.f(); + s.g(); + s.h(); } public static void main(String[] args) { - StateBase b = - new State(new Implementation1()); - test(b); - b.changeImp(new Implementation2()); - test(b); + State s = new State(new Implementation1()); + test(s); + System.out.println("Changing implementation"); + s.change(new Implementation2()); + test(s); } } /* Output: Implementation1.f() Implementation1.g() Implementation1.h() +Changing implementation Implementation2.f() Implementation2.g() Implementation2.h() diff --git a/patterns/TemplateMethod.java b/patterns/TemplateMethod.java index 2df6a3d4..e9774e18 100644 --- a/patterns/TemplateMethod.java +++ b/patterns/TemplateMethod.java @@ -2,29 +2,29 @@ // (c)2021 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. -// Simple demonstration of Template Method +// Basic Template Method pattern. import java.util.stream.*; abstract class ApplicationFramework { ApplicationFramework() { templateMethod(); } - abstract void customize1(); - abstract void customize2(); + abstract void customize1(int n); + abstract void customize2(int n); // "private" means automatically "final": private void templateMethod() { IntStream.range(0, 5).forEach( - n -> { customize1(); customize2(); }); + n -> { customize1(n); customize2(n); }); } } -// Create a new "application": +// Create a new application: class MyApp extends ApplicationFramework { - @Override void customize1() { - System.out.print("Hello "); + @Override void customize1(int n) { + System.out.print("customize1 " + n); } - @Override void customize2() { - System.out.println("World!"); + @Override void customize2(int n) { + System.out.println(" customize2 " + n); } } @@ -34,9 +34,9 @@ public class TemplateMethod { } } /* Output: -Hello World! -Hello World! -Hello World! -Hello World! -Hello World! +customize1 0 customize2 0 +customize1 1 customize2 1 +customize1 2 customize2 2 +customize1 3 customize2 3 +customize1 4 customize2 4 */ diff --git a/patterns/TestSingle.java b/patterns/TestSingle.java new file mode 100644 index 00000000..209f0647 --- /dev/null +++ b/patterns/TestSingle.java @@ -0,0 +1,20 @@ +// patterns/TestSingle.java +// (c)2021 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. + +public class TestSingle { + public static void main(String[] args) { + Single ss = new Single<>("hello"); + System.out.println(ss.get()); + try { + Single ss2 = new Single<>("world"); + } catch(Exception e) { + System.out.println(e.getMessage()); + } + } +} +/* Output: +hello +Attempt to reassign Single +*/ diff --git a/patterns/TypeMap.java b/patterns/TypeMap.java new file mode 100644 index 00000000..db2f393a --- /dev/null +++ b/patterns/TypeMap.java @@ -0,0 +1,21 @@ +// patterns/TypeMap.java +// (c)2021 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. +// Generic TypeMap works for any types. +package patterns; +import java.util.*; +import java.util.stream.*; + +public class TypeMap { + public final Map> map = + new HashMap<>(); + public void add(T o) { + Class type = o.getClass(); + map.computeIfAbsent(type, + k -> new ArrayList()).add(o); + } + public Stream> values() { + return map.values().stream(); + } +} diff --git a/patterns/abstractfactory/GameEnvironment.java b/patterns/abstractfactory/GameEnvironment.java index a990562d..51b42d55 100644 --- a/patterns/abstractfactory/GameEnvironment.java +++ b/patterns/abstractfactory/GameEnvironment.java @@ -2,7 +2,7 @@ // (c)2021 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 +// An example of the Abstract Factory pattern. // {java patterns.abstractfactory.GameEnvironment} package patterns.abstractfactory; import java.util.function.*; @@ -23,10 +23,10 @@ class Kitty implements Player { } } -class KungFuGuy implements Player { +class Fighter implements Player { @Override public void interactWith(Obstacle ob) { - System.out.print("KungFuGuy now battles a "); + System.out.print("Fighter now battles a "); ob.action(); } } @@ -37,9 +37,9 @@ class Puzzle implements Obstacle { } } -class NastyWeapon implements Obstacle { +class Weapon implements Obstacle { @Override public void action() { - System.out.println("NastyWeapon"); + System.out.println("Weapon"); } } @@ -58,11 +58,11 @@ extends GameElementFactory { } } -class KillAndDismember +class Melee extends GameElementFactory { - KillAndDismember() { - player = KungFuGuy::new; - obstacle = NastyWeapon::new; + Melee() { + player = Fighter::new; + obstacle = Weapon::new; } } @@ -80,15 +80,15 @@ public class GameEnvironment { public static void main(String[] args) { GameElementFactory kp = new KittiesAndPuzzles(), - kd = new KillAndDismember(); + ml = new Melee(); GameEnvironment g1 = new GameEnvironment(kp), - g2 = new GameEnvironment(kd); + g2 = new GameEnvironment(ml); g1.play(); g2.play(); } } /* Output: Kitty has encountered a Puzzle -KungFuGuy now battles a NastyWeapon +Fighter now battles a Weapon */ diff --git a/patterns/adapt/Adapter.java b/patterns/adapt/Adapter.java index 0e847f36..0b3a0919 100644 --- a/patterns/adapt/Adapter.java +++ b/patterns/adapt/Adapter.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Variations on the Adapter pattern +// Variations on the Adapter pattern. // {java patterns.adapt.Adapter} package patterns.adapt; diff --git a/patterns/chain/ChainOfResponsibility.java b/patterns/chain/ChainOfResponsibility.java index 737c3c20..b291c3e0 100644 --- a/patterns/chain/ChainOfResponsibility.java +++ b/patterns/chain/ChainOfResponsibility.java @@ -2,55 +2,32 @@ // (c)2021 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. -// Using the Functional interface // {java patterns.chain.ChainOfResponsibility} package patterns.chain; import java.util.*; import java.util.function.*; -class Result { - boolean success; - List line; - Result(List data) { - success = true; - line = data; - } - Result() { - success = false; - line = Collections.emptyList(); - } -} - -class Fail extends Result {} - interface Algorithm { Result algorithm(List line); } class FindMinima { - public static Result leastSquares(List line) { - System.out.println("LeastSquares.algorithm"); - boolean weSucceed = false; - if(weSucceed) // Actual test/calculation here - return new Result(Arrays.asList(1.1, 2.2)); + public static Result test( + boolean success, String id, double d1, double d2) { + System.out.println(id); + if(success) // Actual test/calculation here + return new Result(Arrays.asList(d1, d2)); else // Try the next one in the chain: - return new Fail(); + return Result.fail; + } + public static Result leastSquares(List line) { + return test(false, "LeastSquares", 1.1, 2.2); } public static Result perturbation(List line) { - System.out.println("Perturbation.algorithm"); - boolean weSucceed = false; - if(weSucceed) // Actual test/calculation here - return new Result(Arrays.asList(3.3, 4.4)); - else - return new Fail(); + return test(false, "Perturbation", 3.3, 4.4); } public static Result bisection(List line) { - System.out.println("Bisection.algorithm"); - boolean weSucceed = true; - if(weSucceed) // Actual test/calculation here - return new Result(Arrays.asList(5.5, 6.6)); - else - return new Fail(); + return test(true, "Bisection", 5.5, 6.6); } static List, Result>> algorithms = Arrays.asList( @@ -65,7 +42,7 @@ class FindMinima { if(result.success) return result; } - return new Fail(); + return Result.fail; } } @@ -83,8 +60,8 @@ public class ChainOfResponsibility { } } /* Output: -LeastSquares.algorithm -Perturbation.algorithm -Bisection.algorithm +LeastSquares +Perturbation +Bisection [5.5, 6.6] */ diff --git a/patterns/chain/Result.java b/patterns/chain/Result.java new file mode 100644 index 00000000..dc459b39 --- /dev/null +++ b/patterns/chain/Result.java @@ -0,0 +1,21 @@ +// patterns/chain/Result.java +// (c)2021 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. +// Carries the result or indicates failure. +package patterns.chain; +import java.util.*; + +public class Result { + public final boolean success; + public final List line; + public Result(List data) { + success = true; + line = data; + } + private Result() { + success = false; + line = Collections.emptyList(); + } + public static final Result fail = new Result(); +} diff --git a/patterns/doubledispatch/Aluminum.java b/patterns/doubledispatch/Aluminum.java index f48a2077..9f5678b1 100644 --- a/patterns/doubledispatch/Aluminum.java +++ b/patterns/doubledispatch/Aluminum.java @@ -2,13 +2,13 @@ // (c)2021 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. -// Aluminum for double dispatching +// Aluminum with double dispatching. package patterns.doubledispatch; import patterns.trash.*; import java.util.*; public class Aluminum extends patterns.trash.Aluminum - implements TypedBinMember { +implements TypedBinMember { public Aluminum(double wt) { super(wt); } @Override public boolean addToBin(List tbins) { diff --git a/patterns/doubledispatch/Cardboard.java b/patterns/doubledispatch/Cardboard.java index 2150328f..39029232 100644 --- a/patterns/doubledispatch/Cardboard.java +++ b/patterns/doubledispatch/Cardboard.java @@ -2,13 +2,13 @@ // (c)2021 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. -// Cardboard for double dispatching +// Cardboard with double dispatching. package patterns.doubledispatch; import patterns.trash.*; import java.util.*; public class Cardboard extends patterns.trash.Cardboard - implements TypedBinMember { +implements TypedBinMember { public Cardboard(double wt) { super(wt); } @Override public boolean addToBin(List tbins) { diff --git a/patterns/doubledispatch/DoubleDispatch.java b/patterns/doubledispatch/DoubleDispatch.java index f65d2680..d1ddfe25 100644 --- a/patterns/doubledispatch/DoubleDispatch.java +++ b/patterns/doubledispatch/DoubleDispatch.java @@ -3,90 +3,104 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Using multiple dispatching to handle more -// than one unknown type during a method call +// than one unknown type during a method call. // {java patterns.doubledispatch.DoubleDispatch} package patterns.doubledispatch; import patterns.trash.*; import java.util.*; class AluminumBin extends TypedBin { + public AluminumBin() { super("Aluminum"); } @Override public boolean add(Aluminum a) { return addIt(a); } } class PaperBin extends TypedBin { + public PaperBin() { super("Paper"); } @Override public boolean add(Paper a) { return addIt(a); } } class GlassBin extends TypedBin { + public GlassBin() { super("Glass"); } @Override public boolean add(Glass a) { return addIt(a); } } class CardboardBin extends TypedBin { + public CardboardBin() { super("Cardboard"); } @Override public boolean add(Cardboard a) { return addIt(a); } } class TrashBinSet { - private List binSet = Arrays.asList( - new AluminumBin(), - new PaperBin(), - new GlassBin(), - new CardboardBin() - ); + public final List binSet = + Arrays.asList( + new AluminumBin(), new PaperBin(), + new GlassBin(), new CardboardBin() + ); @SuppressWarnings("unchecked") public void sortIntoBins(List bin) { bin.forEach( aBin -> { TypedBinMember t = (TypedBinMember)aBin; if(!t.addToBin(binSet)) - System.err.println("Couldn't add " + t); + throw new RuntimeException( + "sortIntoBins() couldn't add " + t); }); } - public List binSet() { return binSet; } } public class DoubleDispatch { public static void main(String[] args) { List bin = new ArrayList<>(); - TrashBinSet bins = new TrashBinSet(); - // ParseTrash still works, without changes: ParseTrash.fillBin("doubledispatch", bin); + TrashBinSet bins = new TrashBinSet(); // Sort from the master bin into the // individually-typed bins: bins.sortIntoBins(bin); - // Perform sumValue for each bin... - bins.binSet() - .forEach(tb -> Trash.sumValue(tb.v)); - // ... and for the master bin - Trash.sumValue(bin); + // Sum value of each bin... + bins.binSet.forEach(tb -> + TrashValue.sum(tb.bin(), tb.type)); + // ... and for the master bin: + TrashValue.sum(bin, "Trash"); } } -/* Output: (First and Last 10 Lines) -Loading patterns.doubledispatch.Glass +/* Output: +Loading patterns.doubledispatch.Cardboard Loading patterns.doubledispatch.Paper Loading patterns.doubledispatch.Aluminum -Loading patterns.doubledispatch.Cardboard -weight of patterns.doubledispatch.Aluminum = 89.0 -weight of patterns.doubledispatch.Aluminum = 76.0 -weight of patterns.doubledispatch.Aluminum = 25.0 -weight of patterns.doubledispatch.Aluminum = 34.0 -weight of patterns.doubledispatch.Aluminum = 27.0 -weight of patterns.doubledispatch.Aluminum = 18.0 -...________...________...________...________... -weight of patterns.doubledispatch.Aluminum = 93.0 -weight of patterns.doubledispatch.Glass = 93.0 -weight of patterns.doubledispatch.Paper = 80.0 -weight of patterns.doubledispatch.Glass = 36.0 -weight of patterns.doubledispatch.Glass = 12.0 -weight of patterns.doubledispatch.Glass = 60.0 -weight of patterns.doubledispatch.Paper = 66.0 -weight of patterns.doubledispatch.Aluminum = 36.0 -weight of patterns.doubledispatch.Cardboard = 22.0 -Total value = 1086.0599818825722 +Loading patterns.doubledispatch.Glass +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Total Aluminum value = 13.19 +Paper weight: 8.00 * price: 0.10 = 0.80 +Paper weight: 6.60 * price: 0.10 = 0.66 +Paper weight: 9.10 * price: 0.10 = 0.91 +Total Paper value = 2.37 +Glass weight: 5.40 * price: 0.23 = 1.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Glass value = 3.06 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Total Cardboard value = 0.86 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Paper weight: 8.00 * price: 0.10 = 0.80 +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Glass weight: 5.40 * price: 0.23 = 1.24 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Paper weight: 6.60 * price: 0.10 = 0.66 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Paper weight: 9.10 * price: 0.10 = 0.91 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Trash value = 19.48 */ diff --git a/patterns/doubledispatch/Glass.java b/patterns/doubledispatch/Glass.java index 0092652f..c0800cdf 100644 --- a/patterns/doubledispatch/Glass.java +++ b/patterns/doubledispatch/Glass.java @@ -2,13 +2,13 @@ // (c)2021 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. -// Glass for double dispatching +// Glass with double dispatching. package patterns.doubledispatch; import patterns.trash.*; import java.util.*; public class Glass extends patterns.trash.Glass - implements TypedBinMember { +implements TypedBinMember { public Glass(double wt) { super(wt); } @Override public boolean addToBin(List tbins) { diff --git a/patterns/doubledispatch/Paper.java b/patterns/doubledispatch/Paper.java index 67bb0fe3..de25cab3 100644 --- a/patterns/doubledispatch/Paper.java +++ b/patterns/doubledispatch/Paper.java @@ -2,13 +2,13 @@ // (c)2021 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. -// Paper for double dispatching +// Paper with double dispatching. package patterns.doubledispatch; import patterns.trash.*; import java.util.*; public class Paper extends patterns.trash.Paper - implements TypedBinMember { +implements TypedBinMember { public Paper(double wt) { super(wt); } @Override public boolean addToBin(List tbins) { diff --git a/patterns/doubledispatch/TypedBin.java b/patterns/doubledispatch/TypedBin.java index c730eda2..8484552c 100644 --- a/patterns/doubledispatch/TypedBin.java +++ b/patterns/doubledispatch/TypedBin.java @@ -2,15 +2,24 @@ // (c)2021 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 List that can grab the right type +// A List that can grab the right type. package patterns.doubledispatch; import patterns.trash.*; import java.util.*; public class TypedBin { - List v = new ArrayList<>(); + private List typedBin = + new ArrayList<>(); + public final String type; + public TypedBin(String type) { + this.type = type; + } + public List bin() { + // Returns a copy of typedBin: + return new ArrayList(typedBin); + } protected boolean addIt(Trash t) { - v.add(t); + typedBin.add(t); return true; } public boolean add(Aluminum a) { diff --git a/patterns/doubledispatch/TypedBinMember.java b/patterns/doubledispatch/TypedBinMember.java index 17fe2064..109745b4 100644 --- a/patterns/doubledispatch/TypedBinMember.java +++ b/patterns/doubledispatch/TypedBinMember.java @@ -2,13 +2,12 @@ // (c)2021 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 interface for adding the double dispatching -// method to the trash hierarchy without -// modifying the original hierarchy +// Adapts the double-dispatching +// method into the trash hierarchy without +// modifying the original hierarchy. package patterns.doubledispatch; import java.util.*; public interface TypedBinMember { - // The new method: boolean addToBin(List bins); } diff --git a/patterns/dynatrash/DynaTrash.java b/patterns/dynatrash/DynaTrash.java deleted file mode 100644 index 8e8d6ac2..00000000 --- a/patterns/dynatrash/DynaTrash.java +++ /dev/null @@ -1,74 +0,0 @@ -// patterns/dynatrash/DynaTrash.java -// (c)2021 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. -// Using a Map of Lists and RTTI to automatically -// sort trash into Lists. This solution, despite -// the use of RTTI, is extensible. -// {java patterns.dynatrash.DynaTrash} -package patterns.dynatrash; -import patterns.trash.*; -import java.util.*; -import java.util.stream.*; - -// Generic TypeMap works in any situation: -class TypeMap { - private Map> t = new HashMap<>(); - public void add(T o) { - Class type = o.getClass(); - if(t.containsKey(type)) - t.get(type).add(o); - else { - List v = new ArrayList<>(); - v.add(o); - t.put(type,v); - } - } - public Stream> values() { - return t.values().stream(); - } -} - -// Adapter class for callbacks -// from ParseTrash.fillBin(): -class TypeMapAdapter implements Fillable { - TypeMap map; - TypeMapAdapter(TypeMap tm) { - map = tm; - } - @Override - public void addTrash(Trash t) { map.add(t); } -} - -public class DynaTrash { - @SuppressWarnings("unchecked") - public static void main(String[] args) { - TypeMap bin = new TypeMap<>(); - ParseTrash.fillBin( - "trash", new TypeMapAdapter(bin)); - bin.values().forEach(Trash::sumValue); - } -} -/* Output: (First and Last 10 Lines) -Loading patterns.trash.Glass -Loading patterns.trash.Paper -Loading patterns.trash.Aluminum -Loading patterns.trash.Cardboard -weight of patterns.trash.Paper = 22.0 -weight of patterns.trash.Paper = 11.0 -weight of patterns.trash.Paper = 88.0 -weight of patterns.trash.Paper = 91.0 -weight of patterns.trash.Paper = 80.0 -weight of patterns.trash.Paper = 66.0 -...________...________...________...________... -weight of patterns.trash.Glass = 93.0 -weight of patterns.trash.Glass = 36.0 -weight of patterns.trash.Glass = 12.0 -weight of patterns.trash.Glass = 60.0 -Total value = 150.1900027245283 -weight of patterns.trash.Cardboard = 96.0 -weight of patterns.trash.Cardboard = 44.0 -weight of patterns.trash.Cardboard = 12.0 -weight of patterns.trash.Cardboard = 22.0 -Total value = 40.02000072598457 -*/ diff --git a/patterns/observer/ObservedFlower.java b/patterns/observer/ObservedFlower.java index a8c9bd00..acfb0f65 100644 --- a/patterns/observer/ObservedFlower.java +++ b/patterns/observer/ObservedFlower.java @@ -2,19 +2,35 @@ // (c)2021 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. -// Demonstration of "Observer" pattern +// Demonstration of the Observer pattern. // {java patterns.observer.ObservedFlower} package patterns.observer; import java.util.*; +import java.util.function.*; @SuppressWarnings("deprecation") class Flower { - private boolean isOpen; - private boolean alreadyOpen; - private boolean alreadyClosed; - Flower() { isOpen = false; } - OpenNotifier opening = new OpenNotifier(); - CloseNotifier closing = new CloseNotifier(); + private boolean isOpen = false; + private boolean alreadyOpen = false; + private boolean alreadyClosed = false; + Observable opening = new Observable() { + @Override public void notifyObservers() { + if(isOpen && !alreadyOpen) { + setChanged(); + super.notifyObservers(); + alreadyOpen = true; + } + } + }; + Observable closing = new Observable() { + @Override public void notifyObservers() { + if(!isOpen && !alreadyClosed) { + setChanged(); + super.notifyObservers(); + alreadyClosed = true; + } + } + }; public void open() { // Opens its petals isOpen = true; opening.notifyObservers(); @@ -25,55 +41,32 @@ class Flower { closing.notifyObservers(); alreadyOpen = false; } - class OpenNotifier extends Observable { - @Override public void notifyObservers() { - if(isOpen && !alreadyOpen) { - setChanged(); - super.notifyObservers(); - alreadyOpen = true; - } - } - } - class CloseNotifier extends Observable{ - @Override public void notifyObservers() { - if(!isOpen && !alreadyClosed) { - setChanged(); - super.notifyObservers(); - alreadyClosed = true; - } - } - } } @SuppressWarnings("deprecation") class Bee { - private String name; - Bee(String nm) { name = nm; } + private String id; + Bee(String name) { id = name; } // Observe openings: - public Observer openObserver() { - return (ob, a) -> System.out.println( - "Bee " + name + "'s breakfast time!"); - } + public final Observer whenOpened = (ob, a) -> + System.out.println( + "Bee " + id + "'s breakfast time!"); // Observe closings: - public Observer closeObserver() { - return (ob, a) -> System.out.println( - "Bee " + name + "'s bed time!"); - } + public final Observer whenClosed = (ob, a) -> + System.out.println( + "Bee " + id + "'s bed time!"); } @SuppressWarnings("deprecation") class Hummingbird { - private String name; - Hummingbird(String nm) { name = nm; } - public Observer openObserver() { - return (ob, a) -> System.out.println( - "Hummingbird " + name + - "'s breakfast time!"); - } - public Observer closeObserver() { - return (ob, a) -> System.out.println( - "Hummingbird " + name + "'s bed time!"); - } + private String id; + Hummingbird(String name) { id = name; } + public final Observer whenOpened = (ob, a) -> + System.out.println("Hummingbird " + + id + "'s breakfast time!"); + public final Observer whenClosed = (ob, a) -> + System.out.println("Hummingbird " + + id + "'s bed time!"); } public class ObservedFlower { @@ -85,39 +78,46 @@ public class ObservedFlower { Hummingbird ha = new Hummingbird("A"), hb = new Hummingbird("B"); - f.opening.addObserver(ha.openObserver()); - f.opening.addObserver(hb.openObserver()); - f.opening.addObserver(ba.openObserver()); - f.opening.addObserver(bb.openObserver()); - f.closing.addObserver(ha.closeObserver()); - f.closing.addObserver(hb.closeObserver()); - f.closing.addObserver(ba.closeObserver()); - f.closing.addObserver(bb.closeObserver()); - // Hummingbird B decides to sleep in: - f.opening.deleteObserver(hb.openObserver()); + f.opening.addObserver(ha.whenOpened); + f.opening.addObserver(hb.whenOpened); + f.opening.addObserver(ba.whenOpened); + f.opening.addObserver(bb.whenOpened); + f.closing.addObserver(ha.whenClosed); + f.closing.addObserver(hb.whenClosed); + f.closing.addObserver(ba.whenClosed); + f.closing.addObserver(bb.whenClosed); + // Hummingbird B decides to sleep in. + // Removing whenOpened stops open updates. + f.opening.deleteObserver(hb.whenOpened); // A change that interests observers: f.open(); - f.open(); // It's already open, no change. - // Bee A doesn't want to go to bed: - f.closing.deleteObserver(ba.closeObserver()); + f.open(); // No effect: it's already open. + System.out.println("---------------"); + // Bee A doesn't want to go to bed. + // Removing whenClosed stops close updates. + f.closing.deleteObserver(ba.whenClosed); f.close(); - f.close(); // It's already closed; no change + System.out.println("+++++++++++++++"); + f.close(); // No effect: it's already closed. + System.out.println("==============="); f.opening.deleteObservers(); - f.open(); - f.close(); + f.open(); // No observers to update. + System.out.println("###############"); + f.close(); // Close observers are still there. } } /* Output: Bee B's breakfast time! Bee A's breakfast time! -Hummingbird B's breakfast time! Hummingbird A's breakfast time! +--------------- Bee B's bed time! -Bee A's bed time! Hummingbird B's bed time! Hummingbird A's bed time! ++++++++++++++++ +=============== +############### Bee B's bed time! -Bee A's bed time! Hummingbird B's bed time! Hummingbird A's bed time! */ diff --git a/patterns/recyclea/RecycleA.java b/patterns/recyclea/RecycleA.java index 184bc4b3..483e700e 100644 --- a/patterns/recyclea/RecycleA.java +++ b/patterns/recyclea/RecycleA.java @@ -2,72 +2,25 @@ // (c)2021 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. -// Recycling with RTTI +// Recycling with reflection. // {java patterns.recyclea.RecycleA} package patterns.recyclea; import java.util.*; import java.util.function.*; import java.util.stream.*; +import patterns.trash.*; -abstract class Trash { - double weight; - Trash(double wt) { weight = wt; } - abstract double value(); - // Sums the value of Trash in a bin: - private static double val; - static void sumValue(List bin) { - val = 0.0f; - bin.forEach( t -> { - // Polymorphism in action: - val += t.weight * t.value(); - System.out.println( - "weight of " + - // Using RTTI to get type - // information about the class: - t.getClass().getSimpleName() + - " = " + t.weight); - }); - System.out.println("Total value = " + val); - } -} - -class Aluminum extends Trash { - static double val = 1.67f; - Aluminum(double wt) { super(wt); } - @Override double value() { return val; } - static void value(double newval) { - val = newval; - } -} - -class Paper extends Trash { - static double val = 0.10f; - Paper(double wt) { super(wt); } - @Override double value() { return val; } - static void value(double newval) { - val = newval; - } -} - -class Glass extends Trash { - static double val = 0.23f; - Glass(double wt) { super(wt); } - @Override double value() { return val; } - static void value(double newval) { - val = newval; - } -} - -class TrashFactory { - static List> ttypes = +class SimpleFactory { + static final + List> constructors = Arrays.asList( Aluminum::new, Paper::new, Glass::new); - static final int SZ = ttypes.size(); + static final int SIZE = constructors.size(); private static SplittableRandom rand = - new SplittableRandom(47); - public static Trash newTrash() { - return ttypes - .get(rand.nextInt(SZ)) + new SplittableRandom(42); + public static Trash random() { + return constructors + .get(rand.nextInt(SIZE)) .apply(rand.nextDouble()); } } @@ -75,50 +28,37 @@ class TrashFactory { public class RecycleA { public static void main(String[] args) { List bin = - Stream.generate(TrashFactory::newTrash) - .limit(25) + Stream.generate(SimpleFactory::random) + .limit(10) .collect(Collectors.toList()); - List glassBin = new ArrayList<>(); - List paperBin = new ArrayList<>(); - List alBin = new ArrayList<>(); - // Sort the Trash: - bin.forEach( t -> { - // RTTI to discover Trash type: - if(t instanceof Aluminum) - alBin.add((Aluminum)t); - if(t instanceof Paper) - paperBin.add((Paper)t); - if(t instanceof Glass) - glassBin.add((Glass)t); - }); - Trash.sumValue(alBin); - Trash.sumValue(paperBin); - Trash.sumValue(glassBin); - Trash.sumValue(bin); + Bins bins = new Bins(bin); + bins.show(); } } -/* Output: (First and Last 11 Lines) -weight of Aluminum = 0.2893030122276371 -weight of Aluminum = 0.1970234961398979 -weight of Aluminum = 0.36295525806274787 -weight of Aluminum = 0.4825532324565849 -weight of Aluminum = 0.8036398273294586 -weight of Aluminum = 0.510430896154935 -weight of Aluminum = 0.6703377164093444 -weight of Aluminum = 0.41477933066243455 -weight of Aluminum = 0.3603022312124007 -weight of Aluminum = 0.43690089841661006 -weight of Aluminum = 0.6708820087907101 -...________...________...________...________... -weight of Aluminum = 0.41477933066243455 -weight of Aluminum = 0.3603022312124007 -weight of Aluminum = 0.43690089841661006 -weight of Glass = 0.5999637765664924 -weight of Glass = 0.7748836191212746 -weight of Paper = 0.5735994548427199 -weight of Glass = 0.5362827750851034 -weight of Aluminum = 0.6708820087907101 -weight of Paper = 0.8370669795210507 -weight of Glass = 0.3397919679731668 -Total value = 9.90671597531968 +/* Output: +Aluminum weight: 0.34 * price: 1.67 = 0.57 +Aluminum weight: 0.62 * price: 1.67 = 1.03 +Aluminum weight: 0.49 * price: 1.67 = 0.82 +Aluminum weight: 0.50 * price: 1.67 = 0.83 +Total Aluminum value = 3.26 +Paper weight: 0.69 * price: 0.10 = 0.07 +Total Paper value = 0.07 +Glass weight: 0.16 * price: 0.23 = 0.04 +Glass weight: 0.87 * price: 0.23 = 0.20 +Glass weight: 0.80 * price: 0.23 = 0.18 +Glass weight: 0.52 * price: 0.23 = 0.12 +Glass weight: 0.20 * price: 0.23 = 0.05 +Total Glass value = 0.59 +Total Cardboard value = 0.00 +Glass weight: 0.16 * price: 0.23 = 0.04 +Aluminum weight: 0.34 * price: 1.67 = 0.57 +Glass weight: 0.87 * price: 0.23 = 0.20 +Glass weight: 0.80 * price: 0.23 = 0.18 +Aluminum weight: 0.62 * price: 1.67 = 1.03 +Aluminum weight: 0.49 * price: 1.67 = 0.82 +Glass weight: 0.52 * price: 0.23 = 0.12 +Glass weight: 0.20 * price: 0.23 = 0.05 +Aluminum weight: 0.50 * price: 1.67 = 0.83 +Paper weight: 0.69 * price: 0.10 = 0.07 +Total Trash value = 3.91 */ diff --git a/patterns/recycleb/RecycleB.java b/patterns/recycleb/RecycleB.java index 25cb6d90..b65f1db3 100644 --- a/patterns/recycleb/RecycleB.java +++ b/patterns/recycleb/RecycleB.java @@ -10,47 +10,43 @@ import java.util.*; public class RecycleB { public static void main(String[] args) { List bin = new ArrayList<>(); - // Fill up the Trash bin: ParseTrash.fillBin("trash", bin); - List glassBin = new ArrayList<>(); - List paperBin = new ArrayList<>(); - List alBin = new ArrayList<>(); - // Sort the Trash: - bin.forEach( t -> { - // RTTI to discover Trash type: - if(t instanceof Aluminum) - alBin.add((Aluminum)t); - if(t instanceof Paper) - paperBin.add((Paper)t); - if(t instanceof Glass) - glassBin.add((Glass)t); - }); - Trash.sumValue(alBin); - Trash.sumValue(paperBin); - Trash.sumValue(glassBin); - Trash.sumValue(bin); + Bins bins = new Bins(bin); + bins.show(); } } -/* Output: (First and Last 10 Lines) -Loading patterns.trash.Glass +/* Output: +Loading patterns.trash.Cardboard Loading patterns.trash.Paper Loading patterns.trash.Aluminum -Loading patterns.trash.Cardboard -weight of patterns.trash.Aluminum = 89.0 -weight of patterns.trash.Aluminum = 76.0 -weight of patterns.trash.Aluminum = 25.0 -weight of patterns.trash.Aluminum = 34.0 -weight of patterns.trash.Aluminum = 27.0 -weight of patterns.trash.Aluminum = 18.0 -...________...________...________...________... -weight of patterns.trash.Aluminum = 93.0 -weight of patterns.trash.Glass = 93.0 -weight of patterns.trash.Paper = 80.0 -weight of patterns.trash.Glass = 36.0 -weight of patterns.trash.Glass = 12.0 -weight of patterns.trash.Glass = 60.0 -weight of patterns.trash.Paper = 66.0 -weight of patterns.trash.Aluminum = 36.0 -weight of patterns.trash.Cardboard = 22.0 -Total value = 1086.0599818825722 +Loading patterns.trash.Glass +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Total Aluminum value = 13.19 +Paper weight: 8.00 * price: 0.10 = 0.80 +Paper weight: 6.60 * price: 0.10 = 0.66 +Paper weight: 9.10 * price: 0.10 = 0.91 +Total Paper value = 2.37 +Glass weight: 5.40 * price: 0.23 = 1.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Glass value = 3.06 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Total Cardboard value = 0.86 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Paper weight: 8.00 * price: 0.10 = 0.80 +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Glass weight: 5.40 * price: 0.23 = 1.24 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Paper weight: 6.60 * price: 0.10 = 0.66 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Paper weight: 9.10 * price: 0.10 = 0.91 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Trash value = 19.48 */ diff --git a/patterns/recyclec/RecycleC.java b/patterns/recyclec/RecycleC.java index 75dd4e5e..611c3090 100644 --- a/patterns/recyclec/RecycleC.java +++ b/patterns/recyclec/RecycleC.java @@ -2,83 +2,105 @@ // (c)2021 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. -// Adding more objects to the recycling problem // {java patterns.recyclec.RecycleC} package patterns.recyclec; import patterns.trash.*; import java.util.*; -// A List that admits only the right type: -class Tbin extends ArrayList { - Class binType; - Tbin(Class type) { - binType = type; +// A List that only admits the right type: +class +TrashBin extends ArrayList { + final Class binType; + TrashBin(Class binType) { + this.binType = binType; } @SuppressWarnings("unchecked") boolean grab(Trash t) { - // Comparing class types: + // Compare class types: if(t.getClass().equals(binType)) { - add((T)t); // Downcast to this TBin's type - return true; // Object grabbed + add((T)t); // Downcast to this TrashBin type + return true; // Trash grabbed } - return false; // Object not grabbed + return false; // Trash not grabbed } } -class TbinList -extends ArrayList> { // [1] - boolean sort(T t) { - for(Tbin ts : this) +class TrashBinList +extends ArrayList> { // [1] + @SuppressWarnings("unchecked") + public TrashBinList(Class... types) { + for(Class type : types) + add(new TrashBin<>(type)); + } + public boolean sort(T t) { + for(TrashBin ts : this) if(ts.grab(t)) return true; - return false; // bin not found for t + return false; // TrashBin not found for t } - void sortBin(Tbin bin) { // [2] - for(T aBin : bin) - if(!sort(aBin)) - System.err.println("Bin not found"); + public void sortBin(TrashBin bin) { // [2] + for(T trash : bin) + if(!sort(trash)) + throw new RuntimeException( + "Bin not found for " + trash); + } + public void show() { + for(TrashBin tbin : this) { + String typeName = tbin.binType.getSimpleName(); + TrashValue.sum(tbin, typeName); + } } } public class RecycleC { - static Tbin bin = new Tbin<>(Trash.class); public static void main(String[] args) { - // Fill up the Trash bin: + TrashBin bin = + new TrashBin<>(Trash.class); ParseTrash.fillBin("trash", bin); - - TbinList trashBins = new TbinList<>(); - trashBins.add(new Tbin<>(Aluminum.class)); - trashBins.add(new Tbin<>(Paper.class)); - trashBins.add(new Tbin<>(Glass.class)); - // add one line here: // [3] - trashBins.add(new Tbin<>(Cardboard.class)); - + @SuppressWarnings("unchecked") + TrashBinList trashBins = + new TrashBinList<>( + Aluminum.class, Paper.class, Glass.class, + // Add one item: + Cardboard.class // [3] + ); trashBins.sortBin(bin); // [4] - - trashBins.forEach(Trash::sumValue); - Trash.sumValue(bin); + trashBins.show(); + TrashValue.sum(bin, "Trash"); } } -/* Output: (First and Last 10 Lines) -Loading patterns.trash.Glass +/* Output: +Loading patterns.trash.Cardboard Loading patterns.trash.Paper Loading patterns.trash.Aluminum -Loading patterns.trash.Cardboard -weight of patterns.trash.Aluminum = 89.0 -weight of patterns.trash.Aluminum = 76.0 -weight of patterns.trash.Aluminum = 25.0 -weight of patterns.trash.Aluminum = 34.0 -weight of patterns.trash.Aluminum = 27.0 -weight of patterns.trash.Aluminum = 18.0 -...________...________...________...________... -weight of patterns.trash.Aluminum = 93.0 -weight of patterns.trash.Glass = 93.0 -weight of patterns.trash.Paper = 80.0 -weight of patterns.trash.Glass = 36.0 -weight of patterns.trash.Glass = 12.0 -weight of patterns.trash.Glass = 60.0 -weight of patterns.trash.Paper = 66.0 -weight of patterns.trash.Aluminum = 36.0 -weight of patterns.trash.Cardboard = 22.0 -Total value = 1086.0599818825722 +Loading patterns.trash.Glass +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Total Aluminum value = 13.19 +Paper weight: 8.00 * price: 0.10 = 0.80 +Paper weight: 6.60 * price: 0.10 = 0.66 +Paper weight: 9.10 * price: 0.10 = 0.91 +Total Paper value = 2.37 +Glass weight: 5.40 * price: 0.23 = 1.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Glass value = 3.06 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Total Cardboard value = 0.86 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Paper weight: 8.00 * price: 0.10 = 0.80 +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Glass weight: 5.40 * price: 0.23 = 1.24 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Paper weight: 6.60 * price: 0.10 = 0.66 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Paper weight: 9.10 * price: 0.10 = 0.91 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Trash value = 19.48 */ diff --git a/patterns/state/StateMachineDemo.java b/patterns/state/StateMachineDemo.java index 0046a684..1b9630a9 100644 --- a/patterns/state/StateMachineDemo.java +++ b/patterns/state/StateMachineDemo.java @@ -2,9 +2,10 @@ // (c)2021 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. -// The StateMachine pattern and Template method +// The State Machine pattern. // {java patterns.state.StateMachineDemo} package patterns.state; +import java.util.*; import onjava.Nap; interface State { @@ -16,7 +17,7 @@ abstract class StateMachine { protected abstract boolean changeState(); // Template method: protected final void runAll() { - while(changeState()) // Customizable + while(changeState()) currentState.run(); } } @@ -46,20 +47,19 @@ class Rinse implements State { class Washer extends StateMachine { private int i = 0; - // The state table: - private State[] states = { - new Wash(), new Spin(), - new Rinse(), new Spin(), - }; + private Iterator states = + Arrays.asList( + new Wash(), new Spin(), + new Rinse(), new Spin() + ).iterator(); Washer() { runAll(); } @Override public boolean changeState() { - if(i < states.length) { - // Change the state by setting the - // surrogate reference to a new object: - currentState = states[i++]; - return true; - } else + if(!states.hasNext()) return false; + // Set the surrogate reference + // to a new State object: + currentState = states.next(); + return true; } } diff --git a/patterns/strategy/StrategyPattern.java b/patterns/strategy/StrategyPattern.java index 32f98962..7e06bc1e 100644 --- a/patterns/strategy/StrategyPattern.java +++ b/patterns/strategy/StrategyPattern.java @@ -9,13 +9,14 @@ import java.util.*; // The common strategy base type: class FindMinima { + protected Function, List> algorithm; } -// The various strategies: +// The various strategies, each producing dummy data: class LeastSquares extends FindMinima { LeastSquares() { - // Line is a sequence of points (Dummy data): + // Line is a sequence of points: algorithm = (line) -> Arrays.asList(1.1, 2.2); } } @@ -35,8 +36,8 @@ class Bisection extends FindMinima { // The "Context" controls the strategy: class MinimaSolver { private FindMinima strategy; - MinimaSolver(FindMinima strat) { - strategy = strat; + MinimaSolver(FindMinima strategy) { + this.strategy = strategy; } List minima(List line) { return strategy.algorithm.apply(line); @@ -54,11 +55,14 @@ public class StrategyPattern { 1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0 ); System.out.println(solver.minima(line)); + solver.changeAlgorithm(new Perturbation()); + System.out.println(solver.minima(line)); solver.changeAlgorithm(new Bisection()); System.out.println(solver.minima(line)); } } /* Output: [1.1, 2.2] +[3.3, 4.4] [5.5, 6.6] */ diff --git a/patterns/strategy/StrategyPattern2.java b/patterns/strategy/StrategyPattern2.java index d63c6e71..640b1c05 100644 --- a/patterns/strategy/StrategyPattern2.java +++ b/patterns/strategy/StrategyPattern2.java @@ -9,6 +9,7 @@ import java.util.*; // "Context" is now incorporated: class FindMinima2 { + private Function, List> algorithm; FindMinima2() { leastSquares(); } // default // The various strategies: @@ -33,11 +34,14 @@ public class StrategyPattern2 { 1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0 ); System.out.println(solver.minima(line)); + solver.perturbation(); + System.out.println(solver.minima(line)); solver.bisection(); System.out.println(solver.minima(line)); } } /* Output: [1.1, 2.2] +[3.3, 4.4] [5.5, 6.6] */ diff --git a/patterns/trash/Aluminum.java b/patterns/trash/Aluminum.java index 085d7047..620cd5e6 100644 --- a/patterns/trash/Aluminum.java +++ b/patterns/trash/Aluminum.java @@ -5,10 +5,12 @@ package patterns.trash; public class Aluminum extends Trash { - private static double val = 1.67f; public Aluminum(double wt) { super(wt); } - @Override public double value() { return val; } - public static void value(double newVal) { - val = newVal; + @Override public double price() { + return Price.ALUMINUM; + } + // Ignore for now; to be used later: + @Override public void accept(Visitor v) { + v.visit(this); } } diff --git a/patterns/trash/Bins.java b/patterns/trash/Bins.java new file mode 100644 index 00000000..304708b3 --- /dev/null +++ b/patterns/trash/Bins.java @@ -0,0 +1,35 @@ +// patterns/trash/Bins.java +// (c)2021 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.trash; +import java.util.*; + +public class Bins { + final List bin; + final List aluminum = new ArrayList<>(); + final List paper = new ArrayList<>(); + final List glass = new ArrayList<>(); + final List cardboard = new ArrayList<>(); + public Bins(List source) { + bin = new ArrayList<>(source); // Copy + bin.forEach( t -> { + // Use reflection to discover Trash type: + if(t instanceof Aluminum) + aluminum.add((Aluminum)t); + if(t instanceof Paper) + paper.add((Paper)t); + if(t instanceof Glass) + glass.add((Glass)t); + if(t instanceof Cardboard) + cardboard.add((Cardboard)t); + }); + } + public void show() { + TrashValue.sum(aluminum, "Aluminum"); + TrashValue.sum(paper, "Paper"); + TrashValue.sum(glass, "Glass"); + TrashValue.sum(cardboard, "Cardboard"); + TrashValue.sum(bin, "Trash"); + } +} diff --git a/patterns/trash/Cardboard.java b/patterns/trash/Cardboard.java index f1998630..923f6647 100644 --- a/patterns/trash/Cardboard.java +++ b/patterns/trash/Cardboard.java @@ -5,10 +5,12 @@ package patterns.trash; public class Cardboard extends Trash { - private static double val = 0.23f; public Cardboard(double wt) { super(wt); } - @Override public double value() { return val; } - public static void value(double newVal) { - val = newVal; + @Override public double price() { + return Price.CARDBOARD; + } + // Ignore for now; to be used later: + @Override public void accept(Visitor v) { + v.visit(this); } } diff --git a/patterns/trash/ClassToListOfTrashMap.java b/patterns/trash/ClassToListOfTrashMap.java new file mode 100644 index 00000000..2095cdd4 --- /dev/null +++ b/patterns/trash/ClassToListOfTrashMap.java @@ -0,0 +1,20 @@ +// patterns/trash/ClassToListOfTrashMap.java +// (c)2021 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. +// Display a Map>. +package patterns.trash; +import java.util.*; + +public class ClassToListOfTrashMap { + public static void + show(Map> map) { + map.values().forEach( bin -> { + String typeName = "Trash"; + if(!bin.isEmpty()) + typeName = + bin.get(0).getClass().getSimpleName(); + TrashValue.sum(bin, typeName); + }); + } +} diff --git a/patterns/trash/DynaFactory.java b/patterns/trash/DynaFactory.java new file mode 100644 index 00000000..1331e9ba --- /dev/null +++ b/patterns/trash/DynaFactory.java @@ -0,0 +1,44 @@ +// patterns/trash/DynaFactory.java +// (c)2021 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. +// Dynamic discovery of Trash types. +package patterns.trash; +import java.util.*; +import java.util.function.*; +import java.lang.reflect.*; + +public class DynaFactory { + private Map constructors = + new HashMap<>(); + private String packageName; + public DynaFactory(String packageName) { + this.packageName = packageName; + } + @SuppressWarnings("unchecked") + public + T create(TrashInfo info) { + try { + String typename = + "patterns." + packageName + "." + info.type; + return (T)constructors.computeIfAbsent( + typename, this::findConstructor + ).newInstance(info.data); + } catch(Exception e) { + throw new RuntimeException( + "Cannot create() Trash: " + info, e); + } + } + private + Constructor findConstructor(String typename) { + try { + System.out.println("Loading " + typename); + return Class.forName(typename) + .getConstructor(double.class); + } catch(Exception e) { + throw new RuntimeException( + "Trash(double) Constructor Not Found: " + + typename, e); + } + } +} diff --git a/patterns/trash/Fillable.java b/patterns/trash/Fillable.java index 6c00f6ea..329c92bd 100644 --- a/patterns/trash/Fillable.java +++ b/patterns/trash/Fillable.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Any object that can be filled with Trash +// Any object that can be filled with Trash. package patterns.trash; public interface Fillable { diff --git a/patterns/trash/FillableList.java b/patterns/trash/FillableList.java index 05d83ff6..8a0bfa74 100644 --- a/patterns/trash/FillableList.java +++ b/patterns/trash/FillableList.java @@ -2,15 +2,17 @@ // (c)2021 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. -// Adapter that makes a List Fillable +// Adapter that makes a List Fillable. package patterns.trash; import java.util.*; public class FillableList implements Fillable { - private List v; - public FillableList(List vv) { - v = vv; + private List list; + public FillableList(List list) { + this.list = list; + } + @Override public void addTrash(T t) { + list.add(t); } - @Override public void addTrash(T t) { v.add(t); } } diff --git a/patterns/trash/Glass.java b/patterns/trash/Glass.java index faf73762..df08c996 100644 --- a/patterns/trash/Glass.java +++ b/patterns/trash/Glass.java @@ -5,10 +5,12 @@ package patterns.trash; public class Glass extends Trash { - private static double val = 0.23f; public Glass(double wt) { super(wt); } - @Override public double value() { return val; } - public static void value(double newVal) { - val = newVal; + @Override public double price() { + return Price.GLASS; + } + // Ignore for now; to be used later: + @Override public void accept(Visitor v) { + v.visit(this); } } diff --git a/patterns/trash/GroupingBy.java b/patterns/trash/GroupingBy.java new file mode 100644 index 00000000..6c8f8bc6 --- /dev/null +++ b/patterns/trash/GroupingBy.java @@ -0,0 +1,41 @@ +// patterns/trash/GroupingBy.java +// (c)2021 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. +// {java patterns.trash.GroupingBy} +package patterns.trash; +import java.util.*; +import java.util.stream.*; + +public class GroupingBy { + public static void main(String[] args) { + List bin = new ArrayList<>(); + ParseTrash.fillBin("trash", bin); + Map> m = + bin.stream().collect( + Collectors.groupingBy(Object::getClass)); + ClassToListOfTrashMap.show(m); + } +} +/* Output: +Loading patterns.trash.Cardboard +Loading patterns.trash.Paper +Loading patterns.trash.Aluminum +Loading patterns.trash.Glass +Glass weight: 5.40 * price: 0.23 = 1.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Glass value = 3.06 +Paper weight: 8.00 * price: 0.10 = 0.80 +Paper weight: 6.60 * price: 0.10 = 0.66 +Paper weight: 9.10 * price: 0.10 = 0.91 +Total Paper value = 2.37 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Total Cardboard value = 0.86 +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Total Aluminum value = 13.19 +*/ diff --git a/patterns/trash/Paper.java b/patterns/trash/Paper.java index dd79e8ea..5a47cea1 100644 --- a/patterns/trash/Paper.java +++ b/patterns/trash/Paper.java @@ -5,10 +5,12 @@ package patterns.trash; public class Paper extends Trash { - private static double val = 0.10f; public Paper(double wt) { super(wt); } - @Override public double value() { return val; } - public static void value(double newVal) { - val = newVal; + @Override public double price() { + return Price.PAPER; + } + // Ignore for now; to be used later: + @Override public void accept(Visitor v) { + v.visit(this); } } diff --git a/patterns/trash/ParseTrash.java b/patterns/trash/ParseTrash.java index 7e69fea3..0ea5df24 100644 --- a/patterns/trash/ParseTrash.java +++ b/patterns/trash/ParseTrash.java @@ -2,8 +2,8 @@ // (c)2021 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. -// Open a file and parse its contents into -// Trash objects, placing each into a List +// Opens a file and parses its contents into +// Trash objects, placing each into a Fillable. // {java patterns.trash.ParseTrash} package patterns.trash; import java.util.*; @@ -13,79 +13,56 @@ import java.nio.file.*; import java.nio.file.Files; public class ParseTrash { + public static String source = "Trash.dat"; public static void - fillBin(String pckg, Fillable bin) { + fillBin(String packageName, Fillable bin) { + DynaFactory factory = + new DynaFactory(packageName); try { - Files.lines(Paths.get("trash", "Trash.dat")) - // Remove empty lines and comment lines: + Files.lines(Paths.get("trash", source)) + // Remove comments and empty lines: .filter(line -> line.trim().length() != 0) .filter(line -> !line.startsWith("//")) - .forEach( line -> { - String type = "patterns." + pckg + "." + - line.substring( + .forEach(line -> { + String type = line.substring( 0, line.indexOf(':')).trim(); double weight = Double.valueOf( line.substring(line.indexOf(':') + 1) .trim()); - bin.addTrash(Trash.factory( - new Trash.Info(type, weight))); + bin.addTrash(factory.create( + new TrashInfo(type, weight))); }); - } catch(IOException | - NumberFormatException | - Trash.TrashClassNotFoundException | - Trash.CannotCreateTrashException e) { + } catch(IOException | NumberFormatException e) { throw new RuntimeException(e); } } - // Special case to handle List: + // Special case to handle a List: public static void - fillBin(String pckg, List bin) { - fillBin(pckg, new FillableList<>(bin)); + fillBin(String packageName, List bin) { + fillBin(packageName, new FillableList<>(bin)); } // Basic test: public static void main(String[] args) { - List t = new ArrayList<>(); - fillBin("trash", t); - t.forEach(System.out::println); + List bin = new ArrayList<>(); + fillBin("trash", bin); + bin.forEach(System.out::println); } } /* Output: -Loading patterns.trash.Glass +Loading patterns.trash.Cardboard Loading patterns.trash.Paper Loading patterns.trash.Aluminum -Loading patterns.trash.Cardboard -patterns.trash.Glass w:54.0 v:0.23 -patterns.trash.Paper w:22.0 v:0.10 -patterns.trash.Paper w:11.0 v:0.10 -patterns.trash.Glass w:17.0 v:0.23 -patterns.trash.Aluminum w:89.0 v:1.67 -patterns.trash.Paper w:88.0 v:0.10 -patterns.trash.Aluminum w:76.0 v:1.67 -patterns.trash.Cardboard w:96.0 v:0.23 -patterns.trash.Aluminum w:25.0 v:1.67 -patterns.trash.Aluminum w:34.0 v:1.67 -patterns.trash.Glass w:11.0 v:0.23 -patterns.trash.Glass w:68.0 v:0.23 -patterns.trash.Glass w:43.0 v:0.23 -patterns.trash.Aluminum w:27.0 v:1.67 -patterns.trash.Cardboard w:44.0 v:0.23 -patterns.trash.Aluminum w:18.0 v:1.67 -patterns.trash.Paper w:91.0 v:0.10 -patterns.trash.Glass w:63.0 v:0.23 -patterns.trash.Glass w:50.0 v:0.23 -patterns.trash.Glass w:80.0 v:0.23 -patterns.trash.Aluminum w:81.0 v:1.67 -patterns.trash.Cardboard w:12.0 v:0.23 -patterns.trash.Glass w:12.0 v:0.23 -patterns.trash.Glass w:54.0 v:0.23 -patterns.trash.Aluminum w:36.0 v:1.67 -patterns.trash.Aluminum w:93.0 v:1.67 -patterns.trash.Glass w:93.0 v:0.23 -patterns.trash.Paper w:80.0 v:0.10 -patterns.trash.Glass w:36.0 v:0.23 -patterns.trash.Glass w:12.0 v:0.23 -patterns.trash.Glass w:60.0 v:0.23 -patterns.trash.Paper w:66.0 v:0.10 -patterns.trash.Aluminum w:36.0 v:1.67 -patterns.trash.Cardboard w:22.0 v:0.23 +Loading patterns.trash.Glass +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Paper weight: 8.00 * price: 0.10 = 0.80 +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Glass weight: 5.40 * price: 0.23 = 1.24 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Paper weight: 6.60 * price: 0.10 = 0.66 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Paper weight: 9.10 * price: 0.10 = 0.91 +Glass weight: 3.60 * price: 0.23 = 0.83 */ diff --git a/patterns/trash/Price.java b/patterns/trash/Price.java new file mode 100644 index 00000000..9aa07181 --- /dev/null +++ b/patterns/trash/Price.java @@ -0,0 +1,13 @@ +// patterns/trash/Price.java +// (c)2021 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.trash; + +public interface Price { + double + ALUMINUM = 1.67, + PAPER = 0.10, + GLASS = 0.23, + CARDBOARD = 0.11; +} diff --git a/patterns/trash/Trash.dat b/patterns/trash/Trash.dat index 436a7a71..13bd6187 100644 --- a/patterns/trash/Trash.dat +++ b/patterns/trash/Trash.dat @@ -1,35 +1,13 @@ // patterns/trash/Trash.dat -Glass:54 -Paper:22 -Paper:11 -Glass:17 -Aluminum:89 -Paper:88 -Aluminum:76 -Cardboard:96 -Aluminum:25 -Aluminum:34 -Glass:11 -Glass:68 -Glass:43 -Aluminum:27 -Cardboard:44 -Aluminum:18 -Paper:91 -Glass:63 -Glass:50 -Glass:80 -Aluminum:81 -Cardboard:12 -Glass:12 -Glass:54 -Aluminum:36 -Aluminum:93 -Glass:93 -Paper:80 -Glass:36 -Glass:12 -Glass:60 -Paper:66 -Aluminum:36 -Cardboard:22 +Cardboard:4.4 +Paper:8.0 +Aluminum:1.8 +Glass:5.4 +Aluminum:3.4 +Cardboard:2.2 +Glass:4.3 +Cardboard:1.2 +Paper:6.6 +Aluminum:2.7 +Paper:9.1 +Glass:3.6 diff --git a/patterns/trash/Trash.java b/patterns/trash/Trash.java index 4920e0ba..9f9feca8 100644 --- a/patterns/trash/Trash.java +++ b/patterns/trash/Trash.java @@ -2,90 +2,21 @@ // (c)2021 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. -// Base class for Trash recycling examples +// Base class for Trash recycling examples. package patterns.trash; -import java.util.*; -import java.lang.reflect.*; public abstract class Trash { - private double weight; - public Trash(double wt) { weight = wt; } - public Trash() {} - public abstract double value(); - public double weight() { return weight; } - // Sums the value of Trash in a bin: - static double val; - public static - void sumValue(List bin) { - val = 0.0f; - bin.forEach( t -> { - val += t.weight() * t.value(); - System.out.println("weight of " + - // RTTI gets type information - // about the class: - t.getClass().getName() + - " = " + t.weight()); - }); - System.out.println("Total value = " + val); + public final double weight; + public Trash(double weight) { + this.weight = weight; } + public abstract double price(); @Override public String toString() { - // Print correct subclass name: - return getClass().getName() + - " w:" + weight() + " v:" + - String.format("%.2f", value()); - } - // Remainder of class supports dynamic creation: - public static class CannotCreateTrashException - extends RuntimeException { - public CannotCreateTrashException(Exception why) { - super(why); - } - } - public static class TrashClassNotFoundException - extends RuntimeException { - public TrashClassNotFoundException(Exception why) { - super(why); - } - } - public static class Info { - public String id; - public double data; - public Info(String name, double data) { - id = name; - this.data = data; - } - } - private static List trashTypes = - new ArrayList<>(); - @SuppressWarnings("unchecked") - public static T factory(Info info) { - for(Class trashType : trashTypes) { - // Determine the type and create one: - if(trashType.getName().contains(info.id)) { - try { - // Get the dynamic constructor method - // that takes a double argument: - Constructor ctor = - trashType.getConstructor(double.class); - // Call the constructor to create a - // new object: - return - (T)ctor.newInstance(info.data); - } catch(Exception e) { - throw new CannotCreateTrashException(e); - } - } - } - // The necessary Class was not in the list. Try to - // load it, but it must be in your class path! - try { - System.out.println("Loading " + info.id); - trashTypes.add(Class.forName(info.id)); - } catch(Exception e) { - throw new TrashClassNotFoundException(e); - } - // Loaded successfully. Recursive call - // should work this time: - return factory(info); + return String.format( + "%s weight: %.2f * price: %.2f = %.2f", + getClass().getSimpleName(), + weight, price(), weight * price()); } + // Ignore for now; to be used later: + public abstract void accept(Visitor v); } diff --git a/patterns/trash/TrashInfo.java b/patterns/trash/TrashInfo.java new file mode 100644 index 00000000..9adc6788 --- /dev/null +++ b/patterns/trash/TrashInfo.java @@ -0,0 +1,18 @@ +// patterns/trash/TrashInfo.java +// (c)2021 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. +// Messenger class carrying Trash creation data. +package patterns.trash; + +public class TrashInfo { + public final String type; + public final double data; + public TrashInfo(String type, double data) { + this.type = type; + this.data = data; + } + @Override public String toString() { + return "TrashInfo(" + type + ", " + data + ")"; + } +} diff --git a/patterns/trash/TrashValue.java b/patterns/trash/TrashValue.java new file mode 100644 index 00000000..0ca9caaf --- /dev/null +++ b/patterns/trash/TrashValue.java @@ -0,0 +1,21 @@ +// patterns/trash/TrashValue.java +// (c)2021 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. +// Sums the value of Trash in a bin. +package patterns.trash; +import java.util.*; + +public class TrashValue { + private static double total; + public static void + sum(List bin, String type) { + total = 0.0; + bin.forEach( t -> { + System.out.println(t); + total += t.weight * t.price(); + }); + System.out.printf( + "Total %s value = %.2f%n", type, total); + } +} diff --git a/patterns/trash/TrashVisitor.java b/patterns/trash/TrashVisitor.java new file mode 100644 index 00000000..3f8431e1 --- /dev/null +++ b/patterns/trash/TrashVisitor.java @@ -0,0 +1,104 @@ +// patterns/trash/TrashVisitor.java +// (c)2021 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. +// Related algorithms packaged in +// each implementation of Visitor. +// {java patterns.trash.TrashVisitor} +package patterns.trash; +import java.util.*; + +class PriceVisitor extends Visitor { + public PriceVisitor() { super("price"); } + @Override public void visit(Aluminum al) { + double price = al.weight * al.price(); + show("Aluminum", price); + alTotal += price; + } + @Override public void visit(Paper p) { + double price = p.weight * p.price(); + show("Paper", price); + pTotal += price; + } + @Override public void visit(Glass g) { + double price = g.weight * g.price(); + show("Glass", price); + gTotal += price; + } + @Override public void visit(Cardboard c) { + double price = c.weight * c.price(); + show("Cardboard", price); + cTotal += price; + } +} + +class WeightVisitor extends Visitor { + public WeightVisitor() { super("weight"); } + @Override public void visit(Aluminum al) { + show("Aluminum", al.weight); + alTotal += al.weight; + } + @Override public void visit(Paper p) { + show("Paper", p.weight); + pTotal += p.weight; + } + @Override public void visit(Glass g) { + show("Glass", g.weight); + gTotal += g.weight; + } + @Override public void visit(Cardboard c) { + show("Cardboard", c.weight); + cTotal += c.weight; + } +} + +public class TrashVisitor { + public static void main(String[] args) { + List bin = new ArrayList<>(); + ParseTrash.fillBin("trash", bin); + List visitors = Arrays.asList( + new PriceVisitor(), new WeightVisitor()); + bin.forEach( + trash -> visitors.forEach(trash::accept) + ); + visitors.forEach(Visitor::total); + } +} +/* Output: +Loading patterns.trash.Cardboard +Loading patterns.trash.Paper +Loading patterns.trash.Aluminum +Loading patterns.trash.Glass +Cardboard price: 0.48 +Cardboard weight: 4.40 +Paper price: 0.80 +Paper weight: 8.00 +Aluminum price: 3.01 +Aluminum weight: 1.80 +Glass price: 1.24 +Glass weight: 5.40 +Aluminum price: 5.68 +Aluminum weight: 3.40 +Cardboard price: 0.24 +Cardboard weight: 2.20 +Glass price: 0.99 +Glass weight: 4.30 +Cardboard price: 0.13 +Cardboard weight: 1.20 +Paper price: 0.66 +Paper weight: 6.60 +Aluminum price: 4.51 +Aluminum weight: 2.70 +Paper price: 0.91 +Paper weight: 9.10 +Glass price: 0.83 +Glass weight: 3.60 +Total Aluminum price: 13.19 +Total Paper price: 2.37 +Total Glass price: 3.06 +Total Cardboard price: 0.86 +Total Aluminum weight: 7.90 +Total Paper weight: 23.70 +Total Glass weight: 13.30 +Total Cardboard weight: 7.80 +*/ diff --git a/patterns/trash/TypeMapTrash.java b/patterns/trash/TypeMapTrash.java new file mode 100644 index 00000000..bd04ab1d --- /dev/null +++ b/patterns/trash/TypeMapTrash.java @@ -0,0 +1,52 @@ +// patterns/trash/TypeMapTrash.java +// (c)2021 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. +// Using a Map of Lists and reflection to sort +// Trash into Lists. This is extensible, despite +// the use of reflection. +// {java patterns.trash.TypeMapTrash} +package patterns.trash; +import patterns.TypeMap; + +// Adapter class for ParseTrash.fillBin(): +class TypeMapAdapter implements Fillable { + private TypeMap map; + TypeMapAdapter(TypeMap map) { + this.map = map; + } + @Override + public void addTrash(Trash t) { map.add(t); } +} + +public class TypeMapTrash { + @SuppressWarnings("unchecked") + public static void main(String[] args) { + TypeMap bin = new TypeMap<>(); + ParseTrash.fillBin( + "trash", new TypeMapAdapter(bin)); + ClassToListOfTrashMap.show(bin.map); + } +} +/* Output: +Loading patterns.trash.Cardboard +Loading patterns.trash.Paper +Loading patterns.trash.Aluminum +Loading patterns.trash.Glass +Paper weight: 8.00 * price: 0.10 = 0.80 +Paper weight: 6.60 * price: 0.10 = 0.66 +Paper weight: 9.10 * price: 0.10 = 0.91 +Total Paper value = 2.37 +Glass weight: 5.40 * price: 0.23 = 1.24 +Glass weight: 4.30 * price: 0.23 = 0.99 +Glass weight: 3.60 * price: 0.23 = 0.83 +Total Glass value = 3.06 +Aluminum weight: 1.80 * price: 1.67 = 3.01 +Aluminum weight: 3.40 * price: 1.67 = 5.68 +Aluminum weight: 2.70 * price: 1.67 = 4.51 +Total Aluminum value = 13.19 +Cardboard weight: 4.40 * price: 0.11 = 0.48 +Cardboard weight: 2.20 * price: 0.11 = 0.24 +Cardboard weight: 1.20 * price: 0.11 = 0.13 +Total Cardboard value = 0.86 +*/ diff --git a/patterns/trash/Visitor.java b/patterns/trash/Visitor.java new file mode 100644 index 00000000..34f0fff8 --- /dev/null +++ b/patterns/trash/Visitor.java @@ -0,0 +1,31 @@ +// patterns/trash/Visitor.java +// (c)2021 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. +// The base class for Visitors. +package patterns.trash; + +public abstract class Visitor { + protected double alTotal; // Aluminum + protected double pTotal; // Paper + protected double gTotal; // Glass + protected double cTotal; // Cardboard + protected String descriptor; + protected Visitor(String descriptor) { + this.descriptor = descriptor; + } + protected void show(String type, double value) { + System.out.printf( + "%s %s: %.2f%n", type, descriptor, value); + } + public void total() { + show("Total Aluminum", alTotal); + show("Total Paper", pTotal); + show("Total Glass", gTotal); + show("Total Cardboard", cTotal); + } + abstract void visit(Aluminum a); + abstract void visit(Paper p); + abstract void visit(Glass g); + abstract void visit(Cardboard c); +} diff --git a/patterns/trashvisitor/Aluminum.java b/patterns/trashvisitor/Aluminum.java deleted file mode 100644 index 9bb9c9e4..00000000 --- a/patterns/trashvisitor/Aluminum.java +++ /dev/null @@ -1,15 +0,0 @@ -// patterns/trashvisitor/Aluminum.java -// (c)2021 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. -// Aluminum for the visitor pattern -package patterns.trashvisitor; -import patterns.trash.*; - -public class Aluminum extends patterns.trash.Aluminum - implements Visitable { - public Aluminum(double wt) { super(wt); } - @Override public void accept(Visitor v) { - v.visit(this); - } -} diff --git a/patterns/trashvisitor/Cardboard.java b/patterns/trashvisitor/Cardboard.java deleted file mode 100644 index 50c3b1c9..00000000 --- a/patterns/trashvisitor/Cardboard.java +++ /dev/null @@ -1,15 +0,0 @@ -// patterns/trashvisitor/Cardboard.java -// (c)2021 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. -// Cardboard for the visitor pattern -package patterns.trashvisitor; -import patterns.trash.*; - -public class Cardboard extends patterns.trash.Cardboard - implements Visitable { - public Cardboard(double wt) { super(wt); } - @Override public void accept(Visitor v) { - v.visit(this); - } -} diff --git a/patterns/trashvisitor/Glass.java b/patterns/trashvisitor/Glass.java deleted file mode 100644 index 71f1de03..00000000 --- a/patterns/trashvisitor/Glass.java +++ /dev/null @@ -1,15 +0,0 @@ -// patterns/trashvisitor/Glass.java -// (c)2021 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. -// Glass for the visitor pattern -package patterns.trashvisitor; -import patterns.trash.*; - -public class Glass extends patterns.trash.Glass - implements Visitable { - public Glass(double wt) { super(wt); } - @Override public void accept(Visitor v) { - v.visit(this); - } -} diff --git a/patterns/trashvisitor/Paper.java b/patterns/trashvisitor/Paper.java deleted file mode 100644 index ad209ffd..00000000 --- a/patterns/trashvisitor/Paper.java +++ /dev/null @@ -1,15 +0,0 @@ -// patterns/trashvisitor/Paper.java -// (c)2021 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. -// Paper for the visitor pattern -package patterns.trashvisitor; -import patterns.trash.*; - -public class Paper extends patterns.trash.Paper - implements Visitable { - public Paper(double wt) { super(wt); } - @Override public void accept(Visitor v) { - v.visit(this); - } -} diff --git a/patterns/trashvisitor/TrashVisitor.java b/patterns/trashvisitor/TrashVisitor.java deleted file mode 100644 index c794d82f..00000000 --- a/patterns/trashvisitor/TrashVisitor.java +++ /dev/null @@ -1,117 +0,0 @@ -// patterns/trashvisitor/TrashVisitor.java -// (c)2021 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. -// {java patterns.trashvisitor.TrashVisitor} -package patterns.trashvisitor; -import patterns.trash.*; -import java.util.*; - -// Specific group of algorithms packaged -// in each implementation of Visitor: -class PriceVisitor implements Visitor { - private double alSum; // Aluminum - private double pSum; // Paper - private double gSum; // Glass - private double cSum; // Cardboard - public static void show(String s) { - System.out.println(s); - } - @Override public void visit(Aluminum al) { - double v = al.weight() * al.value(); - show("value of Aluminum= " + v); - alSum += v; - } - @Override public void visit(Paper p) { - double v = p.weight() * p.value(); - show("value of Paper= " + v); - pSum += v; - } - @Override public void visit(Glass g) { - double v = g.weight() * g.value(); - show("value of Glass= " + v); - gSum += v; - } - @Override public void visit(Cardboard c) { - double v = c.weight() * c.value(); - show("value of Cardboard = " + v); - cSum += v; - } - @Override public void total() { - show( - "Total Aluminum: $" + alSum + "\n" + - "Total Paper: $" + pSum + "\n" + - "Total Glass: $" + gSum + "\n" + - "Total Cardboard: $" + cSum); - } -} - -class WeightVisitor implements Visitor { - private double alSum; // Aluminum - private double pSum; // Paper - private double gSum; // Glass - private double cSum; // Cardboard - public static void show(String s) { - System.out.println(s); - } - @Override public void visit(Aluminum al) { - alSum += al.weight(); - show("Aluminum weight = " + al.weight()); - } - @Override public void visit(Paper p) { - pSum += p.weight(); - show("Paper weight = " + p.weight()); - } - @Override public void visit(Glass g) { - gSum += g.weight(); - show("Glass weight = " + g.weight()); - } - @Override public void visit(Cardboard c) { - cSum += c.weight(); - show("Cardboard weight = " + c.weight()); - } - @Override public void total() { - show("Total weight Aluminum:" + alSum); - show("Total weight Paper:" + pSum); - show("Total weight Glass:" + gSum); - show("Total weight Cardboard:" + cSum); - } -} - -public class TrashVisitor { - public static void main(String[] args) { - List bin = new ArrayList<>(); - // ParseTrash still works, without changes: - ParseTrash.fillBin("trashvisitor", bin); - List visitors = Arrays.asList( - new PriceVisitor(), new WeightVisitor()); - bin.forEach( t -> { - Visitable v = (Visitable) t; - visitors.forEach(visitor -> v.accept(visitor)); - }); - visitors.forEach(Visitor::total); - } -} -/* Output: (First and Last 10 Lines) -Loading patterns.trashvisitor.Glass -Loading patterns.trashvisitor.Paper -Loading patterns.trashvisitor.Aluminum -Loading patterns.trashvisitor.Cardboard -value of Glass= 12.420000225305557 -Glass weight = 54.0 -value of Paper= 2.2000000327825546 -Paper weight = 22.0 -value of Paper= 1.1000000163912773 -Paper weight = 11.0 -...________...________...________...________... -value of Cardboard = 5.060000091791153 -Cardboard weight = 22.0 -Total Aluminum: $860.0499778985977 -Total Paper: $35.80000053346157 -Total Glass: $150.1900027245283 -Total Cardboard: $40.02000072598457 -Total weight Aluminum:515.0 -Total weight Paper:358.0 -Total weight Glass:653.0 -Total weight Cardboard:174.0 -*/ diff --git a/patterns/trashvisitor/Visitable.java b/patterns/trashvisitor/Visitable.java deleted file mode 100644 index 54563d43..00000000 --- a/patterns/trashvisitor/Visitable.java +++ /dev/null @@ -1,12 +0,0 @@ -// patterns/trashvisitor/Visitable.java -// (c)2021 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 interface to add visitor functionality to the -// Trash hierarchy without modifying the base class -package patterns.trashvisitor; - -public interface Visitable { - // The new method: - void accept(Visitor v); -} diff --git a/patterns/trashvisitor/Visitor.java b/patterns/trashvisitor/Visitor.java deleted file mode 100644 index 3f9bfe46..00000000 --- a/patterns/trashvisitor/Visitor.java +++ /dev/null @@ -1,14 +0,0 @@ -// patterns/trashvisitor/Visitor.java -// (c)2021 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. -// The base interface for visitors -package patterns.trashvisitor; - -public interface Visitor { - void visit(Aluminum a); - void visit(Paper p); - void visit(Glass g); - void visit(Cardboard c); - void total(); -} diff --git a/patterns/visitor/BeeAndFlowers.java b/patterns/visitor/BeeAndFlowers.java index a1d40045..39596619 100644 --- a/patterns/visitor/BeeAndFlowers.java +++ b/patterns/visitor/BeeAndFlowers.java @@ -2,7 +2,7 @@ // (c)2021 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. -// Demonstration of "visitor" pattern +// Demonstration of the Visitor pattern. // {java patterns.visitor.BeeAndFlowers} package patterns.visitor; import java.util.*; @@ -11,7 +11,7 @@ import java.util.stream.*; interface Visitor { void visit(Gladiolus g); - void visit(Renuculus r); + void visit(Ranunculus r); void visit(Chrysanthemum c); } @@ -25,7 +25,7 @@ class Gladiolus implements Flower { public void accept(Visitor v) { v.visit(this);} } -class Renuculus implements Flower { +class Ranunculus implements Flower { @Override public void accept(Visitor v) { v.visit(this);} } @@ -37,13 +37,13 @@ class Chrysanthemum implements Flower { // Add the ability to produce a String: class StringVal implements Visitor { - String s; + private String s; @Override public String toString() { return s; } @Override public void visit(Gladiolus g) { s = "Gladiolus"; } - @Override public void visit(Renuculus r) { - s = "Renuculus"; + @Override public void visit(Ranunculus r) { + s = "Ranunculus"; } @Override public void visit(Chrysanthemum c) { s = "Chrysanthemum"; @@ -55,8 +55,8 @@ class Bee implements Visitor { @Override public void visit(Gladiolus g) { System.out.println("Bee and Gladiolus"); } - @Override public void visit(Renuculus r) { - System.out.println("Bee and Renuculus"); + @Override public void visit(Ranunculus r) { + System.out.println("Bee and Ranunculus"); } @Override public void visit(Chrysanthemum c) { System.out.println("Bee and Chrysanthemum"); @@ -66,7 +66,7 @@ class Bee implements Visitor { class FlowerFactory { static List> flowers = Arrays.asList(Gladiolus::new, - Renuculus::new, Chrysanthemum::new); + Ranunculus::new, Chrysanthemum::new); static final int SZ = flowers.size(); private static SplittableRandom rand = new SplittableRandom(47); @@ -95,21 +95,21 @@ public class BeeAndFlowers { Gladiolus Chrysanthemum Gladiolus -Renuculus +Ranunculus Chrysanthemum -Renuculus +Ranunculus Chrysanthemum Chrysanthemum Chrysanthemum -Renuculus +Ranunculus Bee and Gladiolus Bee and Chrysanthemum Bee and Gladiolus -Bee and Renuculus +Bee and Ranunculus Bee and Chrysanthemum -Bee and Renuculus +Bee and Ranunculus Bee and Chrysanthemum Bee and Chrysanthemum Bee and Chrysanthemum -Bee and Renuculus +Bee and Ranunculus */ diff --git a/polymorphism/RTTI.java b/polymorphism/Reflect.java similarity index 81% rename from polymorphism/RTTI.java rename to polymorphism/Reflect.java index fe56a6b2..341e86f6 100644 --- a/polymorphism/RTTI.java +++ b/polymorphism/Reflect.java @@ -1,8 +1,7 @@ -// polymorphism/RTTI.java +// polymorphism/Reflect.java // (c)2021 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. -// Downcasting & Runtime type information (RTTI) // {ThrowsException} class Useful { @@ -18,7 +17,7 @@ class MoreUseful extends Useful { public void w() {} } -public class RTTI { +public class Reflect { public static void main(String[] args) { Useful[] x = { new Useful(), @@ -28,7 +27,7 @@ public class RTTI { x[1].g(); // Compile time: method not found in Useful: //- x[1].u(); - ((MoreUseful)x[1]).u(); // Downcast/RTTI + ((MoreUseful)x[1]).u(); // Downcast/Reflect ((MoreUseful)x[0]).u(); // Exception thrown } } @@ -37,5 +36,5 @@ ___[ Error Output ]___ Exception in thread "main" java.lang.ClassCastException: Useful cannot be cast to MoreUseful - at RTTI.main(RTTI.java:29) + at Reflect.main(Reflect.java:28) */ diff --git a/references/CopyConstructor.java b/references/CopyConstructor.java index 6ffb5303..fe16259f 100644 --- a/references/CopyConstructor.java +++ b/references/CopyConstructor.java @@ -13,7 +13,7 @@ class FruitQualities { private int ripeness; private int smell; // etc. - // No-arg constructor: + // Zero-argument constructor: FruitQualities() { // Do something meaningful... } @@ -32,7 +32,7 @@ class FruitQualities { class Seed { // Members... - Seed() { /* No-arg constructor */ } + Seed() { /* Zero-argument constructor */ } Seed(Seed s) { /* Copy constructor */ } } @@ -81,7 +81,7 @@ class Tomato extends Fruit { class ZebraQualities extends FruitQualities { private int stripedness; - // No-arg constructor: + // Zero-argument constructor: ZebraQualities() { super(); // do something meaningful... @@ -117,7 +117,7 @@ public class CopyConstructor { t.getClass().getName()); } public static void slice(Fruit f) { - f = new Fruit(f); // Hmmm... will this work? // [2] + f = new Fruit(f); // Hmm... will this work? // [2] System.out.println("In slice, f is a " + f.getClass().getName()); } diff --git a/typeinfo/AnonymousImplementation.java b/reflection/AnonymousImplementation.java similarity index 94% rename from typeinfo/AnonymousImplementation.java rename to reflection/AnonymousImplementation.java index 7cff1282..290798bb 100644 --- a/typeinfo/AnonymousImplementation.java +++ b/reflection/AnonymousImplementation.java @@ -1,9 +1,9 @@ -// typeinfo/AnonymousImplementation.java +// reflection/AnonymousImplementation.java // (c)2021 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. // Anonymous inner classes can't hide from reflection -import typeinfo.interfacea.*; +import reflection.interfacea.*; class AnonymousA { public static A makeA() { diff --git a/typeinfo/BoundedClassReferences.java b/reflection/BoundedClassReferences.java similarity index 90% rename from typeinfo/BoundedClassReferences.java rename to reflection/BoundedClassReferences.java index 58d7eb00..6d296059 100644 --- a/typeinfo/BoundedClassReferences.java +++ b/reflection/BoundedClassReferences.java @@ -1,4 +1,4 @@ -// typeinfo/BoundedClassReferences.java +// reflection/BoundedClassReferences.java // (c)2021 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. diff --git a/typeinfo/ClassCasts.java b/reflection/ClassCasts.java similarity index 93% rename from typeinfo/ClassCasts.java rename to reflection/ClassCasts.java index 5f36ba4b..3efd64c3 100644 --- a/typeinfo/ClassCasts.java +++ b/reflection/ClassCasts.java @@ -1,4 +1,4 @@ -// typeinfo/ClassCasts.java +// reflection/ClassCasts.java // (c)2021 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. diff --git a/typeinfo/ClassInitialization.java b/reflection/ClassInitialization.java similarity index 97% rename from typeinfo/ClassInitialization.java rename to reflection/ClassInitialization.java index 3814949d..21b71133 100644 --- a/typeinfo/ClassInitialization.java +++ b/reflection/ClassInitialization.java @@ -1,4 +1,4 @@ -// typeinfo/ClassInitialization.java +// reflection/ClassInitialization.java // (c)2021 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. diff --git a/typeinfo/DynamicSupplier.java b/reflection/DynamicSupplier.java similarity index 96% rename from typeinfo/DynamicSupplier.java rename to reflection/DynamicSupplier.java index 1bcf039a..910d1215 100644 --- a/typeinfo/DynamicSupplier.java +++ b/reflection/DynamicSupplier.java @@ -1,4 +1,4 @@ -// typeinfo/DynamicSupplier.java +// reflection/DynamicSupplier.java // (c)2021 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. diff --git a/typeinfo/FamilyVsExactType.java b/reflection/FamilyVsExactType.java similarity index 90% rename from typeinfo/FamilyVsExactType.java rename to reflection/FamilyVsExactType.java index 10e1b380..db2bc9da 100644 --- a/typeinfo/FamilyVsExactType.java +++ b/reflection/FamilyVsExactType.java @@ -1,10 +1,10 @@ -// typeinfo/FamilyVsExactType.java +// reflection/FamilyVsExactType.java // (c)2021 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. // The difference between instanceof and class -// {java typeinfo.FamilyVsExactType} -package typeinfo; +// {java reflection.FamilyVsExactType} +package reflection; class Base {} class Derived extends Base {} @@ -41,7 +41,7 @@ public class FamilyVsExactType { } } /* Output: -Testing x of type class typeinfo.Base +Testing x of type class reflection.Base x instanceof Base true x instanceof Derived false Base.isInstance(x) true @@ -50,7 +50,7 @@ x.getClass() == Base.class true x.getClass() == Derived.class false x.getClass().equals(Base.class)) true x.getClass().equals(Derived.class)) false -Testing x of type class typeinfo.Derived +Testing x of type class reflection.Derived x instanceof Base true x instanceof Derived true Base.isInstance(x) true diff --git a/typeinfo/GenericClassReferences.java b/reflection/GenericClassReferences.java similarity index 91% rename from typeinfo/GenericClassReferences.java rename to reflection/GenericClassReferences.java index 9925fb19..66601f93 100644 --- a/typeinfo/GenericClassReferences.java +++ b/reflection/GenericClassReferences.java @@ -1,4 +1,4 @@ -// typeinfo/GenericClassReferences.java +// reflection/GenericClassReferences.java // (c)2021 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. diff --git a/typeinfo/HiddenImplementation.java b/reflection/HiddenImplementation.java similarity index 88% rename from typeinfo/HiddenImplementation.java rename to reflection/HiddenImplementation.java index 88dcb238..1d536d30 100644 --- a/typeinfo/HiddenImplementation.java +++ b/reflection/HiddenImplementation.java @@ -1,10 +1,10 @@ -// typeinfo/HiddenImplementation.java +// reflection/HiddenImplementation.java // (c)2021 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. // Sneaking around package hiding -import typeinfo.interfacea.*; -import typeinfo.packageaccess.*; +import reflection.interfacea.*; +import reflection.packageaccess.*; import java.lang.reflect.*; public class HiddenImplementation { @@ -36,7 +36,7 @@ public class HiddenImplementation { } /* Output: public C.f() -typeinfo.packageaccess.C +reflection.packageaccess.C public C.g() package C.u() protected C.v() diff --git a/typeinfo/ID2.java b/reflection/ID2.java similarity index 96% rename from typeinfo/ID2.java rename to reflection/ID2.java index 8463a4fe..6ff472af 100644 --- a/typeinfo/ID2.java +++ b/reflection/ID2.java @@ -1,4 +1,4 @@ -// typeinfo/ID2.java +// reflection/ID2.java // (c)2021 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. diff --git a/typeinfo/InnerImplementation.java b/reflection/InnerImplementation.java similarity index 90% rename from typeinfo/InnerImplementation.java rename to reflection/InnerImplementation.java index 1c15b751..248049c2 100644 --- a/typeinfo/InnerImplementation.java +++ b/reflection/InnerImplementation.java @@ -1,9 +1,9 @@ -// typeinfo/InnerImplementation.java +// reflection/InnerImplementation.java // (c)2021 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. -// Private inner classes can't hide from reflection -import typeinfo.interfacea.*; +// Private inner classes can't hide from reflection. +import reflection.interfacea.*; class InnerA { private static class C implements A { diff --git a/typeinfo/InterfaceViolation.java b/reflection/InterfaceViolation.java similarity index 88% rename from typeinfo/InterfaceViolation.java rename to reflection/InterfaceViolation.java index 84300720..ef8bfc95 100644 --- a/typeinfo/InterfaceViolation.java +++ b/reflection/InterfaceViolation.java @@ -1,9 +1,9 @@ -// typeinfo/InterfaceViolation.java +// reflection/InterfaceViolation.java // (c)2021 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. // Sneaking around an interface -import typeinfo.interfacea.*; +import reflection.interfacea.*; class B implements A { @Override public void f() {} diff --git a/typeinfo/ModifyingPrivateFields.java b/reflection/ModifyingPrivateFields.java similarity index 97% rename from typeinfo/ModifyingPrivateFields.java rename to reflection/ModifyingPrivateFields.java index 0d68972e..99b92953 100644 --- a/typeinfo/ModifyingPrivateFields.java +++ b/reflection/ModifyingPrivateFields.java @@ -1,4 +1,4 @@ -// typeinfo/ModifyingPrivateFields.java +// reflection/ModifyingPrivateFields.java // (c)2021 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. diff --git a/typeinfo/NullRobot.java b/reflection/NullRobot.java similarity index 98% rename from typeinfo/NullRobot.java rename to reflection/NullRobot.java index a3d0ed6b..c3e19659 100644 --- a/typeinfo/NullRobot.java +++ b/reflection/NullRobot.java @@ -1,4 +1,4 @@ -// typeinfo/NullRobot.java +// reflection/NullRobot.java // (c)2021 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. diff --git a/typeinfo/Operation.java b/reflection/Operation.java similarity index 93% rename from typeinfo/Operation.java rename to reflection/Operation.java index d3013aca..4b749e7d 100644 --- a/typeinfo/Operation.java +++ b/reflection/Operation.java @@ -1,4 +1,4 @@ -// typeinfo/Operation.java +// reflection/Operation.java // (c)2021 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. diff --git a/typeinfo/Person.java b/reflection/Person.java similarity index 98% rename from typeinfo/Person.java rename to reflection/Person.java index 77dc091e..070a2490 100644 --- a/typeinfo/Person.java +++ b/reflection/Person.java @@ -1,4 +1,4 @@ -// typeinfo/Person.java +// reflection/Person.java // (c)2021 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. diff --git a/typeinfo/PetCounter.java b/reflection/PetCounter.java similarity index 97% rename from typeinfo/PetCounter.java rename to reflection/PetCounter.java index bc0642f5..97488062 100644 --- a/typeinfo/PetCounter.java +++ b/reflection/PetCounter.java @@ -1,9 +1,9 @@ -// typeinfo/PetCounter.java +// reflection/PetCounter.java // (c)2021 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. // Using instanceof -import typeinfo.pets.*; +import reflection.pets.*; import java.util.*; public class PetCounter { diff --git a/typeinfo/PetCounter2.java b/reflection/PetCounter2.java similarity index 90% rename from typeinfo/PetCounter2.java rename to reflection/PetCounter2.java index 1293a107..ccac0b57 100644 --- a/typeinfo/PetCounter2.java +++ b/reflection/PetCounter2.java @@ -1,8 +1,8 @@ -// typeinfo/PetCounter2.java +// reflection/PetCounter2.java // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; public class PetCounter2 { public static void main(String[] args) { diff --git a/typeinfo/PetCounter3.java b/reflection/PetCounter3.java similarity index 96% rename from typeinfo/PetCounter3.java rename to reflection/PetCounter3.java index ed4c51bd..aec88ef0 100644 --- a/typeinfo/PetCounter3.java +++ b/reflection/PetCounter3.java @@ -1,4 +1,4 @@ -// typeinfo/PetCounter3.java +// reflection/PetCounter3.java // (c)2021 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. @@ -6,7 +6,7 @@ import java.util.*; import java.util.stream.*; import onjava.*; -import typeinfo.pets.*; +import reflection.pets.*; public class PetCounter3 { static class Counter extends diff --git a/typeinfo/PetCounter4.java b/reflection/PetCounter4.java similarity index 92% rename from typeinfo/PetCounter4.java rename to reflection/PetCounter4.java index a7058c92..9015e66c 100644 --- a/typeinfo/PetCounter4.java +++ b/reflection/PetCounter4.java @@ -1,8 +1,8 @@ -// typeinfo/PetCounter4.java +// reflection/PetCounter4.java // (c)2021 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 typeinfo.pets.*; +import reflection.pets.*; import onjava.*; public class PetCounter4 { diff --git a/typeinfo/Position.java b/reflection/Position.java similarity index 98% rename from typeinfo/Position.java rename to reflection/Position.java index 44df8269..ea07ca78 100644 --- a/typeinfo/Position.java +++ b/reflection/Position.java @@ -1,4 +1,4 @@ -// typeinfo/Position.java +// reflection/Position.java // (c)2021 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. diff --git a/typeinfo/RegisteredFactories.java b/reflection/RegisteredFactories.java similarity index 98% rename from typeinfo/RegisteredFactories.java rename to reflection/RegisteredFactories.java index bc7d0545..04267c42 100644 --- a/typeinfo/RegisteredFactories.java +++ b/reflection/RegisteredFactories.java @@ -1,4 +1,4 @@ -// typeinfo/RegisteredFactories.java +// reflection/RegisteredFactories.java // (c)2021 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. diff --git a/typeinfo/Robot.java b/reflection/Robot.java similarity index 96% rename from typeinfo/Robot.java rename to reflection/Robot.java index ed7adc3f..00818d37 100644 --- a/typeinfo/Robot.java +++ b/reflection/Robot.java @@ -1,4 +1,4 @@ -// typeinfo/Robot.java +// reflection/Robot.java // (c)2021 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. diff --git a/typeinfo/SelectingMethods.java b/reflection/SelectingMethods.java similarity index 97% rename from typeinfo/SelectingMethods.java rename to reflection/SelectingMethods.java index eb679194..9002cf35 100644 --- a/typeinfo/SelectingMethods.java +++ b/reflection/SelectingMethods.java @@ -1,4 +1,4 @@ -// typeinfo/SelectingMethods.java +// reflection/SelectingMethods.java // (c)2021 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. diff --git a/typeinfo/Shapes.java b/reflection/Shapes.java similarity index 96% rename from typeinfo/Shapes.java rename to reflection/Shapes.java index ec929650..05d15df5 100644 --- a/typeinfo/Shapes.java +++ b/reflection/Shapes.java @@ -1,4 +1,4 @@ -// typeinfo/Shapes.java +// reflection/Shapes.java // (c)2021 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. diff --git a/typeinfo/ShowMethods.java b/reflection/ShowMethods.java similarity index 98% rename from typeinfo/ShowMethods.java rename to reflection/ShowMethods.java index ad4f16eb..48024cdc 100644 --- a/typeinfo/ShowMethods.java +++ b/reflection/ShowMethods.java @@ -1,4 +1,4 @@ -// typeinfo/ShowMethods.java +// reflection/ShowMethods.java // (c)2021 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. diff --git a/typeinfo/SimpleDynamicProxy.java b/reflection/SimpleDynamicProxy.java similarity index 97% rename from typeinfo/SimpleDynamicProxy.java rename to reflection/SimpleDynamicProxy.java index 544b780b..9eeb0292 100644 --- a/typeinfo/SimpleDynamicProxy.java +++ b/reflection/SimpleDynamicProxy.java @@ -1,4 +1,4 @@ -// typeinfo/SimpleDynamicProxy.java +// reflection/SimpleDynamicProxy.java // (c)2021 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. diff --git a/typeinfo/SimpleProxyDemo.java b/reflection/SimpleProxyDemo.java similarity index 97% rename from typeinfo/SimpleProxyDemo.java rename to reflection/SimpleProxyDemo.java index 1e5eea41..24f7b016 100644 --- a/typeinfo/SimpleProxyDemo.java +++ b/reflection/SimpleProxyDemo.java @@ -1,4 +1,4 @@ -// typeinfo/SimpleProxyDemo.java +// reflection/SimpleProxyDemo.java // (c)2021 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. diff --git a/typeinfo/SnowRobot.java b/reflection/SnowRobot.java similarity index 97% rename from typeinfo/SnowRobot.java rename to reflection/SnowRobot.java index b28b1f7f..227dcb49 100644 --- a/typeinfo/SnowRobot.java +++ b/reflection/SnowRobot.java @@ -1,4 +1,4 @@ -// typeinfo/SnowRobot.java +// reflection/SnowRobot.java // (c)2021 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. diff --git a/typeinfo/Staff.java b/reflection/Staff.java similarity index 98% rename from typeinfo/Staff.java rename to reflection/Staff.java index 20428034..8c3bb76e 100644 --- a/typeinfo/Staff.java +++ b/reflection/Staff.java @@ -1,4 +1,4 @@ -// typeinfo/Staff.java +// reflection/Staff.java // (c)2021 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. diff --git a/typeinfo/SweetShop.java b/reflection/SweetShop.java similarity index 97% rename from typeinfo/SweetShop.java rename to reflection/SweetShop.java index a046b281..d5f964f0 100644 --- a/typeinfo/SweetShop.java +++ b/reflection/SweetShop.java @@ -1,4 +1,4 @@ -// typeinfo/SweetShop.java +// reflection/SweetShop.java // (c)2021 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. diff --git a/typeinfo/WildcardClassReferences.java b/reflection/WildcardClassReferences.java similarity index 87% rename from typeinfo/WildcardClassReferences.java rename to reflection/WildcardClassReferences.java index 066b15d5..d7266f73 100644 --- a/typeinfo/WildcardClassReferences.java +++ b/reflection/WildcardClassReferences.java @@ -1,4 +1,4 @@ -// typeinfo/WildcardClassReferences.java +// reflection/WildcardClassReferences.java // (c)2021 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. diff --git a/typeinfo/interfacea/A.java b/reflection/interfacea/A.java similarity index 75% rename from typeinfo/interfacea/A.java rename to reflection/interfacea/A.java index 359fb98e..2b4c33ec 100644 --- a/typeinfo/interfacea/A.java +++ b/reflection/interfacea/A.java @@ -1,8 +1,8 @@ -// typeinfo/interfacea/A.java +// reflection/interfacea/A.java // (c)2021 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 typeinfo.interfacea; +package reflection.interfacea; public interface A { void f(); diff --git a/typeinfo/packageaccess/HiddenC.java b/reflection/packageaccess/HiddenC.java similarity index 84% rename from typeinfo/packageaccess/HiddenC.java rename to reflection/packageaccess/HiddenC.java index 53277281..24fb20d4 100644 --- a/typeinfo/packageaccess/HiddenC.java +++ b/reflection/packageaccess/HiddenC.java @@ -1,9 +1,9 @@ -// typeinfo/packageaccess/HiddenC.java +// reflection/packageaccess/HiddenC.java // (c)2021 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 typeinfo.packageaccess; -import typeinfo.interfacea.*; +package reflection.packageaccess; +import reflection.interfacea.*; class C implements A { @Override public void f() { diff --git a/typeinfo/pets/Cat.java b/reflection/pets/Cat.java similarity index 83% rename from typeinfo/pets/Cat.java rename to reflection/pets/Cat.java index 68594e74..2ed2aba9 100644 --- a/typeinfo/pets/Cat.java +++ b/reflection/pets/Cat.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Cat.java +// reflection/pets/Cat.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Cat extends Pet { public Cat(String name) { super(name); } diff --git a/typeinfo/pets/Creator.java b/reflection/pets/Creator.java similarity index 95% rename from typeinfo/pets/Creator.java rename to reflection/pets/Creator.java index e3703e76..f5f152fe 100644 --- a/typeinfo/pets/Creator.java +++ b/reflection/pets/Creator.java @@ -1,9 +1,9 @@ -// typeinfo/pets/Creator.java +// reflection/pets/Creator.java // (c)2021 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. // Creates random Pets -package typeinfo.pets; +package reflection.pets; import java.util.*; import java.util.function.*; import java.util.stream.*; diff --git a/typeinfo/pets/Cymric.java b/reflection/pets/Cymric.java similarity index 83% rename from typeinfo/pets/Cymric.java rename to reflection/pets/Cymric.java index 4835e4a2..11521fe8 100644 --- a/typeinfo/pets/Cymric.java +++ b/reflection/pets/Cymric.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Cymric.java +// reflection/pets/Cymric.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Cymric extends Manx { public Cymric(String name) { super(name); } diff --git a/typeinfo/pets/Dog.java b/reflection/pets/Dog.java similarity index 83% rename from typeinfo/pets/Dog.java rename to reflection/pets/Dog.java index 2e5b9a8a..abd370ff 100644 --- a/typeinfo/pets/Dog.java +++ b/reflection/pets/Dog.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Dog.java +// reflection/pets/Dog.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Dog extends Pet { public Dog(String name) { super(name); } diff --git a/typeinfo/pets/EgyptianMau.java b/reflection/pets/EgyptianMau.java similarity index 82% rename from typeinfo/pets/EgyptianMau.java rename to reflection/pets/EgyptianMau.java index e728de93..4e314fb6 100644 --- a/typeinfo/pets/EgyptianMau.java +++ b/reflection/pets/EgyptianMau.java @@ -1,8 +1,8 @@ -// typeinfo/pets/EgyptianMau.java +// reflection/pets/EgyptianMau.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class EgyptianMau extends Cat { public EgyptianMau(String name) { super(name); } diff --git a/typeinfo/pets/ForNamePetCreator.java b/reflection/pets/ForNamePetCreator.java similarity index 71% rename from typeinfo/pets/ForNamePetCreator.java rename to reflection/pets/ForNamePetCreator.java index 6e35740d..92648db5 100644 --- a/typeinfo/pets/ForNamePetCreator.java +++ b/reflection/pets/ForNamePetCreator.java @@ -1,8 +1,8 @@ -// typeinfo/pets/ForNamePetCreator.java +// reflection/pets/ForNamePetCreator.java // (c)2021 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 typeinfo.pets; +package reflection.pets; import java.util.*; public class ForNamePetCreator extends Creator { @@ -10,14 +10,14 @@ public class ForNamePetCreator extends Creator { new ArrayList<>(); // Types you want randomly created: private static String[] typeNames = { - "typeinfo.pets.Mutt", - "typeinfo.pets.Pug", - "typeinfo.pets.EgyptianMau", - "typeinfo.pets.Manx", - "typeinfo.pets.Cymric", - "typeinfo.pets.Rat", - "typeinfo.pets.Mouse", - "typeinfo.pets.Hamster" + "reflection.pets.Mutt", + "reflection.pets.Pug", + "reflection.pets.EgyptianMau", + "reflection.pets.Manx", + "reflection.pets.Cymric", + "reflection.pets.Rat", + "reflection.pets.Mouse", + "reflection.pets.Hamster" }; @SuppressWarnings("unchecked") private static void loader() { diff --git a/typeinfo/pets/Hamster.java b/reflection/pets/Hamster.java similarity index 83% rename from typeinfo/pets/Hamster.java rename to reflection/pets/Hamster.java index 9763b30b..fb09f3ab 100644 --- a/typeinfo/pets/Hamster.java +++ b/reflection/pets/Hamster.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Hamster.java +// reflection/pets/Hamster.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Hamster extends Rodent { public Hamster(String name) { super(name); } diff --git a/typeinfo/pets/Individual.java b/reflection/pets/Individual.java similarity index 95% rename from typeinfo/pets/Individual.java rename to reflection/pets/Individual.java index 1783621d..524fba6e 100644 --- a/typeinfo/pets/Individual.java +++ b/reflection/pets/Individual.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Individual.java +// reflection/pets/Individual.java // (c)2021 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 typeinfo.pets; +package reflection.pets; import java.util.*; public class diff --git a/typeinfo/pets/Manx.java b/reflection/pets/Manx.java similarity index 83% rename from typeinfo/pets/Manx.java rename to reflection/pets/Manx.java index bf949bc0..a5c6bea9 100644 --- a/typeinfo/pets/Manx.java +++ b/reflection/pets/Manx.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Manx.java +// reflection/pets/Manx.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Manx extends Cat { public Manx(String name) { super(name); } diff --git a/typeinfo/pets/Mouse.java b/reflection/pets/Mouse.java similarity index 83% rename from typeinfo/pets/Mouse.java rename to reflection/pets/Mouse.java index 07a7aac8..9ad9b290 100644 --- a/typeinfo/pets/Mouse.java +++ b/reflection/pets/Mouse.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Mouse.java +// reflection/pets/Mouse.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Mouse extends Rodent { public Mouse(String name) { super(name); } diff --git a/typeinfo/pets/Mutt.java b/reflection/pets/Mutt.java similarity index 83% rename from typeinfo/pets/Mutt.java rename to reflection/pets/Mutt.java index be99e73b..f50ba64d 100644 --- a/typeinfo/pets/Mutt.java +++ b/reflection/pets/Mutt.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Mutt.java +// reflection/pets/Mutt.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Mutt extends Dog { public Mutt(String name) { super(name); } diff --git a/typeinfo/pets/Person.java b/reflection/pets/Person.java similarity index 81% rename from typeinfo/pets/Person.java rename to reflection/pets/Person.java index f2055e64..bfe61ca3 100644 --- a/typeinfo/pets/Person.java +++ b/reflection/pets/Person.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Person.java +// reflection/pets/Person.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Person extends Individual { public Person(String name) { super(name); } diff --git a/typeinfo/pets/Pet.java b/reflection/pets/Pet.java similarity index 83% rename from typeinfo/pets/Pet.java rename to reflection/pets/Pet.java index b4c934ac..a3a0cf2e 100644 --- a/typeinfo/pets/Pet.java +++ b/reflection/pets/Pet.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Pet.java +// reflection/pets/Pet.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Pet extends Individual { public Pet(String name) { super(name); } diff --git a/typeinfo/pets/PetCreator.java b/reflection/pets/PetCreator.java similarity index 75% rename from typeinfo/pets/PetCreator.java rename to reflection/pets/PetCreator.java index 3541160b..1f01ceed 100644 --- a/typeinfo/pets/PetCreator.java +++ b/reflection/pets/PetCreator.java @@ -1,10 +1,10 @@ -// typeinfo/pets/PetCreator.java +// reflection/pets/PetCreator.java // (c)2021 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. // Using class literals -// {java typeinfo.pets.PetCreator} -package typeinfo.pets; +// {java reflection.pets.PetCreator} +package reflection.pets; import java.util.*; public class PetCreator extends Creator { @@ -33,10 +33,10 @@ public class PetCreator extends Creator { } } /* Output: -[class typeinfo.pets.Mutt, class typeinfo.pets.Pug, -class typeinfo.pets.EgyptianMau, class -typeinfo.pets.Manx, class typeinfo.pets.Cymric, class -typeinfo.pets.Rat, class typeinfo.pets.Mouse, class -typeinfo.pets.Hamster] +[class reflection.pets.Mutt, class reflection.pets.Pug, +class reflection.pets.EgyptianMau, class +reflection.pets.Manx, class reflection.pets.Cymric, class +reflection.pets.Rat, class reflection.pets.Mouse, class +reflection.pets.Hamster] [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug] */ diff --git a/typeinfo/pets/Pug.java b/reflection/pets/Pug.java similarity index 83% rename from typeinfo/pets/Pug.java rename to reflection/pets/Pug.java index 83da9518..1a150b86 100644 --- a/typeinfo/pets/Pug.java +++ b/reflection/pets/Pug.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Pug.java +// reflection/pets/Pug.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Pug extends Dog { public Pug(String name) { super(name); } diff --git a/typeinfo/pets/Rat.java b/reflection/pets/Rat.java similarity index 83% rename from typeinfo/pets/Rat.java rename to reflection/pets/Rat.java index 68a346fb..cd006426 100644 --- a/typeinfo/pets/Rat.java +++ b/reflection/pets/Rat.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Rat.java +// reflection/pets/Rat.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Rat extends Rodent { public Rat(String name) { super(name); } diff --git a/typeinfo/pets/Rodent.java b/reflection/pets/Rodent.java similarity index 83% rename from typeinfo/pets/Rodent.java rename to reflection/pets/Rodent.java index cab79e75..3c9063ba 100644 --- a/typeinfo/pets/Rodent.java +++ b/reflection/pets/Rodent.java @@ -1,8 +1,8 @@ -// typeinfo/pets/Rodent.java +// reflection/pets/Rodent.java // (c)2021 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 typeinfo.pets; +package reflection.pets; public class Rodent extends Pet { public Rodent(String name) { super(name); } diff --git a/typeinfo/toys/GenericToyTest.java b/reflection/toys/GenericToyTest.java similarity index 85% rename from typeinfo/toys/GenericToyTest.java rename to reflection/toys/GenericToyTest.java index 930fbc77..3332bcd6 100644 --- a/typeinfo/toys/GenericToyTest.java +++ b/reflection/toys/GenericToyTest.java @@ -1,10 +1,10 @@ -// typeinfo/toys/GenericToyTest.java +// reflection/toys/GenericToyTest.java // (c)2021 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. // Testing class Class -// {java typeinfo.toys.GenericToyTest} -package typeinfo.toys; +// {java reflection.toys.GenericToyTest} +package reflection.toys; public class GenericToyTest { public static void diff --git a/typeinfo/toys/ToyTest.java b/reflection/toys/ToyTest.java similarity index 67% rename from typeinfo/toys/ToyTest.java rename to reflection/toys/ToyTest.java index f67261f4..e81e64b7 100644 --- a/typeinfo/toys/ToyTest.java +++ b/reflection/toys/ToyTest.java @@ -1,10 +1,10 @@ -// typeinfo/toys/ToyTest.java +// reflection/toys/ToyTest.java // (c)2021 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. // Testing class Class -// {java typeinfo.toys.ToyTest} -package typeinfo.toys; +// {java reflection.toys.ToyTest} +package reflection.toys; import java.lang.reflect.InvocationTargetException; interface HasBatteries {} @@ -12,7 +12,7 @@ interface Waterproof {} interface Shoots {} class Toy { - // Comment out the following no-arg + // Comment out the following zero-argument // constructor to see NoSuchMethodError public Toy() {} public Toy(int i) {} @@ -36,7 +36,7 @@ public class ToyTest { public static void main(String[] args) { Class c = null; try { - c = Class.forName("typeinfo.toys.FancyToy"); + c = Class.forName("reflection.toys.FancyToy"); } catch(ClassNotFoundException e) { System.out.println("Can't find FancyToy"); System.exit(1); @@ -47,7 +47,7 @@ public class ToyTest { Class up = c.getSuperclass(); Object obj = null; try { - // Requires public no-arg constructor: + // Requires public zero-argument constructor: obj = up.newInstance(); } catch(Exception e) { throw new @@ -57,22 +57,22 @@ public class ToyTest { } } /* Output: -Class name: typeinfo.toys.FancyToy is interface? +Class name: reflection.toys.FancyToy is interface? [false] Simple name: FancyToy -Canonical name : typeinfo.toys.FancyToy -Class name: typeinfo.toys.HasBatteries is interface? +Canonical name : reflection.toys.FancyToy +Class name: reflection.toys.HasBatteries is interface? [true] Simple name: HasBatteries -Canonical name : typeinfo.toys.HasBatteries -Class name: typeinfo.toys.Waterproof is interface? +Canonical name : reflection.toys.HasBatteries +Class name: reflection.toys.Waterproof is interface? [true] Simple name: Waterproof -Canonical name : typeinfo.toys.Waterproof -Class name: typeinfo.toys.Shoots is interface? [true] +Canonical name : reflection.toys.Waterproof +Class name: reflection.toys.Shoots is interface? [true] Simple name: Shoots -Canonical name : typeinfo.toys.Shoots -Class name: typeinfo.toys.Toy is interface? [false] +Canonical name : reflection.toys.Shoots +Class name: reflection.toys.Toy is interface? [false] Simple name: Toy -Canonical name : typeinfo.toys.Toy +Canonical name : reflection.toys.Toy */ diff --git a/reuse/Bath.java b/reuse/Bath.java index 51fbe264..f31a1490 100644 --- a/reuse/Bath.java +++ b/reuse/Bath.java @@ -18,14 +18,14 @@ public class Bath { s1 = "Happy", s2 = "Happy", s3, s4; - private Soap castille; + private Soap castile; private int i; private float toy; public Bath() { System.out.println("Inside Bath()"); s3 = "Joy"; toy = 3.14f; - castille = new Soap(); + castile = new Soap(); } // Instance initialization: { i = 47; } @@ -39,7 +39,7 @@ public class Bath { "s4 = " + s4 + "\n" + "i = " + i + "\n" + "toy = " + toy + "\n" + - "castille = " + castille; + "castile = " + castile; } public static void main(String[] args) { Bath b = new Bath(); @@ -55,5 +55,5 @@ s3 = Joy s4 = Joy i = 47 toy = 3.14 -castille = Constructed +castile = Constructed */ diff --git a/serialization/AStoreCADState.java b/serialization/AStoreCADState.java index f9c9e79f..93b7d5f5 100644 --- a/serialization/AStoreCADState.java +++ b/serialization/AStoreCADState.java @@ -21,7 +21,7 @@ abstract class Shape implements Serializable { dimension = dim; } @Override public String toString() { - return getClass() + "color[" + getColor() + + return getClass() + "Color[" + getColor() + "] xPos[" + xPos + "] yPos[" + yPos + "] dim[" + dimension + "]\n"; } @@ -106,15 +106,15 @@ public class AStoreCADState { } } /* Output: -[class Circlecolor[GREEN] xPos[58] yPos[55] dim[93] -, class Squarecolor[GREEN] xPos[61] yPos[61] dim[29] -, class Linecolor[GREEN] xPos[68] yPos[0] dim[22] -, class Circlecolor[GREEN] xPos[7] yPos[88] dim[28] -, class Squarecolor[GREEN] xPos[51] yPos[89] dim[9] -, class Linecolor[GREEN] xPos[78] yPos[98] dim[61] -, class Circlecolor[GREEN] xPos[20] yPos[58] dim[16] -, class Squarecolor[GREEN] xPos[40] yPos[11] dim[22] -, class Linecolor[GREEN] xPos[4] yPos[83] dim[6] -, class Circlecolor[GREEN] xPos[75] yPos[10] dim[42] +[class CircleColor[GREEN] xPos[58] yPos[55] dim[93] +, class SquareColor[GREEN] xPos[61] yPos[61] dim[29] +, class LineColor[GREEN] xPos[68] yPos[0] dim[22] +, class CircleColor[GREEN] xPos[7] yPos[88] dim[28] +, class SquareColor[GREEN] xPos[51] yPos[89] dim[9] +, class LineColor[GREEN] xPos[78] yPos[98] dim[61] +, class CircleColor[GREEN] xPos[20] yPos[58] dim[16] +, class SquareColor[GREEN] xPos[40] yPos[11] dim[22] +, class LineColor[GREEN] xPos[4] yPos[83] dim[6] +, class CircleColor[GREEN] xPos[75] yPos[10] dim[42] ] */ diff --git a/serialization/Blip3.java b/serialization/Blip3.java index 3d66f573..0e690737 100644 --- a/serialization/Blip3.java +++ b/serialization/Blip3.java @@ -16,7 +16,7 @@ public class Blip3 implements Externalizable { System.out.println("Blip3(String x, int a)"); s = x; i = a; - // s & i initialized only in non-no-arg constructor. + // s & i initialized only in non-zero-argument constructor. } @Override public String toString() { return s + i; } @Override public void writeExternal(ObjectOutput out) diff --git a/serialization/RecoverCADState.java b/serialization/RecoverCADState.java index 188ac976..b27da662 100644 --- a/serialization/RecoverCADState.java +++ b/serialization/RecoverCADState.java @@ -28,15 +28,15 @@ public class RecoverCADState { } } /* Output: -[class Circlecolor[RED] xPos[58] yPos[55] dim[93] -, class Squarecolor[RED] xPos[61] yPos[61] dim[29] -, class Linecolor[GREEN] xPos[68] yPos[0] dim[22] -, class Circlecolor[RED] xPos[7] yPos[88] dim[28] -, class Squarecolor[RED] xPos[51] yPos[89] dim[9] -, class Linecolor[GREEN] xPos[78] yPos[98] dim[61] -, class Circlecolor[RED] xPos[20] yPos[58] dim[16] -, class Squarecolor[RED] xPos[40] yPos[11] dim[22] -, class Linecolor[GREEN] xPos[4] yPos[83] dim[6] -, class Circlecolor[RED] xPos[75] yPos[10] dim[42] +[class CircleColor[RED] xPos[58] yPos[55] dim[93] +, class SquareColor[RED] xPos[61] yPos[61] dim[29] +, class LineColor[GREEN] xPos[68] yPos[0] dim[22] +, class CircleColor[RED] xPos[7] yPos[88] dim[28] +, class SquareColor[RED] xPos[51] yPos[89] dim[9] +, class LineColor[GREEN] xPos[78] yPos[98] dim[61] +, class CircleColor[RED] xPos[20] yPos[58] dim[16] +, class SquareColor[RED] xPos[40] yPos[11] dim[22] +, class LineColor[GREEN] xPos[4] yPos[83] dim[6] +, class CircleColor[RED] xPos[75] yPos[10] dim[42] ] */ diff --git a/serialization/Worm.java b/serialization/Worm.java index 08e3d684..7959be09 100644 --- a/serialization/Worm.java +++ b/serialization/Worm.java @@ -31,7 +31,7 @@ public class Worm implements Serializable { next = new Worm(i, (char)(x + 1)); } public Worm() { - System.out.println("No-arg constructor"); + System.out.println("Zero-argument constructor"); } @Override public String toString() { StringBuilder result = new StringBuilder(":");