Permalink
Browse files

Initial commit of Effective Java, 3e source code, only eight months t…

…oo late.
  • Loading branch information...
jbloch committed Aug 3, 2018
0 parents commit b4dd08373abbb81f7de84fa7a746cd44644d15ed
Showing with 4,385 additions and 0 deletions.
  1. +65 −0 .gitignore
  2. +27 −0 src/effectivejava/chapter10/item74/IndexOutOfBoundsException.java
  3. +20 −0 src/effectivejava/chapter11/item78/brokenstopthread/StopThread.java
  4. +28 −0 src/effectivejava/chapter11/item78/fixedstopthread1/StopThread.java
  5. +20 −0 src/effectivejava/chapter11/item78/fixedstopthread2/StopThread.java
  6. +30 −0 src/effectivejava/chapter11/item79/ForwardingSet.java
  7. +71 −0 src/effectivejava/chapter11/item79/ObservableSet.java
  8. +7 −0 src/effectivejava/chapter11/item79/SetObserver.java
  9. +15 −0 src/effectivejava/chapter11/item79/Test1.java
  10. +21 −0 src/effectivejava/chapter11/item79/Test2.java
  11. +34 −0 src/effectivejava/chapter11/item79/Test3.java
  12. +34 −0 src/effectivejava/chapter11/item81/ConcurrentTimer.java
  13. +25 −0 src/effectivejava/chapter11/item81/Intern.java
  14. +4 −0 src/effectivejava/chapter11/item83/FieldType.java
  15. +52 −0 src/effectivejava/chapter11/item83/Initialization.java
  16. +25 −0 src/effectivejava/chapter11/item84/SlowCountDownLatch.java
  17. +24 −0 src/effectivejava/chapter12/Util.java
  18. +31 −0 src/effectivejava/chapter12/item85/DeserializationBomb.java
  19. +48 −0 src/effectivejava/chapter12/item87/StringList.java
  20. +13 −0 src/effectivejava/chapter12/item89/enumsingleton/Elvis.java
  21. +58 −0 src/effectivejava/chapter12/item90/Period.java
  22. +55 −0 src/effectivejava/chapter2/item2/builder/NutritionFacts.java
  23. +31 −0 src/effectivejava/chapter2/item2/hierarchicalbuilder/Calzone.java
  24. +32 −0 src/effectivejava/chapter2/item2/hierarchicalbuilder/NyPizza.java
  25. +28 −0 src/effectivejava/chapter2/item2/hierarchicalbuilder/Pizza.java
  26. +17 −0 src/effectivejava/chapter2/item2/hierarchicalbuilder/PizzaTest.java
  27. +30 −0 src/effectivejava/chapter2/item2/javabeans/NutritionFacts.java
  28. +45 −0 src/effectivejava/chapter2/item2/telescopingconstructor/NutritionFacts.java
  29. +16 −0 src/effectivejava/chapter2/item3/enumtype/Elvis.java
  30. +18 −0 src/effectivejava/chapter2/item3/field/Elvis.java
  31. +18 −0 src/effectivejava/chapter2/item3/staticfactory/Elvis.java
  32. +11 −0 src/effectivejava/chapter2/item4/UtilityClass.java
  33. +40 −0 src/effectivejava/chapter2/item6/RomanNumerals.java
  34. +29 −0 src/effectivejava/chapter2/item6/Sum.java
  35. +5 −0 src/effectivejava/chapter2/item7/EmptyStackException.java
  36. +51 −0 src/effectivejava/chapter2/item7/Stack.java
  37. +10 −0 src/effectivejava/chapter2/item8/Adult.java
  38. +38 −0 src/effectivejava/chapter2/item8/Room.java
  39. +14 −0 src/effectivejava/chapter2/item8/Teenager.java
  40. +31 −0 src/effectivejava/chapter2/item9/tryfinally/Copy.java
  41. +22 −0 src/effectivejava/chapter2/item9/tryfinally/TopLine.java
  42. +24 −0 src/effectivejava/chapter2/item9/trywithresources/Copy.java
  43. +21 −0 src/effectivejava/chapter2/item9/trywithresources/TopLine.java
  44. +23 −0 src/effectivejava/chapter2/item9/trywithresources/TopLineWithDefault.java
  45. +41 −0 src/effectivejava/chapter3/item10/CaseInsensitiveString.java
  46. +3 −0 src/effectivejava/chapter3/item10/Color.java
  47. +30 −0 src/effectivejava/chapter3/item10/PhoneNumber.java
  48. +32 −0 src/effectivejava/chapter3/item10/Point.java
  49. +35 −0 src/effectivejava/chapter3/item10/composition/ColorPoint.java
  50. +48 −0 src/effectivejava/chapter3/item10/inheritance/ColorPoint.java
  51. +16 −0 src/effectivejava/chapter3/item10/inheritance/CounterPoint.java
  52. +27 −0 src/effectivejava/chapter3/item10/inheritance/CounterPointTest.java
  53. +65 −0 src/effectivejava/chapter3/item11/PhoneNumber.java
  54. +57 −0 src/effectivejava/chapter3/item12/PhoneNumber.java
  55. +4 −0 src/effectivejava/chapter3/item13/EmptyStackException.java
  56. +71 −0 src/effectivejava/chapter3/item13/PhoneNumber.java
  57. +60 −0 src/effectivejava/chapter3/item13/Stack.java
  58. +39 −0 src/effectivejava/chapter3/item14/CaseInsensitiveString.java
  59. +90 −0 src/effectivejava/chapter3/item14/PhoneNumber.java
  60. +11 −0 src/effectivejava/chapter3/item14/WordList.java
  61. +18 −0 src/effectivejava/chapter4/item16/Point.java
  62. +21 −0 src/effectivejava/chapter4/item16/Time.java
  63. +62 −0 src/effectivejava/chapter4/item17/Complex.java
  64. +30 −0 src/effectivejava/chapter4/item18/ForwardingSet.java
  65. +35 −0 src/effectivejava/chapter4/item18/InstrumentedHashSet.java
  66. +29 −0 src/effectivejava/chapter4/item18/InstrumentedSet.java
  67. +23 −0 src/effectivejava/chapter4/item19/Sub.java
  68. +12 −0 src/effectivejava/chapter4/item19/Super.java
  69. +32 −0 src/effectivejava/chapter4/item20/AbstractMapEntry.java
  70. +37 −0 src/effectivejava/chapter4/item20/IntArrays.java
  71. +13 −0 src/effectivejava/chapter4/item22/constantinterface/PhysicalConstants.java
  72. +15 −0 src/effectivejava/chapter4/item22/constantutilityclass/PhysicalConstants.java
  73. +10 −0 src/effectivejava/chapter4/item23/hierarchy/Circle.java
  74. +6 −0 src/effectivejava/chapter4/item23/hierarchy/Figure.java
  75. +13 −0 src/effectivejava/chapter4/item23/hierarchy/Rectangle.java
  76. +8 −0 src/effectivejava/chapter4/item23/hierarchy/Square.java
  77. +40 −0 src/effectivejava/chapter4/item23/taggedclass/Figure.java
  78. +10 −0 src/effectivejava/chapter4/item25/Dessert.java
  79. +8 −0 src/effectivejava/chapter4/item25/Main.java
  80. +16 −0 src/effectivejava/chapter4/item25/Test.java
  81. +10 −0 src/effectivejava/chapter4/item25/Utensil.java
  82. +16 −0 src/effectivejava/chapter5/item26/Raw.java
  83. +21 −0 src/effectivejava/chapter5/item28/Chooser.java
  84. +4 −0 src/effectivejava/chapter5/item29/EmptyStackException.java
  85. +50 −0 src/effectivejava/chapter5/item29/technqiue1/Stack.java
  86. +51 −0 src/effectivejava/chapter5/item29/technqiue2/Stack.java
  87. +29 −0 src/effectivejava/chapter5/item30/GenericSingletonFactory.java
  88. +23 −0 src/effectivejava/chapter5/item30/RecursiveTypeBound.java
  89. +21 −0 src/effectivejava/chapter5/item30/Union.java
  90. +28 −0 src/effectivejava/chapter5/item32/Chooser.java
  91. +4 −0 src/effectivejava/chapter5/item32/EmptyStackException.java
  92. +6 −0 src/effectivejava/chapter5/item32/Function.java
  93. +23 −0 src/effectivejava/chapter5/item32/RecursiveTypeBound.java
  94. +36 −0 src/effectivejava/chapter5/item32/Reduction.java
  95. +75 −0 src/effectivejava/chapter5/item32/Stack.java
  96. +21 −0 src/effectivejava/chapter5/item32/Swap.java
  97. +30 −0 src/effectivejava/chapter5/item32/Union.java
  98. +34 −0 src/effectivejava/chapter5/item33/Favorites.java
  99. +32 −0 src/effectivejava/chapter5/item33/PrintAnnotation.java
  100. +47 −0 src/effectivejava/chapter6/item34/Operation.java
  101. +39 −0 src/effectivejava/chapter6/item34/PayrollDay.java
  102. +34 −0 src/effectivejava/chapter6/item34/Planet.java
  103. +12 −0 src/effectivejava/chapter6/item34/WeightTable.java
  104. +12 −0 src/effectivejava/chapter6/item35/Ensemble.java
  105. +16 −0 src/effectivejava/chapter6/item36/Text.java
  106. +49 −0 src/effectivejava/chapter6/item37/Phase.java
  107. +65 −0 src/effectivejava/chapter6/item37/Plant.java
  108. +27 −0 src/effectivejava/chapter6/item38/BasicOperation.java
  109. +49 −0 src/effectivejava/chapter6/item38/ExtendedOperation.java
  110. +6 −0 src/effectivejava/chapter6/item38/Operation.java
  111. +21 −0 src/effectivejava/chapter6/item39/regularannotation/ExceptionTest.java
  112. +48 −0 src/effectivejava/chapter6/item39/regularannotation/RunTests.java
  113. +17 −0 src/effectivejava/chapter6/item39/regularannotation/Sample.java
  114. +29 −0 src/effectivejava/chapter6/item39/regularannotation/Sample2.java
  115. +14 −0 src/effectivejava/chapter6/item39/regularannotation/Test.java
  116. +16 −0 src/effectivejava/chapter6/item39/repeatableannotation/ExceptionTest.java
  117. +15 −0 src/effectivejava/chapter6/item39/repeatableannotation/ExceptionTestContainer.java
  118. +51 −0 src/effectivejava/chapter6/item39/repeatableannotation/RunTests.java
  119. +33 −0 src/effectivejava/chapter6/item39/repeatableannotation/Sample2.java
  120. +17 −0 src/effectivejava/chapter6/item39/repeatableannotation/Test.java
  121. +29 −0 src/effectivejava/chapter6/item40/Bigram.java
  122. +25 −0 src/effectivejava/chapter7/item42/Operation.java
  123. +37 −0 src/effectivejava/chapter7/item42/SortFourWays.java
  124. +17 −0 src/effectivejava/chapter7/item43/Freq.java
  125. +48 −0 src/effectivejava/chapter7/item45/Card.java
  126. +42 −0 src/effectivejava/chapter7/item45/Freq.java
  127. +20 −0 src/effectivejava/chapter7/item45/MersennePrimes.java
  128. +31 −0 src/effectivejava/chapter7/item45/anagrams/HybridAnagrams.java
  129. +32 −0 src/effectivejava/chapter7/item45/anagrams/IterativeAnagrams.java
  130. +29 −0 src/effectivejava/chapter7/item45/anagrams/StreamAnagrams.java
  131. +16 −0 src/effectivejava/chapter7/item47/Adapters.java
  132. +33 −0 src/effectivejava/chapter7/item47/PowerSet.java
  133. +37 −0 src/effectivejava/chapter7/item47/SubLists.java
  134. +21 −0 src/effectivejava/chapter7/item48/ParallelMersennePrimes.java
  135. +19 −0 src/effectivejava/chapter7/item48/ParallelPrimeCounting.java
  136. +21 −0 src/effectivejava/chapter8/item50/Attack.java
  137. +54 −0 src/effectivejava/chapter8/item50/Period.java
  138. +5 −0 src/effectivejava/chapter8/item52/Champagne.java
  139. +34 −0 src/effectivejava/chapter8/item52/CollectionClassifier.java
  140. +13 −0 src/effectivejava/chapter8/item52/Overriding.java
  141. +20 −0 src/effectivejava/chapter8/item52/SetList.java
  142. +5 −0 src/effectivejava/chapter8/item52/SparklingWine.java
  143. +5 −0 src/effectivejava/chapter8/item52/Wine.java
  144. +46 −0 src/effectivejava/chapter8/item53/Varargs.java
  145. +47 −0 src/effectivejava/chapter8/item55/Max.java
  146. +17 −0 src/effectivejava/chapter8/item55/ParentPid.java
  147. +34 −0 src/effectivejava/chapter9/item58/Card.java
  148. +22 −0 src/effectivejava/chapter9/item58/DiceRolls.java
  149. +14 −0 src/effectivejava/chapter9/item59/Curl.java
  150. +21 −0 src/effectivejava/chapter9/item59/RandomBug.java
  151. +20 −0 src/effectivejava/chapter9/item60/BigDecimalChange.java
  152. +15 −0 src/effectivejava/chapter9/item60/Change.java
  153. +14 −0 src/effectivejava/chapter9/item60/IntChange.java
  154. +20 −0 src/effectivejava/chapter9/item61/BrokenComparator.java
  155. +11 −0 src/effectivejava/chapter9/item61/Unbelievable.java
  156. +52 −0 src/effectivejava/chapter9/item65/ReflectiveInstantiation.java
@@ -0,0 +1,65 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# I [jjb] added this to ignore emacs autosaves
*~
# User-specific stuff
# .idea/**/workspace.xml
# .idea/**/tasks.xml
# .idea/**/usage.statistics.xml
# .idea/**/dictionaries
# .idea/**/shelf
.idea
# Sensitive or high-churn files
# .idea/**/dataSources/
# .idea/**/dataSources.ids
# .idea/**/dataSources.local.xml
# .idea/**/sqlDataSources.xml
# .idea/**/dynamic.xml
# .idea/**/uiDesigner.xml
# .idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# CMake
cmake-build-*/
# Mongo Explorer plugin
# .idea/**/mongoSettings.xml
# File-based project format
*.iws
*.iml
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
# .idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
# .idea/httpRequests
@@ -0,0 +1,27 @@
package effectivejava.chapter10.item74;
public class IndexOutOfBoundsException extends RuntimeException {
private final int lowerBound;
private final int upperBound;
private final int index;
/**
* Constructs an IndexOutOfBoundsException.
*
* @param lowerBound the lowest legal index value
* @param upperBound the highest legal index value plus one
* @param index the actual index value
*/
public IndexOutOfBoundsException(int lowerBound, int upperBound,
int index) {
// Generate a detail message that captures the failure
super(String.format(
"Lower bound: %d, Upper bound: %d, Index: %d",
lowerBound, upperBound, index));
// Save failure information for programmatic access
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.index = index;
}
}
@@ -0,0 +1,20 @@
package effectivejava.chapter11.item78.brokenstopthread;
import java.util.concurrent.*;
// Broken! - How long would you expect this program to run? (Page 312)
public class StopThread {
private static boolean stopRequested;
public static void main(String[] args)
throws InterruptedException {
Thread backgroundThread = new Thread(() -> {
int i = 0;
while (!stopRequested)
i++;
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
stopRequested = true;
}
}
@@ -0,0 +1,28 @@
package effectivejava.chapter11.item78.fixedstopthread1;
import java.util.concurrent.*;
// Properly synchronized cooperative thread termination
public class StopThread {
private static boolean stopRequested;
private static synchronized void requestStop() {
stopRequested = true;
}
private static synchronized boolean stopRequested() {
return stopRequested;
}
public static void main(String[] args)
throws InterruptedException {
Thread backgroundThread = new Thread(() -> {
int i = 0;
while (!stopRequested())
i++;
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
requestStop();
}
}
@@ -0,0 +1,20 @@
package effectivejava.chapter11.item78.fixedstopthread2;
import java.util.concurrent.*;
// Cooperative thread termination with a volatile field
public class StopThread {
private static volatile boolean stopRequested;
public static void main(String[] args)
throws InterruptedException {
Thread backgroundThread = new Thread(() -> {
int i = 0;
while (!stopRequested)
i++;
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
stopRequested = true;
}
}
@@ -0,0 +1,30 @@
package effectivejava.chapter11.item79;
import java.util.*;
// Reusable forwarding class (Page XXX)
public class ForwardingSet<E> implements Set<E> {
private final Set<E> s;
public ForwardingSet(Set<E> s) { this.s = s; }
public void clear() { s.clear(); }
public boolean contains(Object o) { return s.contains(o); }
public boolean isEmpty() { return s.isEmpty(); }
public int size() { return s.size(); }
public Iterator<E> iterator() { return s.iterator(); }
public boolean add(E e) { return s.add(e); }
public boolean remove(Object o) { return s.remove(o); }
public boolean containsAll(Collection<?> c)
{ return s.containsAll(c); }
public boolean addAll(Collection<? extends E> c)
{ return s.addAll(c); }
public boolean removeAll(Collection<?> c)
{ return s.removeAll(c); }
public boolean retainAll(Collection<?> c)
{ return s.retainAll(c); }
public Object[] toArray() { return s.toArray(); }
public <T> T[] toArray(T[] a) { return s.toArray(a); }
@Override public boolean equals(Object o)
{ return s.equals(o); }
@Override public int hashCode() { return s.hashCode(); }
@Override public String toString() { return s.toString(); }
}
@@ -0,0 +1,71 @@
package effectivejava.chapter11.item79;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
// Broken - invokes alien method from synchronized block!
public class ObservableSet<E> extends ForwardingSet<E> {
public ObservableSet(Set<E> set) { super(set); }
// private final List<SetObserver<E>> observers
// = new ArrayList<>();
// public void addObserver(SetObserver<E> observer) {
// synchronized(observers) {
// observers.add(observer);
// }
// }
//
// public boolean removeObserver(SetObserver<E> observer) {
// synchronized(observers) {
// return observers.remove(observer);
// }
// }
// private void notifyElementAdded(E element) {
// synchronized(observers) {
// for (SetObserver<E> observer : observers)
// observer.added(this, element);
// }
// }
// // Alien method moved outside of synchronized block - open calls
// private void notifyElementAdded(E element) {
// List<SetObserver<E>> snapshot = null;
// synchronized(observers) {
// snapshot = new ArrayList<>(observers);
// }
// for (SetObserver<E> observer : snapshot)
// observer.added(this, element);
// }
// Thread-safe observable set with CopyOnWriteArrayList
private final List<SetObserver<E>> observers =
new CopyOnWriteArrayList<>();
public void addObserver(SetObserver<E> observer) {
observers.add(observer);
}
public boolean removeObserver(SetObserver<E> observer) {
return observers.remove(observer);
}
private void notifyElementAdded(E element) {
for (SetObserver<E> observer : observers)
observer.added(this, element);
}
@Override public boolean add(E element) {
boolean added = super.add(element);
if (added)
notifyElementAdded(element);
return added;
}
@Override public boolean addAll(Collection<? extends E> c) {
boolean result = false;
for (E element : c)
result |= add(element); // Calls notifyElementAdded
return result;
}
}
@@ -0,0 +1,7 @@
package effectivejava.chapter11.item79;
// Set obeserver callback interface - Page 266
public interface SetObserver<E> {
// Invoked when an element is added to the observable set
void added(ObservableSet<E> set, E element);
}
@@ -0,0 +1,15 @@
package effectivejava.chapter11.item79;
import java.util.*;
// Simple test of ObservableSet - Page 318
public class Test1 {
public static void main(String[] args) {
ObservableSet<Integer> set =
new ObservableSet<>(new HashSet<>());
set.addObserver((s, e) -> System.out.println(e));
for (int i = 0; i < 100; i++)
set.add(i);
}
}
@@ -0,0 +1,21 @@
package effectivejava.chapter11.item79;
import java.util.*;
// More complex test of ObservableSet - Page 318-9
public class Test2 {
public static void main(String[] args) {
ObservableSet<Integer> set =
new ObservableSet<>(new HashSet<>());
set.addObserver(new SetObserver<>() {
public void added(ObservableSet<Integer> s, Integer e) {
System.out.println(e);
if (e == 23)
s.removeObserver(this);
}
});
for (int i = 0; i < 100; i++)
set.add(i);
}
}
@@ -0,0 +1,34 @@
package effectivejava.chapter11.item79;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// Simple test of ObservableSet - Page 319
public class Test3 {
public static void main(String[] args) {
ObservableSet<Integer> set =
new ObservableSet<>(new HashSet<>());
// Observer that uses a background thread needlessly
set.addObserver(new SetObserver<>() {
public void added(ObservableSet<Integer> s, Integer e) {
System.out.println(e);
if (e == 23) {
ExecutorService exec =
Executors.newSingleThreadExecutor();
try {
exec.submit(() -> s.removeObserver(this)).get();
} catch (ExecutionException | InterruptedException ex) {
throw new AssertionError(ex);
} finally {
exec.shutdown();
}
}
}
});
for (int i = 0; i < 100; i++)
set.add(i);
}
}
@@ -0,0 +1,34 @@
package effectivejava.chapter11.item81;
import java.util.concurrent.*;
// Simple framework for timing concurrent execution 327
public class ConcurrentTimer {
private ConcurrentTimer() { } // Noninstantiable
public static long time(Executor executor, int concurrency,
Runnable action) throws InterruptedException {
CountDownLatch ready = new CountDownLatch(concurrency);
CountDownLatch start = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; i++) {
executor.execute(() -> {
ready.countDown(); // Tell timer we're ready
try {
start.await(); // Wait till peers are ready
action.run();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
done.countDown(); // Tell timer we're done
}
});
}
ready.await(); // Wait for all workers to be ready
long startNanos = System.nanoTime();
start.countDown(); // And they're off!
done.await(); // Wait for all workers to finish
return System.nanoTime() - startNanos;
}
}
Oops, something went wrong.

0 comments on commit b4dd083

Please sign in to comment.