Radically reorganized

Plus many new examples in "streams" chapter.
This commit is contained in:
Bruce Eckel 2015-11-11 20:20:04 -08:00
parent 0edda16067
commit bc0026c3e1
257 changed files with 2024 additions and 422 deletions

View File

@ -31,8 +31,7 @@
<exclude name="**/*.cpp" />
<exclude name="**/VendingMachineInput.txt" />
<exclude name="**/Trash.dat" />
<exclude name="**/DDTrash.dat" />
<exclude name="**/VTrash.dat" />
<exclude name="**/Cheese.dat" />
<exclude name="**/log.prop"/>
<exclude name="**/runall.ps1" />
</fileset>

View File

@ -74,7 +74,13 @@
<target name="base">
<javac includeantruntime="false"
classpath="${java.class.path};${basedir};${basedir}/.."
srcdir="${basedir}/../com/mindviewinc/">
srcdir="${basedir}/../onjava/">
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
<javac includeantruntime="false"
classpath="${java.class.path};${basedir};${basedir}/.."
srcdir="${basedir}/../com/">
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
@ -90,6 +96,12 @@
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
<javac includeantruntime="false"
classpath="${java.class.path};${basedir};${basedir}/.."
srcdir="${basedir}/../enums/menu/">
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
</target>
<target

View File

@ -2,7 +2,7 @@
// Creating non-embedded tests.
package annotations;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnitComposition {
AtUnitExample1 testObject = new AtUnitExample1();

View File

@ -1,7 +1,7 @@
// annotations/AtUnitExample1.java
package annotations;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnitExample1 {
public String methodOne() {

View File

@ -3,7 +3,7 @@
package annotations;
import java.io.*;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnitExample2 {
public String methodOne() {

View File

@ -1,7 +1,7 @@
// annotations/AtUnitExample3.java
package annotations;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnitExample3 {
private int n;

View File

@ -2,7 +2,7 @@
package annotations;
import java.util.*;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnitExample4 {
static String theory = "All brontosauruses " +

View File

@ -2,7 +2,7 @@
package annotations;
import java.io.*;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnitExample5 {
private String text;

View File

@ -2,7 +2,7 @@
// Creating non-embedded tests.
package annotations;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnitExternalTest extends AtUnitExample1 {
@Test boolean _methodOne() {

View File

@ -2,7 +2,7 @@
package annotations;
import java.util.*;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class HashSetTest {
HashSet<String> testObject = new HashSet<>();

View File

@ -2,7 +2,7 @@
// Applying @Unit to generics.
package annotations;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import onjava.*;
public class StackLStringTest extends StackL<String> {
@Test void _push() {

View File

@ -1,7 +1,7 @@
// arrays/AlphabeticSearch.java
// Searching with a Comparator.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AlphabeticSearch {
public static void main(String[] args) {

View File

@ -7,6 +7,7 @@ public class ArrayOptions {
// Arrays of objects:
BerylliumSphere[] a; // Local uninitialized variable
BerylliumSphere[] b = new BerylliumSphere[5];
// The references inside the array are
// automatically initialized to null:
System.out.println("b: " + Arrays.toString(b));
@ -14,14 +15,17 @@ public class ArrayOptions {
for(int i = 0; i < c.length; i++)
if(c[i] == null) // Can test for null reference
c[i] = new BerylliumSphere();
// Aggregate initialization:
BerylliumSphere[] d = { new BerylliumSphere(),
new BerylliumSphere(), new BerylliumSphere()
};
// Dynamic aggregate initialization:
a = new BerylliumSphere[]{
new BerylliumSphere(), new BerylliumSphere(),
};
// (Trailing comma is optional in both cases)
System.out.println("a.length = " + a.length);
System.out.println("b.length = " + b.length);
@ -33,6 +37,7 @@ public class ArrayOptions {
// Arrays of primitives:
int[] e; // Null reference
int[] f = new int[5];
// The primitives inside the array are
// automatically initialized to zero:
System.out.println("f: " + Arrays.toString(f));
@ -40,6 +45,7 @@ public class ArrayOptions {
for(int i = 0; i < g.length; i++)
g[i] = i*i;
int[] h = { 11, 47, 93 };
// Compile error: variable e not initialized:
//!print("e.length = " + e.length);
System.out.println("f.length = " + f.length);

View File

@ -2,7 +2,7 @@
// Using Arrays.binarySearch().
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
public class ArraySearching {
public static void main(String[] args) {

View File

@ -2,7 +2,7 @@
// Implementing Comparable in a class.
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
public class CompType implements Comparable<CompType> {
int i;

View File

@ -1,7 +1,7 @@
// arrays/ComparatorTest.java
// Implementing a Comparator for a class.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
class CompTypeComparator implements Comparator<CompType> {
public int compare(CompType o1, CompType o2) {

View File

@ -1,6 +1,6 @@
// arrays/PrimitiveConversionDemonstration.java
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class PrimitiveConversionDemonstration {
public static void main(String[] args) {

View File

@ -1,5 +1,5 @@
// arrays/RandomSuppliersTest.java
import com.mindviewinc.util.*;
import onjava.*;
public class RandomSuppliersTest {
public static void main(String[] args) {

View File

@ -1,7 +1,7 @@
// arrays/Reverse.java
// The Collections.reverseOrder() Comparator
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class Reverse {
public static void main(String[] args) {

View File

@ -1,7 +1,7 @@
// arrays/StringSorting.java
// Sorting an array of Strings.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class StringSorting {
public static void main(String[] args) {

View File

@ -1,6 +1,6 @@
// arrays/SuppliersTest.java
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
public class SuppliersTest {
public static int size = 10;

View File

@ -1,7 +1,7 @@
// arrays/TestArrayGeneration.java
// Test the tools that use generators to fill arrays.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class TestArrayGeneration {
public static void main(String[] args) {

View File

@ -1,6 +1,6 @@
// arrays/TestGenerated.java
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class TestGenerated {
public static void main(String[] args) {

View File

@ -30,28 +30,35 @@
polymorphism/build.xml
interfaces/build.xml
innerclasses/build.xml
functions/build.xml
containers/build.xml
streams/build.xml
exceptions/build.xml
strings/build.xml
typeinfo/build.xml
generics/build.xml
arrays/build.xml
containersindepth/build.xml
io/build.xml
xml/build.xml
files/build.xml
enums/build.xml
annotations/build.xml
concurrency/build.xml
ui/build.xml
swt/build.xml
patterns/build.xml
references/build.xml
assertions/build.xml
unittesting/build.xml
debugging/build.xml
logging/build.xml
assertions/build.xml
references/build.xml
iostreams/build.xml
standardio/build.xml
newio/build.xml
compression/build.xml
serialization/build.xml
preferences/build.xml
network/build.xml
remote/build.xml
logging/build.xml
debugging/build.xml
staticchecking/build.xml
"/>

View File

@ -9,13 +9,6 @@
<target name="run" description="Compile and run" depends="build">
<jrun cls="com.mindviewinc.atunit.AtUnit" dirpath="../com/mindviewinc/atunit" />
<jrun cls="com.mindviewinc.atunit.ClassNameFinder" dirpath="../com/mindviewinc/atunit" />
<jrun cls="com.mindviewinc.util.ContainerMethodDifferences" dirpath="../com/mindviewinc/util" />
<jrun cls="com.mindviewinc.util.CountingIntegerList" dirpath="../com/mindviewinc/util" />
<jrun cls="com.mindviewinc.util.CountingMapData" dirpath="../com/mindviewinc/util" />
<jrun cls="com.mindviewinc.util.Countries" dirpath="../com/mindviewinc/util" />
<jrun cls="com.mindviewinc.util.Directory" dirpath="../com/mindviewinc/util" />
<jrun cls="com.mindviewinc.util.Hex" dirpath="../com/mindviewinc/util" />
<jrun cls="com.mindviewinc.util.TextFile" dirpath="../com/mindviewinc/util" />
</target>
</project>

View File

@ -4,7 +4,7 @@ package com.mindviewinc.atunit;
import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtUnit implements ProcessFiles.Strategy {
static Class<?> testClass;

View File

@ -2,7 +2,7 @@
package com.mindviewinc.atunit;
import java.io.*;
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class ClassNameFinder {
public static String thisClass(byte[] classBytes) {

View File

@ -1,16 +0,0 @@
// com/mindviewinc/util/FiveTuple.java
package com.mindviewinc.util;
public class FiveTuple<A, B, C, D, E>
extends FourTuple<A, B, C, D> {
public final E fifth;
public FiveTuple(A a, B b, C c, D d, E e) {
super(a, b, c, d);
fifth = e;
}
@Override
public String toString() {
return "(" + first + ", " + second + ", " +
third + ", " + fourth + ", " + fifth + ")";
}
}

View File

@ -1,16 +0,0 @@
// com/mindviewinc/util/FourTuple.java
package com.mindviewinc.util;
public class FourTuple<A, B, C, D>
extends ThreeTuple<A, B, C> {
public final D fourth;
public FourTuple(A a, B b, C c, D d) {
super(a, b, c);
fourth = d;
}
@Override
public String toString() {
return "(" + first + ", " + second + ", " +
third + ", " + fourth + ")";
}
}

View File

@ -1,3 +0,0 @@
// com/mindviewinc/util/Null.java
package com.mindviewinc.util;
public interface Null {}

View File

@ -1,14 +0,0 @@
// com/mindviewinc/util/ThreeTuple.java
package com.mindviewinc.util;
public class ThreeTuple<A,B,C> extends TwoTuple<A, B> {
public final C third;
public ThreeTuple(A a, B b, C c) {
super(a, b);
third = c;
}
@Override
public String toString() {
return "(" + first + ", " + second + ", " + third +")";
}
}

View File

@ -1,21 +0,0 @@
// com/mindviewinc/util/Tuple.java
// Tuple library using type argument inference.
package com.mindviewinc.util;
public class Tuple {
public static <A, B> TwoTuple<A, B> tuple(A a, B b) {
return new TwoTuple<>(a, b);
}
public static <A, B, C> ThreeTuple<A, B, C>
tuple(A a, B b, C c) {
return new ThreeTuple<>(a, b, c);
}
public static <A, B, C, D> FourTuple<A, B, C, D>
tuple(A a, B b, C c, D d) {
return new FourTuple<>(a, b, c, d);
}
public static <A, B, C, D, E>
FiveTuple<A, B, C, D, E> tuple(A a, B b, C c, D d, E e) {
return new FiveTuple<>(a, b, c, d, e);
}
}

View File

@ -1,12 +0,0 @@
// com/mindviewinc/util/TwoTuple.java
package com.mindviewinc.util;
public class TwoTuple<A, B> {
public final A first;
public final B second;
public TwoTuple(A a, B b) { first = a; second = b; }
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
}

View File

@ -2,7 +2,7 @@
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class AtomicIntegerTest implements Runnable {
private AtomicInteger i = new AtomicInteger(0);

View File

@ -1,7 +1,7 @@
// concurrency/DaemonFromFactory.java
// Using a Thread Factory to create daemons.
import java.util.concurrent.*;
import com.mindviewinc.util.*;
import onjava.*;
public class DaemonFromFactory implements Runnable {
@Override

View File

@ -2,7 +2,7 @@
import java.util.concurrent.*;
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
class ExchangerProducer<T> implements Runnable {
private Supplier<T> generator;

View File

@ -3,7 +3,7 @@
// Rough comparison of thread-safe List performance.
import java.util.concurrent.*;
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
abstract class ListTest extends Tester<List<Integer>> {
ListTest(String testId, int nReaders, int nWriters) {

View File

@ -3,7 +3,7 @@
// Rough comparison of thread-safe Map performance.
import java.util.concurrent.*;
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
abstract class MapTest
extends Tester<Map<Integer,Integer>> {

View File

@ -1,7 +1,7 @@
// concurrency/Tester.java
// Framework to test performance of concurrency containers.
import java.util.concurrent.*;
import com.mindviewinc.util.*;
import onjava.*;
public abstract class Tester<C> {
static int testReps = 10;

View File

@ -1,5 +1,5 @@
// containers/AdapterMethodIdiom.java
// The "Adapter Method" idiom uses for-each
// The "Adapter Method" idiom uses for-in
// with additional kinds of Iterables.
import java.util.*;

View File

@ -11,7 +11,7 @@ public class ApplesAndOrangesWithGenerics {
for(Apple apple : apples) {
System.out.println(apple.id());
}
// Using for-each:
// Using for-in:
for(Apple c : apples)
System.out.println(c.id());
}

View File

@ -9,7 +9,7 @@ public class ArrayIsNotIterable {
public static void main(String[] args) {
test(Arrays.asList(1, 2, 3));
String[] strings = { "A", "B", "C" };
// An array works in for-each, but it's not Iterable:
// An array works in for-in, but it's not Iterable:
//! test(strings);
// You must explicitly convert it to an Iterable:
test(Arrays.asList(strings));

View File

@ -1,5 +1,5 @@
// containers/ContainerMethods.java
import com.mindviewinc.util.*;
import onjava.*;
public class ContainerMethods {
public static void main(String[] args) {
@ -8,7 +8,7 @@ public class ContainerMethods {
}
/* Output:
Collection: [add, addAll, clear, contains, containsAll,
equals, for-each, hashCode, isEmpty, iterator,
equals, forEach, hashCode, isEmpty, iterator,
parallelStream, remove, removeAll, removeIf, retainAll,
size, spliterator, stream, toArray]
Interfaces in Collection: [Iterable]
@ -42,7 +42,7 @@ Interfaces in Queue: [Collection]
PriorityQueue extends Queue, adds: [comparator]
Interfaces in PriorityQueue: [Serializable]
Map: [clear, compute, computeIfAbsent, computeIfPresent,
containsKey, containsValue, entrySet, equals, for-each, get,
containsKey, containsValue, entrySet, equals, forEach, get,
getOrDefault, hashCode, isEmpty, keySet, merge, put,
putAll, putIfAbsent, remove, replace, replaceAll, size,
values]

View File

@ -1,8 +1,8 @@
// containers/ForEachCollections.java
// All collections work with for-each.
// containers/ForInCollections.java
// All collections work with for-in.
import java.util.*;
public class ForEachCollections {
public class ForInCollections {
public static void main(String[] args) {
Collection<String> cs = new LinkedList<>();
Collections.addAll(cs,

View File

@ -1,5 +1,5 @@
// containers/IterableClass.java
// Anything Iterable works with for-each.
// Anything Iterable works with for-in.
import java.util.*;
public class IterableClass implements Iterable<String> {

View File

@ -2,8 +2,7 @@
public class StackCollision {
public static void main(String[] args) {
com.mindviewinc.util.Stack<String> stack =
new com.mindviewinc.util.Stack<>();
onjava.Stack<String> stack = new onjava.Stack<>();
for(String s : "My dog has fleas".split(" "))
stack.push(s);
while(!stack.empty())

View File

@ -1,5 +1,5 @@
// containers/StackTest.java
import com.mindviewinc.util.*;
import onjava.*;
public class StackTest {
public static void main(String[] args) {

View File

@ -1,6 +1,6 @@
// containers/UniqueWords.java
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class UniqueWords {
public static void main(String[] args) {

View File

@ -1,7 +1,7 @@
// containers/UniqueWordsAlphabetic.java
// Producing an alphabetic listing.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class UniqueWordsAlphabetic {
public static void main(String[] args) {

View File

@ -17,7 +17,7 @@
<jrun cls="ContainerMethods" />
<jrun cls="CrossContainerIteration" />
<jrun cls="EnvironmentVariables" />
<jrun cls="ForEachCollections" />
<jrun cls="ForInCollections" />
<jrun cls="GenericsAndUpcasting" />
<jrun cls="InterfaceVsIterator" />
<jrun cls="IterableClass" />

View File

@ -1,7 +1,7 @@
// containersindepth/CollectionDataGeneration.java
// Using the Suppliers defined in the Arrays chapter.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class CollectionDataGeneration {
public static void main(String[] args) {

View File

@ -1,7 +1,7 @@
// containersindepth/CollectionDataTest.java
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
class Government implements Supplier<String> {
String[] foundation = ("strange women lying in ponds " +

View File

@ -1,7 +1,7 @@
// containersindepth/CollectionMethods.java
// Things you can do with all Collections.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class CollectionMethods {
public static void main(String[] args) {

View File

@ -11,7 +11,7 @@ public class CountedString {
s = str;
created.add(s);
// id is the total number of instances
// of this string in use by CountedString:
// of this String in use by CountedString:
for(String s2 : created)
if(s2.equals(s))
id++;

View File

@ -1,5 +1,5 @@
// containersindepth/DequeTest.java
import com.mindviewinc.util.*;
import onjava.*;
public class DequeTest {
static void fillTest(Deque<Integer> deque) {

View File

@ -1,7 +1,7 @@
// containersindepth/Enumerations.java
// Java 1.0/1.1 Vector and Enumeration.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class Enumerations {
public static void main(String[] args) {

View File

@ -1,7 +1,7 @@
// containersindepth/LinkedHashMapDemo.java
// What you can do with a LinkedHashMap.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class LinkedHashMapDemo {
public static void main(String[] args) {

View File

@ -2,7 +2,7 @@
// Demonstrates performance differences in Lists.
// {Args: 100 500} Small to keep build testing short
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class ListPerformance {
static Random rand = new Random();

View File

@ -1,7 +1,7 @@
// containersindepth/Lists.java
// Things you can do with Lists.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class Lists {
private static boolean b;

View File

@ -1,7 +1,7 @@
// containersindepth/MapDataTest.java
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
class Letters implements Supplier<Pair<Integer,String>>,
Iterable<Integer> {

View File

@ -2,7 +2,7 @@
// Things you can do with Maps.
import java.util.concurrent.*;
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class Maps {
public static void printKeys(Map<Integer,String> map) {

View File

@ -3,7 +3,7 @@
import java.util.concurrent.*;
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
public class QueueBehavior {
private static int count = 10;

View File

@ -1,7 +1,7 @@
// containersindepth/ReadOnly.java
// Using the Collections.unmodifiable methods.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class ReadOnly {
static Collection<String> data =

View File

@ -1,7 +1,7 @@
// containersindepth/SimpleHashMap.java
// A demonstration hashed Map.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class SimpleHashMap<K, V> extends AbstractMap<K, V> {
// Choose a prime number for the hash table

View File

@ -1,7 +1,7 @@
// containersindepth/SlowMap.java
// A Map implemented with ArrayLists.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class SlowMap<K, V> extends AbstractMap<K, V> {
private List<K> keys = new ArrayList<>();

View File

@ -1,7 +1,7 @@
// containersindepth/SortedMapDemo.java
// What you can do with a TreeMap.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class SortedMapDemo {
public static void main(String[] args) {

View File

@ -28,8 +28,8 @@ public class Stacks {
// Using the Stack class from
// the Containers Chapter:
com.mindviewinc.util.Stack<String> stack2 =
new com.mindviewinc.util.Stack<>();
onjava.Stack<String> stack2 =
new onjava.Stack<>();
for(Month m : Month.values())
stack2.push(m.toString());
System.out.println("stack2 = " + stack2);

View File

@ -1,6 +1,6 @@
// control/BreakAndContinue.java
// Demonstrates break and continue keywords.
import static com.mindviewinc.util.Range.*;
import static onjava.Range.*;
public class BreakAndContinue {
public static void main(String[] args) {
@ -10,7 +10,7 @@ public class BreakAndContinue {
System.out.println(i + " ");
}
System.out.println();
// Using forEach:
// Using for-in:
for(int i : range(100)) {
if(i == 74) break; // Out of for loop
if(i % 9 != 0) continue; // Next iteration

View File

@ -1,7 +1,7 @@
// control/ForEachFloat.java
// control/ForInFloat.java
import java.util.*;
public class ForEachFloat {
public class ForInFloat {
public static void main(String[] args) {
Random rand = new Random(47);
float f[] = new float[10];

View File

@ -1,7 +1,7 @@
// control/ForEachInt.java
import static com.mindviewinc.util.Range.*;
// control/ForInInt.java
import static onjava.Range.*;
public class ForEachInt {
public class ForInInt {
public static void main(String[] args) {
for(int i : range(10)) // 0..9
System.out.print(i + " ");
@ -12,10 +12,14 @@ public class ForEachInt {
for(int i : range(5, 20, 3)) // 5..20 step 3
System.out.print(i + " ");
System.out.println();
for(int i : range(20, 5, -3)) // Count down
System.out.print(i + " ");
System.out.println();
}
}
/* Output:
0 1 2 3 4 5 6 7 8 9
5 6 7 8 9
5 8 11 14 17
20 17 14 11 8
*/

View File

@ -1,6 +1,6 @@
// control/ForEachString.java
// control/ForInString.java
public class ForEachString {
public class ForInString {
public static void main(String[] args) {
for(char c : "An African Swallow".toCharArray())
System.out.println(c + " ");

View File

@ -9,9 +9,9 @@
<target name="run" description="Compile and run" depends="build">
<jrun cls="BreakAndContinue" />
<jrun cls="CommaOperator" />
<jrun cls="ForEachFloat" />
<jrun cls="ForEachInt" />
<jrun cls="ForEachString" />
<jrun cls="ForInFloat" />
<jrun cls="ForInInt" />
<jrun cls="ForInString" />
<jrun cls="IfElse" />
<jrun cls="IfElse2" />
<jrun cls="LabeledFor" />

View File

@ -14,7 +14,7 @@ public class EnumClass {
System.out.println(s.name());
System.out.println("----------------------");
}
// Produce an enum value from a string name:
// Produce an enum value from a String name:
for(String s : "HANGING CRAWLING GROUND".split(" ")) {
Shrubbery shrub = Enum.valueOf(Shrubbery.class, s);
System.out.println(shrub);

View File

@ -1,7 +1,7 @@
// enums/PostOffice.java
// Modeling a post office.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
class Mail {
// The NO's lower the probability of random selection:

View File

@ -1,5 +1,5 @@
// enums/RandomTest.java
import com.mindviewinc.util.*;
import onjava.*;
enum Activity { SITTING, LYING, STANDING, HOPPING,
RUNNING, DODGING, JUMPING, FALLING, FLYING }

View File

@ -2,7 +2,7 @@
// Analyzing enums using reflection.
import java.lang.reflect.*;
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
enum Explore { HERE, THERE }

View File

@ -1,7 +1,7 @@
// enums/RoShamBo.java
// Common tools for RoShamBo examples.
package enums;
import com.mindviewinc.util.*;
import onjava.*;
public class RoShamBo {
public static <T extends Competitor<T>>

View File

@ -1,6 +1,6 @@
// enums/SecurityCategory.java
// More succinct subcategorization of enums.
import com.mindviewinc.util.*;
import onjava.*;
enum SecurityCategory {
STOCK(Security.Stock.class), BOND(Security.Bond.class);

View File

@ -2,7 +2,7 @@
// {Args: VendingMachineInput.txt}
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import onjava.*;
enum Category {
MONEY(Input.NICKEL, Input.DIME,

View File

@ -1,6 +1,6 @@
// enums/menu/Course.java
package enums.menu;
import com.mindviewinc.util.*;
import onjava.*;
public enum Course {
APPETIZER(Food.Appetizer.class),

View File

@ -1,6 +1,6 @@
// enums/menu/Meal2.java
package enums.menu;
import com.mindviewinc.util.*;
import onjava.*;
public enum Meal2 {
APPETIZER(Food.Appetizer.class),

View File

@ -89,7 +89,8 @@ public class DynamicFields {
df.setField("d", "A new value for d");
df.setField("number3", 11);
System.out.println("df: " + df);
System.out.println("df.getField(\"d\") : " + df.getField("d"));
System.out.println("df.getField(\"d\") : "
+ df.getField("d"));
Object field = df.setField("d", null); // Exception
} catch(NoSuchFieldException |
DynamicFieldsException e) {

View File

@ -1,4 +1,4 @@
// io/DirList.java
// files/DirList.java
// Display a directory listing using regular expressions.
// {Args: "D.*\.java"}
import java.util.regex.*;

View File

@ -1,4 +1,4 @@
// io/DirList2.java
// files/DirList2.java
// Uses anonymous inner classes.
// {Args: "D.*\.java"}
import java.util.regex.*;

View File

@ -1,4 +1,4 @@
// io/DirList3.java
// files/DirList3.java
// Building the anonymous inner class "in-place."
// {Args: "D.*\.java"}
import java.util.regex.*;

View File

@ -1,7 +1,7 @@
// io/DirectoryDemo.java
// files/DirectoryDemo.java
// Sample use of Directory utilities.
import java.io.*;
import com.mindviewinc.util.*;
import onjava.*;
public class DirectoryDemo {
public static void main(String[] args) {

View File

@ -1,4 +1,4 @@
// io/MakeDirectories.java
// files/MakeDirectories.java
// Demonstrates using the File class to
// create directories and manipulate files.
// {Args: MakeDirectoriesTest}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<project default="run">
<property name="chapter" value="io"/>
<property name="chapter" value="files"/>
<property name="excludedfiles" value=""/>
<import file="../Ant-Common.xml"/>
<import file="../Ant-Clean.xml"/>

View File

@ -1,5 +1,5 @@
// generics/BasicSupplierDemo.java
import com.mindviewinc.util.*;
import onjava.*;
import java.util.function.*;
public class BasicSupplierDemo {

View File

@ -1,21 +1,21 @@
// generics/DynamicProxyMixin.java
import java.lang.reflect.*;
import java.util.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Tuple.*;
import onjava.*;
import static onjava.Tuple.*;
class MixinProxy implements InvocationHandler {
Map<String,Object> delegatesByMethod;
@SuppressWarnings("unchecked")
public MixinProxy(TwoTuple<Object,Class<?>>... pairs) {
public MixinProxy(Tuple2<Object,Class<?>>... pairs) {
delegatesByMethod = new HashMap<>();
for(TwoTuple<Object,Class<?>> pair : pairs) {
for(Method method : pair.second.getMethods()) {
for(Tuple2<Object,Class<?>> pair : pairs) {
for(Method method : pair._2.getMethods()) {
String methodName = method.getName();
// The first interface in the map
// implements the method.
if(!delegatesByMethod.containsKey(methodName))
delegatesByMethod.put(methodName, pair.first);
delegatesByMethod.put(methodName, pair._1);
}
}
}
@ -27,13 +27,13 @@ class MixinProxy implements InvocationHandler {
return method.invoke(delegate, args);
}
@SuppressWarnings("unchecked")
public static Object newInstance(TwoTuple... pairs) {
public static Object newInstance(Tuple2... pairs) {
Class[] interfaces = new Class[pairs.length];
for(int i = 0; i < pairs.length; i++) {
interfaces[i] = (Class)pairs[i].second;
interfaces[i] = (Class)pairs[i]._2;
}
ClassLoader cl =
pairs[0].first.getClass().getClassLoader();
pairs[0]._1.getClass().getClassLoader();
return Proxy.newProxyInstance(
cl, interfaces, new MixinProxy(pairs));
}

View File

@ -31,7 +31,7 @@ public class Functional {
// object can act as a collecting parameter, so it is
// returned at the end.
public static <T> Collector<T>
forEach(Iterable<T> seq, Collector<T> func) {
forIn(Iterable<T> seq, Collector<T> func) {
for(T t : seq)
func.function(t);
return func;
@ -132,10 +132,10 @@ public class Functional {
System.out.println(filter(li, new GreaterThan<>(4)));
System.out.println(forEach(li,
System.out.println(forIn(li,
new MultiplyingIntegerCollector()).result());
System.out.println(forEach(filter(li, new GreaterThan<>(4)),
System.out.println(forIn(filter(li, new GreaterThan<>(4)),
new MultiplyingIntegerCollector()).result());
MathContext mc = new MathContext(7);

View File

@ -1,5 +1,5 @@
// generics/PrimitiveGenericTest.java
import com.mindviewinc.util.*;
import onjava.*;
import java.util.function.*;
// Fill an array using a generator:

View File

@ -1,16 +1,16 @@
// generics/TupleList.java
// Combining generic types to make complex generic types.
import java.util.*;
import com.mindviewinc.util.*;
import onjava.*;
public class TupleList<A, B, C, D>
extends ArrayList<FourTuple<A, B, C, D>> {
extends ArrayList<Tuple4<A, B, C, D>> {
public static void main(String[] args) {
TupleList<Vehicle, Amphibian, String, Integer> tl =
new TupleList<>();
tl.add(TupleTest.h());
tl.add(TupleTest.h());
for(FourTuple<Vehicle,Amphibian,String,Integer> i: tl)
for(Tuple4<Vehicle,Amphibian,String,Integer> i: tl)
System.out.println(i);
}
}

View File

@ -1,33 +1,33 @@
// generics/TupleTest.java
import com.mindviewinc.util.*;
import onjava.*;
class Amphibian {}
class Vehicle {}
public class TupleTest {
static TwoTuple<String, Integer> f() {
static Tuple2<String, Integer> f() {
// Autoboxing converts the int to Integer:
return new TwoTuple<>("hi", 47);
return new Tuple2<>("hi", 47);
}
static ThreeTuple<Amphibian, String, Integer> g() {
return new ThreeTuple<>(new Amphibian(), "hi", 47);
static Tuple3<Amphibian, String, Integer> g() {
return new Tuple3<>(new Amphibian(), "hi", 47);
}
static
FourTuple<Vehicle, Amphibian, String, Integer> h() {
Tuple4<Vehicle, Amphibian, String, Integer> h() {
return
new FourTuple<>(
new Tuple4<>(
new Vehicle(), new Amphibian(), "hi", 47);
}
static
FiveTuple<Vehicle, Amphibian, String, Integer, Double> k(){
Tuple5<Vehicle, Amphibian, String, Integer, Double> k(){
return new
FiveTuple<>(
Tuple5<>(
new Vehicle(), new Amphibian(), "hi", 47, 11.1);
}
public static void main(String[] args) {
TwoTuple<String, Integer> ttsi = f();
Tuple2<String, Integer> ttsi = f();
System.out.println(ttsi);
// ttsi.first = "there"; // Compile error: final
// ttsi._1 = "there"; // Compile error: final
System.out.println(g());
System.out.println(h());
System.out.println(k());

View File

@ -1,26 +1,26 @@
// generics/TupleTest2.java
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Tuple.*;
import onjava.*;
import static onjava.Tuple.*;
public class TupleTest2 {
static TwoTuple<String, Integer> f() {
static Tuple2<String, Integer> f() {
return tuple("hi", 47);
}
static TwoTuple f2() { return tuple("hi", 47); }
static ThreeTuple<Amphibian, String, Integer> g() {
static Tuple2 f2() { return tuple("hi", 47); }
static Tuple3<Amphibian, String, Integer> g() {
return tuple(new Amphibian(), "hi", 47);
}
static
FourTuple<Vehicle, Amphibian, String, Integer> h() {
Tuple4<Vehicle, Amphibian, String, Integer> h() {
return tuple(new Vehicle(), new Amphibian(), "hi", 47);
}
static
FiveTuple<Vehicle, Amphibian, String, Integer, Double> k(){
Tuple5<Vehicle, Amphibian, String, Integer, Double> k(){
return tuple(new Vehicle(), new Amphibian(),
"hi", 47, 11.1);
}
public static void main(String[] args) {
TwoTuple<String, Integer> ttsi = f();
Tuple2<String, Integer> ttsi = f();
System.out.println(ttsi);
System.out.println(f2());
System.out.println(g());

View File

@ -1,7 +1,7 @@
// generics/WatercolorSets.java
import generics.watercolors.*;
import java.util.*;
import static com.mindviewinc.util.Sets.*;
import static onjava.Sets.*;
import static generics.watercolors.Watercolors.*;
public class WatercolorSets {

View File

@ -1,7 +1,7 @@
// housekeeping/TerminationCondition.java
// Using finalize() to detect an object that
// hasn't been properly cleaned up.
import com.mindviewinc.util.*;
import onjava.*;
class Book {
boolean checkedOut = false;

View File

@ -6,7 +6,7 @@ public class ConfigureLogging {
static Logger
lgr = Logger.getLogger("com"),
lgr2 = Logger.getLogger("com.mindviewinc"),
util= Logger.getLogger("com.mindviewinc.util"),
util= Logger.getLogger("onjava"),
test= Logger.getLogger("com.mindviewinc.test"),
rand = Logger.getLogger("random");
public ConfigureLogging() {
@ -42,7 +42,7 @@ public class ConfigureLogging {
/* Output:
Logger Name : net Level: SEVERE
Logger Name : com.mindviewinc Level: FINEST
Logger Name : com.mindviewinc.util Level: INFO
Logger Name : onjava Level: INFO
Logger Name : com.mindviewinc.test Level: FINER
Logger Name : random Level: SEVERE
___[ Error Output ]___

View File

@ -6,7 +6,7 @@ public class LoggingLevelManipulation {
private static Logger
lgr = Logger.getLogger("com"),
lgr2 = Logger.getLogger("com.mindviewinc"),
util= Logger.getLogger("com.mindviewinc.util"),
util= Logger.getLogger("onjava"),
test= Logger.getLogger("com.mindviewinc.test"),
rand = Logger.getLogger("random");
static void printLogMessages(Logger logger) {
@ -59,18 +59,18 @@ public class LoggingLevelManipulation {
}
/* Output:
-- printing levels -- net : null com.mindviewinc : null
com.mindviewinc.util : null com.mindviewinc.test : null
onjava : null com.mindviewinc.test : null
random : null
-- printing levels -- net : SEVERE com.mindviewinc : null
com.mindviewinc.util : null com.mindviewinc.test : null
onjava : null com.mindviewinc.test : null
random : null
net level: SEVERE
-- printing levels -- net : SEVERE com.mindviewinc : null
com.mindviewinc.util : FINEST com.mindviewinc.test : FINEST
onjava : FINEST com.mindviewinc.test : FINEST
random : FINEST
individual loggers set to FINEST
-- printing levels -- net : FINEST com.mindviewinc : null
com.mindviewinc.util : FINEST com.mindviewinc.test : FINEST
onjava : FINEST com.mindviewinc.test : FINEST
random : FINEST
net level: FINEST
___[ Error Output ]___
@ -88,13 +88,13 @@ printLogMessages
SEVERE: com.mindviewinc Severe
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.util Info
INFO: onjava Info
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
WARNING: com.mindviewinc.util Warning
WARNING: onjava Warning
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc.util Severe
SEVERE: onjava Severe
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.test Info
@ -127,13 +127,13 @@ printLogMessages
SEVERE: com.mindviewinc Severe
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.util Info
INFO: onjava Info
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
WARNING: com.mindviewinc.util Warning
WARNING: onjava Warning
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc.util Severe
SEVERE: onjava Severe
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.test Info
@ -172,13 +172,13 @@ printLogMessages
SEVERE: com.mindviewinc Severe
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.util Info
INFO: onjava Info
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
WARNING: com.mindviewinc.util Warning
WARNING: onjava Warning
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc.util Severe
SEVERE: onjava Severe
Jun 15, 2015 3:47:52 PM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.test Info

View File

@ -6,7 +6,7 @@ public class LoggingLevels {
private static Logger
lgr = Logger.getLogger("com"),
lgr2 = Logger.getLogger("com.mindviewinc"),
util= Logger.getLogger("com.mindviewinc.util"),
util= Logger.getLogger("onjava"),
test= Logger.getLogger("com.mindviewinc.test"),
rand = Logger.getLogger("random");
private static void logMessages() {

Some files were not shown because too many files have changed in this diff Show More