Reorganized

This commit is contained in:
Bruce Eckel 2015-11-03 12:00:44 -08:00
parent 5a25feafef
commit 0edda16067
969 changed files with 2477 additions and 4057 deletions

View File

@ -5,7 +5,7 @@
<description>
Ant build.xml for the source code for chapter ${chapter}
Bruce Eckel on Java
Bruce Eckel On Java
Code available at https://github.com/BruceEckel/OnJava-Examples
See installation instructions in README.md
See copyright notice in CopyRight.txt

View File

@ -1,5 +1,4 @@
// HelloDate.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
public class HelloDate {

View File

@ -1,18 +0,0 @@
# *On Java* Code Examples
## Get the Examples ##
[Click here](https://github.com/BruceEckel/OnJava-Examples/archive/master.zip) to download the zip file. Unzip the file into a directory of your choice.
## Build the Examples ##
1. Install the [latest version of Java 8 (JDK8)](http://www.oracle.com/technetwork/java/javase/downloads/index.html). (Use the 32-bit version on Windows because of some library issues).
1. Install Apache Ant. Follow the instructions [here](https://ant.apache.org/manual/install.html#getting).
1. Start a command prompt in the directory where you installed the examples. If your installation is successful, you should be able to type **ant build** and build everything. **ant run** will build AND run the examples. You will initially get some error messages instructing you to install specified packages (jar files), but eventually you should get through the whole build process successfully.
Report bugs in the examples or in the book [here](https://github.com/BruceEckel/OnJava-Examples/issues).
----------
**&copy;2015 Bruce Eckel, MindView LLC. All Rights Reserved. See *Copyright.txt* for usage details.**

View File

@ -1,5 +1,4 @@
// annotations/AtUnitComposition.java
// ©2015 MindView LLC: see Copyright.txt
// Creating non-embedded tests.
package annotations;
import com.mindviewinc.atunit.*;

View File

@ -1,5 +1,4 @@
// annotations/AtUnitExample1.java
// ©2015 MindView LLC: see Copyright.txt
package annotations;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;

View File

@ -1,5 +1,4 @@
// annotations/AtUnitExample2.java
// ©2015 MindView LLC: see Copyright.txt
// Assertions and exceptions can be used in @Tests.
package annotations;
import java.io.*;

View File

@ -1,5 +1,4 @@
// annotations/AtUnitExample3.java
// ©2015 MindView LLC: see Copyright.txt
package annotations;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;

View File

@ -1,10 +1,8 @@
// annotations/AtUnitExample4.java
// ©2015 MindView LLC: see Copyright.txt
package annotations;
import java.util.*;
import com.mindviewinc.atunit.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class AtUnitExample4 {
static String theory = "All brontosauruses " +
@ -15,6 +13,7 @@ public class AtUnitExample4 {
public AtUnitExample4(String word) { this.word = word; }
public String getWord() { return word; }
public String scrambleWord() {
// <* Improve this: *>
List<Character> chars = new ArrayList<>();
for(Character c : word.toCharArray())
chars.add(c);
@ -35,22 +34,22 @@ public class AtUnitExample4 {
return null;
}
@Test boolean words() {
print("'" + getWord() + "'");
System.out.println("'" + getWord() + "'");
return getWord().equals("are");
}
@Test boolean scramble1() {
// Change to a specific seed to get verifiable results:
rand = new Random(47);
print("'" + getWord() + "'");
System.out.println("'" + getWord() + "'");
String scrambled = scrambleWord();
print(scrambled);
System.out.println(scrambled);
return scrambled.equals("lAl");
}
@Test boolean scramble2() {
rand = new Random(74);
print("'" + getWord() + "'");
System.out.println("'" + getWord() + "'");
String scrambled = scrambleWord();
print(scrambled);
System.out.println(scrambled);
return scrambled.equals("tsaeborornussu");
}
public static void main(String[] args) throws Exception {

View File

@ -1,5 +1,4 @@
// annotations/AtUnitExample5.java
// ©2015 MindView LLC: see Copyright.txt
package annotations;
import java.io.*;
import com.mindviewinc.atunit.*;

View File

@ -1,5 +1,4 @@
// annotations/AtUnitExternalTest.java
// ©2015 MindView LLC: see Copyright.txt
// Creating non-embedded tests.
package annotations;
import com.mindviewinc.atunit.*;

View File

@ -1,5 +1,4 @@
// annotations/HashSetTest.java
// ©2015 MindView LLC: see Copyright.txt
package annotations;
import java.util.*;
import com.mindviewinc.atunit.*;

View File

@ -1,5 +1,4 @@
// annotations/PasswordUtils.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
public class PasswordUtils {

View File

@ -1,5 +1,4 @@
// annotations/SimulatingNull.java
// ©2015 MindView LLC: see Copyright.txt
import java.lang.annotation.*;
@Target(ElementType.METHOD)

View File

@ -1,5 +1,4 @@
// annotations/StackL.java
// ©2015 MindView LLC: see Copyright.txt
// A stack built on a linkedList.
package annotations;
import java.util.*;

View File

@ -1,5 +1,4 @@
// annotations/StackLStringTest.java
// ©2015 MindView LLC: see Copyright.txt
// Applying @Unit to generics.
package annotations;
import com.mindviewinc.atunit.*;

View File

@ -1,5 +1,4 @@
// annotations/Testable.java
// ©2015 MindView LLC: see Copyright.txt
package annotations;
import com.mindviewinc.atunit.*;

View File

@ -1,5 +1,4 @@
// annotations/UseCase.java
// ©2015 MindView LLC: see Copyright.txt
import java.lang.annotation.*;
@Target(ElementType.METHOD)

View File

@ -1,5 +1,4 @@
// annotations/UseCaseTracker.java
// ©2015 MindView LLC: see Copyright.txt
import java.lang.reflect.*;
import java.util.*;
@ -19,6 +18,7 @@ public class UseCaseTracker {
}
}
public static void main(String[] args) {
// <* Can't use Arrays.asList() for some reason *>
List<Integer> useCases = new ArrayList<>();
Collections.addAll(useCases, 47, 48, 49, 50);
trackUseCases(useCases, PasswordUtils.class);

View File

@ -1,5 +1,4 @@
// annotations/database/Constraints.java
// ©2015 MindView LLC: see Copyright.txt
package annotations.database;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// annotations/database/DBTable.java
// ©2015 MindView LLC: see Copyright.txt
package annotations.database;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// annotations/database/Member.java
// ©2015 MindView LLC: see Copyright.txt
package annotations.database;
@DBTable(name = "MEMBER")

View File

@ -1,5 +1,4 @@
// annotations/database/SQLInteger.java
// ©2015 MindView LLC: see Copyright.txt
package annotations.database;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// annotations/database/SQLString.java
// ©2015 MindView LLC: see Copyright.txt
package annotations.database;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// annotations/database/TableCreator.java
// ©2015 MindView LLC: see Copyright.txt
// Reflection-based annotation processor.
// {Args: annotations.database.Member}
package annotations.database;

View File

@ -1,5 +1,4 @@
// annotations/database/Uniqueness.java
// ©2015 MindView LLC: see Copyright.txt
// Sample of nested annotations
package annotations.database;

View File

@ -1,5 +1,4 @@
// annotations/ifx/ExtractInterface.java
// ©2015 MindView LLC: see Copyright.txt
// javac-based annotation processing.
package annotations.ifx;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// annotations/ifx/IfaceExtractorProcessor.java
// ©2015 MindView LLC: see Copyright.txt
// javac-based annotation processing.
package annotations.ifx;
import javax.annotation.processing.*;

View File

@ -1,5 +1,4 @@
// annotations/ifx/Multiplier.java
// ©2015 MindView LLC: see Copyright.txt
// javac-based annotation processing.
package annotations.ifx;

View File

@ -1,5 +1,4 @@
// annotations/simplest/Simple.java
// ©2015 MindView LLC: see Copyright.txt
// A bare-bones annotation.
package annotations.simplest;
import java.lang.annotation.Retention;

View File

@ -1,5 +1,4 @@
// annotations/simplest/SimpleProcessor.java
// ©2015 MindView LLC: see Copyright.txt
// A bare-bones annotation processor.
package annotations.simplest;
import javax.annotation.processing.*;

View File

@ -1,5 +1,4 @@
// annotations/simplest/SimpleTest.java
// ©2015 MindView LLC: see Copyright.txt
// Test the "Simple" annotation
package annotations.simplest;

View File

@ -1,5 +1,4 @@
// arrays/AlphabeticSearch.java
// ©2015 MindView LLC: see Copyright.txt
// Searching with a Comparator.
import java.util.*;
import com.mindviewinc.util.*;
@ -7,7 +6,7 @@ import com.mindviewinc.util.*;
public class AlphabeticSearch {
public static void main(String[] args) {
String[] sa = Generated.array(new String[30],
new RandomGenerator.String(5));
new RandomSupplier.String(5));
Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
System.out.println(Arrays.toString(sa));
int index = Arrays.binarySearch(sa, sa[10],

View File

@ -1,5 +1,4 @@
// arrays/ArrayOfGenericType.java
// ©2015 MindView LLC: see Copyright.txt
// Arrays of generic types won't compile.
public class ArrayOfGenericType<T> {

View File

@ -1,5 +1,4 @@
// arrays/ArrayOfGenerics.java
// ©2015 MindView LLC: see Copyright.txt
// It is possible to create arrays of generics.
import java.util.*;

View File

@ -1,8 +1,6 @@
// arrays/ArrayOptions.java
// ©2015 MindView LLC: see Copyright.txt
// Initialization & re-assignment of arrays.
import java.util.*;
import static com.mindviewinc.util.Print.*;
public class ArrayOptions {
public static void main(String[] args) {
@ -11,7 +9,7 @@ public class ArrayOptions {
BerylliumSphere[] b = new BerylliumSphere[5];
// The references inside the array are
// automatically initialized to null:
print("b: " + Arrays.toString(b));
System.out.println("b: " + Arrays.toString(b));
BerylliumSphere[] c = new BerylliumSphere[4];
for(int i = 0; i < c.length; i++)
if(c[i] == null) // Can test for null reference
@ -25,32 +23,32 @@ public class ArrayOptions {
new BerylliumSphere(), new BerylliumSphere(),
};
// (Trailing comma is optional in both cases)
print("a.length = " + a.length);
print("b.length = " + b.length);
print("c.length = " + c.length);
print("d.length = " + d.length);
System.out.println("a.length = " + a.length);
System.out.println("b.length = " + b.length);
System.out.println("c.length = " + c.length);
System.out.println("d.length = " + d.length);
a = d;
print("a.length = " + a.length);
System.out.println("a.length = " + a.length);
// Arrays of primitives:
int[] e; // Null reference
int[] f = new int[5];
// The primitives inside the array are
// automatically initialized to zero:
print("f: " + Arrays.toString(f));
System.out.println("f: " + Arrays.toString(f));
int[] g = new int[4];
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);
print("f.length = " + f.length);
print("g.length = " + g.length);
print("h.length = " + h.length);
System.out.println("f.length = " + f.length);
System.out.println("g.length = " + g.length);
System.out.println("h.length = " + h.length);
e = h;
print("e.length = " + e.length);
System.out.println("e.length = " + e.length);
e = new int[]{ 1, 2 };
print("e.length = " + e.length);
System.out.println("e.length = " + e.length);
}
}
/* Output:

View File

@ -1,23 +1,22 @@
// arrays/ArraySearching.java
// ©2015 MindView LLC: see Copyright.txt
// Using Arrays.binarySearch().
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class ArraySearching {
public static void main(String[] args) {
Generator<Integer> gen =
new RandomGenerator.Integer(1000);
Supplier<Integer> gen =
new RandomSupplier.Integer(1000);
int[] a = ConvertTo.primitive(
Generated.array(new Integer[25], gen));
Arrays.sort(a);
print("Sorted array: " + Arrays.toString(a));
System.out.println("Sorted array: " + Arrays.toString(a));
while(true) {
int r = gen.next();
int r = gen.get();
int location = Arrays.binarySearch(a, r);
if(location >= 0) {
print("Location of " + r + " is " + location +
System.out.println("Location of " + r + " is " + location +
", a[" + location + "] = " + a[location]);
break; // Out of while loop
}

View File

@ -1,5 +1,4 @@
// arrays/AssemblingMultidimensionalArrays.java
// ©2015 MindView LLC: see Copyright.txt
// Creating multidimensional arrays.
import java.util.*;

View File

@ -1,5 +1,4 @@
// arrays/AutoboxingArrays.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
public class AutoboxingArrays {

View File

@ -1,9 +1,8 @@
// arrays/CompType.java
// ©2015 MindView LLC: see Copyright.txt
// Implementing Comparable in a class.
import java.util.*;
import java.util.function.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class CompType implements Comparable<CompType> {
int i;
@ -25,18 +24,18 @@ public class CompType implements Comparable<CompType> {
return (i < rv.i ? -1 : (i == rv.i ? 0 : 1));
}
private static Random r = new Random(47);
public static Generator<CompType> generator() {
public static Supplier<CompType> generator() {
return () ->
new CompType(r.nextInt(100), r.nextInt(100));
}
public static void main(String[] args) {
CompType[] a =
Generated.array(new CompType[12], generator());
print("before sorting:");
print(Arrays.toString(a));
System.out.println("before sorting:");
System.out.println(Arrays.toString(a));
Arrays.sort(a);
print("after sorting:");
print(Arrays.toString(a));
System.out.println("after sorting:");
System.out.println(Arrays.toString(a));
}
}
/* Output:

View File

@ -1,9 +1,7 @@
// arrays/ComparatorTest.java
// ©2015 MindView LLC: see Copyright.txt
// Implementing a Comparator for a class.
import java.util.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
class CompTypeComparator implements Comparator<CompType> {
public int compare(CompType o1, CompType o2) {
@ -15,11 +13,11 @@ public class ComparatorTest {
public static void main(String[] args) {
CompType[] a = Generated.array(
new CompType[12], CompType.generator());
print("before sorting:");
print(Arrays.toString(a));
System.out.println("before sorting:");
System.out.println(Arrays.toString(a));
Arrays.sort(a, new CompTypeComparator());
print("after sorting:");
print(Arrays.toString(a));
System.out.println("after sorting:");
System.out.println(Arrays.toString(a));
}
}
/* Output:

View File

@ -1,8 +1,6 @@
// arrays/ComparingArrays.java
// Š2015 MindView LLC: see Copyright.txt
// Using Arrays.equals()
import java.util.*;
import static com.mindviewinc.util.Print.*;
public class ComparingArrays {
public static void main(String[] args) {
@ -10,14 +8,14 @@ public class ComparingArrays {
int[] a2 = new int[10];
Arrays.fill(a1, 47);
Arrays.fill(a2, 47);
print(Arrays.equals(a1, a2));
System.out.println(Arrays.equals(a1, a2));
a2[3] = 11;
print(Arrays.equals(a1, a2));
System.out.println(Arrays.equals(a1, a2));
String[] s1 = new String[4];
Arrays.fill(s1, "Hi");
String[] s2 = { new String("Hi"), new String("Hi"),
new String("Hi"), new String("Hi") };
print(Arrays.equals(s1, s2));
System.out.println(Arrays.equals(s1, s2));
}
}
/* Output:

View File

@ -1,7 +1,5 @@
// arrays/ContainerComparison.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
import static com.mindviewinc.util.Print.*;
class BerylliumSphere {
private static long counter;
@ -15,24 +13,24 @@ public class ContainerComparison {
BerylliumSphere[] spheres = new BerylliumSphere[10];
for(int i = 0; i < 5; i++)
spheres[i] = new BerylliumSphere();
print(Arrays.toString(spheres));
print(spheres[4]);
System.out.println(Arrays.toString(spheres));
System.out.println(spheres[4]);
List<BerylliumSphere> sphereList= new ArrayList<>();
for(int i = 0; i < 5; i++)
sphereList.add(new BerylliumSphere());
print(sphereList);
print(sphereList.get(4));
System.out.println(sphereList);
System.out.println(sphereList.get(4));
int[] integers = { 0, 1, 2, 3, 4, 5 };
print(Arrays.toString(integers));
print(integers[4]);
System.out.println(Arrays.toString(integers));
System.out.println(integers[4]);
List<Integer> intList = new ArrayList<>(
Arrays.asList(0, 1, 2, 3, 4, 5));
intList.add(97);
print(intList);
print(intList.get(4));
System.out.println(intList);
System.out.println(intList.get(4));
}
}
/* Output:

View File

@ -1,8 +1,6 @@
// arrays/CopyingArrays.java
// ©2015 MindView LLC: see Copyright.txt
// Using System.arraycopy()
import java.util.*;
import static com.mindviewinc.util.Print.*;
public class CopyingArrays {
public static void main(String[] args) {
@ -10,26 +8,26 @@ public class CopyingArrays {
int[] j = new int[10];
Arrays.fill(i, 47);
Arrays.fill(j, 99);
print("i = " + Arrays.toString(i));
print("j = " + Arrays.toString(j));
System.out.println("i = " + Arrays.toString(i));
System.out.println("j = " + Arrays.toString(j));
System.arraycopy(i, 0, j, 0, i.length);
print("j = " + Arrays.toString(j));
System.out.println("j = " + Arrays.toString(j));
int[] k = new int[5];
Arrays.fill(k, 103);
System.arraycopy(i, 0, k, 0, k.length);
print("k = " + Arrays.toString(k));
System.out.println("k = " + Arrays.toString(k));
Arrays.fill(k, 103);
System.arraycopy(k, 0, i, 0, k.length);
print("i = " + Arrays.toString(i));
System.out.println("i = " + Arrays.toString(i));
// Objects:
Integer[] u = new Integer[10];
Integer[] v = new Integer[5];
Arrays.fill(u, 47);
Arrays.fill(v, 99);
print("u = " + Arrays.toString(u));
print("v = " + Arrays.toString(v));
System.out.println("u = " + Arrays.toString(u));
System.out.println("v = " + Arrays.toString(v));
System.arraycopy(v, 0, u, u.length/2, v.length);
print("u = " + Arrays.toString(u));
System.out.println("u = " + Arrays.toString(u));
}
}
/* Output:

View File

@ -1,8 +1,6 @@
// arrays/FillingArrays.java
// ©2015 MindView LLC: see Copyright.txt
// Using Arrays.fill()
import java.util.*;
import static com.mindviewinc.util.Print.*;
public class FillingArrays {
public static void main(String[] args) {
@ -17,26 +15,26 @@ public class FillingArrays {
double[] a8 = new double[size];
String[] a9 = new String[size];
Arrays.fill(a1, true);
print("a1 = " + Arrays.toString(a1));
System.out.println("a1 = " + Arrays.toString(a1));
Arrays.fill(a2, (byte)11);
print("a2 = " + Arrays.toString(a2));
System.out.println("a2 = " + Arrays.toString(a2));
Arrays.fill(a3, 'x');
print("a3 = " + Arrays.toString(a3));
System.out.println("a3 = " + Arrays.toString(a3));
Arrays.fill(a4, (short)17);
print("a4 = " + Arrays.toString(a4));
System.out.println("a4 = " + Arrays.toString(a4));
Arrays.fill(a5, 19);
print("a5 = " + Arrays.toString(a5));
System.out.println("a5 = " + Arrays.toString(a5));
Arrays.fill(a6, 23);
print("a6 = " + Arrays.toString(a6));
System.out.println("a6 = " + Arrays.toString(a6));
Arrays.fill(a7, 29);
print("a7 = " + Arrays.toString(a7));
System.out.println("a7 = " + Arrays.toString(a7));
Arrays.fill(a8, 47);
print("a8 = " + Arrays.toString(a8));
System.out.println("a8 = " + Arrays.toString(a8));
Arrays.fill(a9, "Hello");
print("a9 = " + Arrays.toString(a9));
System.out.println("a9 = " + Arrays.toString(a9));
// Manipulating ranges:
Arrays.fill(a9, 3, 5, "World");
print("a9 = " + Arrays.toString(a9));
System.out.println("a9 = " + Arrays.toString(a9));
}
}
/* Output:

View File

@ -1,5 +1,4 @@
// arrays/IceCream.java
// ©2015 MindView LLC: see Copyright.txt
// Returning arrays from methods.
import java.util.*;

View File

@ -1,5 +1,4 @@
// arrays/MultiDimWrapperArray.java
// ©2015 MindView LLC: see Copyright.txt
// Multidimensional arrays of "wrapper" objects.
import java.util.*;

View File

@ -1,5 +1,4 @@
// arrays/MultidimensionalObjectArrays.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
public class MultidimensionalObjectArrays {

View File

@ -1,5 +1,4 @@
// arrays/MultidimensionalPrimitiveArray.java
// ©2015 MindView LLC: see Copyright.txt
// Creating multidimensional arrays.
import java.util.*;

View File

@ -1,5 +1,4 @@
// arrays/ParameterizedArrayType.java
// ©2015 MindView LLC: see Copyright.txt
class ClassParameter<T> {
public T[] f(T[] arg) { return arg; }

View File

@ -1,17 +1,16 @@
// arrays/PrimitiveConversionDemonstration.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
import com.mindviewinc.util.*;
public class PrimitiveConversionDemonstration {
public static void main(String[] args) {
Integer[] a = Generated.array(Integer.class,
new CountingGenerator.Integer(), 15);
new CountingSupplier.Integer(), 15);
int[] b = ConvertTo.primitive(a);
System.out.println(Arrays.toString(b));
boolean[] c = ConvertTo.primitive(
Generated.array(Boolean.class,
new CountingGenerator.Boolean(), 7));
new CountingSupplier.Boolean(), 7));
System.out.println(Arrays.toString(c));
}
}

View File

@ -1,5 +1,4 @@
# arrays/PythonLists.py
# ©2015 MindView LLC: see Copyright.txt
aList = [1, 2, 3, 4, 5]
print(type(aList)) # <type 'list'>
@ -22,4 +21,3 @@ class MyList(list): # Inherit from list
list2 = MyList(aList)
print(type(list2)) # <class '__main__.MyList'>
print(list2.getReversed()) # [8, 7, 6, 5, 4, 3, 2, 1]
#>

View File

@ -1,5 +1,4 @@
// arrays/RaggedArray.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
public class RaggedArray {

View File

@ -1,10 +1,9 @@
// arrays/RandomGeneratorsTest.java
// ©2015 MindView LLC: see Copyright.txt
// arrays/RandomSuppliersTest.java
import com.mindviewinc.util.*;
public class RandomGeneratorsTest {
public class RandomSuppliersTest {
public static void main(String[] args) {
GeneratorsTest.test(RandomGenerator.class);
SuppliersTest.test(RandomSupplier.class);
}
}
/* Output:

View File

@ -1,19 +1,17 @@
// arrays/Reverse.java
// ©2015 MindView LLC: see Copyright.txt
// The Collections.reverseOrder() Comparator
import java.util.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class Reverse {
public static void main(String[] args) {
CompType[] a = Generated.array(
new CompType[12], CompType.generator());
print("before sorting:");
print(Arrays.toString(a));
System.out.println("before sorting:");
System.out.println(Arrays.toString(a));
Arrays.sort(a, Collections.reverseOrder());
print("after sorting:");
print(Arrays.toString(a));
System.out.println("after sorting:");
System.out.println(Arrays.toString(a));
}
}
/* Output:

View File

@ -1,21 +1,19 @@
// arrays/StringSorting.java
// ©2015 MindView LLC: see Copyright.txt
// Sorting an array of Strings.
import java.util.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class StringSorting {
public static void main(String[] args) {
String[] sa = Generated.array(new String[20],
new RandomGenerator.String(5));
print("Before sort: " + Arrays.toString(sa));
new RandomSupplier.String(5));
System.out.println("Before sort: " + Arrays.toString(sa));
Arrays.sort(sa);
print("After sort: " + Arrays.toString(sa));
System.out.println("After sort: " + Arrays.toString(sa));
Arrays.sort(sa, Collections.reverseOrder());
print("Reverse sort: " + Arrays.toString(sa));
System.out.println("Reverse sort: " + Arrays.toString(sa));
Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
print("Case-insensitive sort: " + Arrays.toString(sa));
System.out.println("Case-insensitive sort: " + Arrays.toString(sa));
}
}
/* Output:

View File

@ -1,16 +1,16 @@
// arrays/GeneratorsTest.java
// ©2015 MindView LLC: see Copyright.txt
// arrays/SuppliersTest.java
import java.util.function.*;
import com.mindviewinc.util.*;
public class GeneratorsTest {
public class SuppliersTest {
public static int size = 10;
public static void test(Class<?> surroundingClass) {
for(Class<?> type : surroundingClass.getClasses()) {
System.out.print(type.getSimpleName() + ": ");
try {
Generator<?> g = (Generator<?>)type.newInstance();
Supplier<?> g = (Supplier<?>)type.newInstance();
for(int i = 0; i < size; i++)
System.out.printf(g.next() + " ");
System.out.printf(g.get() + " ");
System.out.println();
} catch(InstantiationException |
IllegalAccessException e) {
@ -19,7 +19,7 @@ public class GeneratorsTest {
}
}
public static void main(String[] args) {
test(CountingGenerator.class);
test(CountingSupplier.class);
}
}
/* Output:

View File

@ -1,38 +1,36 @@
// arrays/TestArrayGeneration.java
// ©2015 MindView LLC: see Copyright.txt
// Test the tools that use generators to fill arrays.
import java.util.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class TestArrayGeneration {
public static void main(String[] args) {
int size = 6;
boolean[] a1 = ConvertTo.primitive(Generated.array(
Boolean.class, new RandomGenerator.Boolean(), size));
print("a1 = " + Arrays.toString(a1));
Boolean.class, new RandomSupplier.Boolean(), size));
System.out.println("a1 = " + Arrays.toString(a1));
byte[] a2 = ConvertTo.primitive(Generated.array(
Byte.class, new RandomGenerator.Byte(), size));
print("a2 = " + Arrays.toString(a2));
Byte.class, new RandomSupplier.Byte(), size));
System.out.println("a2 = " + Arrays.toString(a2));
char[] a3 = ConvertTo.primitive(Generated.array(
Character.class,
new RandomGenerator.Character(), size));
print("a3 = " + Arrays.toString(a3));
new RandomSupplier.Character(), size));
System.out.println("a3 = " + Arrays.toString(a3));
short[] a4 = ConvertTo.primitive(Generated.array(
Short.class, new RandomGenerator.Short(), size));
print("a4 = " + Arrays.toString(a4));
Short.class, new RandomSupplier.Short(), size));
System.out.println("a4 = " + Arrays.toString(a4));
int[] a5 = ConvertTo.primitive(Generated.array(
Integer.class, new RandomGenerator.Integer(), size));
print("a5 = " + Arrays.toString(a5));
Integer.class, new RandomSupplier.Integer(), size));
System.out.println("a5 = " + Arrays.toString(a5));
long[] a6 = ConvertTo.primitive(Generated.array(
Long.class, new RandomGenerator.Long(), size));
print("a6 = " + Arrays.toString(a6));
Long.class, new RandomSupplier.Long(), size));
System.out.println("a6 = " + Arrays.toString(a6));
float[] a7 = ConvertTo.primitive(Generated.array(
Float.class, new RandomGenerator.Float(), size));
print("a7 = " + Arrays.toString(a7));
Float.class, new RandomSupplier.Float(), size));
System.out.println("a7 = " + Arrays.toString(a7));
double[] a8 = ConvertTo.primitive(Generated.array(
Double.class, new RandomGenerator.Double(), size));
print("a8 = " + Arrays.toString(a8));
Double.class, new RandomSupplier.Double(), size));
System.out.println("a8 = " + Arrays.toString(a8));
}
}
/* Output:

View File

@ -1,5 +1,4 @@
// arrays/TestGenerated.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
import com.mindviewinc.util.*;
@ -7,10 +6,10 @@ public class TestGenerated {
public static void main(String[] args) {
Integer[] a = { 9, 8, 7, 6 };
System.out.println(Arrays.toString(a));
a = Generated.array(a, new CountingGenerator.Integer());
a = Generated.array(a, new CountingSupplier.Integer());
System.out.println(Arrays.toString(a));
Integer[] b = Generated.array(Integer.class,
new CountingGenerator.Integer(), 15);
new CountingSupplier.Integer(), 15);
System.out.println(Arrays.toString(b));
}
}

View File

@ -1,5 +1,4 @@
// arrays/ThreeDWithNew.java
// ©2015 MindView LLC: see Copyright.txt
import java.util.*;
public class ThreeDWithNew {

View File

@ -19,7 +19,6 @@
<jrun cls="ContainerComparison" />
<jrun cls="CopyingArrays" />
<jrun cls="FillingArrays" />
<jrun cls="GeneratorsTest" />
<jrun cls="IceCream" />
<jrun cls="MultidimensionalObjectArrays" />
<jrun cls="MultidimensionalPrimitiveArray" />
@ -27,9 +26,10 @@
<jrun cls="ParameterizedArrayType" />
<jrun cls="PrimitiveConversionDemonstration" />
<jrun cls="RaggedArray" />
<jrun cls="RandomGeneratorsTest" />
<jrun cls="RandomSuppliersTest" />
<jrun cls="Reverse" />
<jrun cls="StringSorting" />
<jrun cls="SuppliersTest" />
<jrun cls="TestArrayGeneration" />
<jrun cls="TestGenerated" />
<jrun cls="ThreeDWithNew" />

View File

@ -1,5 +1,4 @@
// assertions/Assert1.java
// ©2015 MindView LLC: see Copyright.txt
// Non-informative style of assert
// {JVMArgs: -ea} // Must run with -ea
// {ThrowsException}

View File

@ -1,5 +1,4 @@
// assertions/Assert2.java
// ©2015 MindView LLC: see Copyright.txt
// Assert with an informative message
// {JVMArgs: -ea}
// {ThrowsException}

View File

@ -1,5 +1,4 @@
// assertions/LoaderAssertions.java
// ©2015 MindView LLC: see Copyright.txt
// Using the class loader to enable assertions
// {ThrowsException}

View File

@ -1,5 +1,4 @@
// assertions/Queue.java
// ©2015 MindView LLC: see Copyright.txt
// Demonstration of Design by Contract (DbC) combined
// with white-box unit testing.
// (Install libraries from www.junit.org)

View File

@ -15,7 +15,6 @@
<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.New" dirpath="../com/mindviewinc/util" />
<jrun cls="com.mindviewinc.util.TextFile" dirpath="../com/mindviewinc/util" />
</target>

View File

@ -1,12 +1,10 @@
// com/mindviewinc/atunit/AtUnit.java
// ©2015 MindView LLC: see Copyright.txt
// An annotation-based unit-test framework.
package com.mindviewinc.atunit;
import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class AtUnit implements ProcessFiles.Strategy {
static Class<?> testClass;
@ -18,13 +16,13 @@ public class AtUnit implements ProcessFiles.Strategy {
.setDefaultAssertionStatus(true); // Enable asserts
new ProcessFiles(new AtUnit(), "class").start(args);
if(failures == 0)
print("OK (" + testsRun + " tests)");
System.out.println("OK (" + testsRun + " tests)");
else {
print("(" + testsRun + " tests)");
print("\n>>> " + failures + " FAILURE" +
System.out.println("(" + testsRun + " tests)");
System.out.println("\n>>> " + failures + " FAILURE" +
(failures > 1 ? "S" : "") + " <<<");
for(String failed : failedTests)
print(" " + failed);
System.out.println(" " + failed);
}
}
@Override
@ -53,17 +51,17 @@ public class AtUnit implements ProcessFiles.Strategy {
try {
if(!Modifier.isPublic(testClass
.getDeclaredConstructor().getModifiers())) {
print("Error: " + testClass +
" default constructor must be public");
System.out.println("Error: " + testClass +
" no-arg constructor must be public");
System.exit(1);
}
} catch(NoSuchMethodException e) {
// Synthesized default constructor; OK
// Synthesized no-arg constructor; OK
}
print(testClass.getName());
System.out.println(testClass.getName());
}
for(Method m : testMethods) {
printnb(" . " + m.getName() + " ");
System.out.print(" . " + m.getName() + " ");
try {
Object testObject = createTestObject(creator);
boolean success = false;
@ -76,9 +74,9 @@ public class AtUnit implements ProcessFiles.Strategy {
}
} catch(InvocationTargetException e) {
// Actual exception is inside e:
print(e.getCause());
System.out.println(e.getCause());
}
print(success ? "" : "(failed)");
System.out.println(success ? "" : "(failed)");
testsRun++;
if(!success) {
failures++;
@ -146,7 +144,7 @@ public class AtUnit implements ProcessFiles.Strategy {
throw new RuntimeException("Couldn't run " +
"@TestObject (creator) method.");
}
} else { // Use the default constructor:
} else { // Use the no-arg constructor:
try {
return testClass.newInstance();
} catch(InstantiationException |

View File

@ -1,10 +1,8 @@
// com/mindviewinc/atunit/ClassNameFinder.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.atunit;
import java.io.*;
import java.util.*;
import com.mindviewinc.util.*;
import static com.mindviewinc.util.Print.*;
public class ClassNameFinder {
public static String thisClass(byte[] classBytes) {
@ -67,11 +65,11 @@ public class ClassNameFinder {
public static void main(String[] args) throws Exception {
if(args.length > 0) {
for(String arg : args)
print(thisClass(BinaryFile.read(new File(arg))));
System.out.println(thisClass(BinaryFile.read(new File(arg))));
} else
// Walk the entire tree:
// Walk the entire tree: <* Use NIO2 here *>
for(File klass : Directory.walk(".", ".*\\.class"))
print(thisClass(BinaryFile.read(klass)));
System.out.println(thisClass(BinaryFile.read(klass)));
}
}
/* Output:

View File

@ -1,5 +1,4 @@
// com/mindviewinc/atunit/Test.java
// ©2015 MindView LLC: see Copyright.txt
// The @Test tag.
package com.mindviewinc.atunit;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/atunit/TestObjectCleanup.java
// ©2015 MindView LLC: see Copyright.txt
// The @Unit @TestObjectCleanup tag.
package com.mindviewinc.atunit;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/atunit/TestObjectCreate.java
// ©2015 MindView LLC: see Copyright.txt
// The @Unit @TestObjectCreate tag.
package com.mindviewinc.atunit;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/atunit/TestProperty.java
// ©2015 MindView LLC: see Copyright.txt
// The @Unit @TestProperty tag.
package com.mindviewinc.atunit;
import java.lang.annotation.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/simple/List.java
// ©2015 MindView LLC: see Copyright.txt
// Creating a package.
package com.mindviewinc.simple;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/simple/Vector.java
// ©2015 MindView LLC: see Copyright.txt
// Creating a package.
package com.mindviewinc.simple;

View File

@ -1,14 +1,14 @@
// com/mindviewinc/util/BasicGenerator.java
// ©2015 MindView LLC: see Copyright.txt
// Automatically create a Generator, given a class
// com/mindviewinc/util/BasicSupplier.java
// Automatically create a Supplier, given a class
// with a default (no-arg) constructor.
package com.mindviewinc.util;
import java.util.function.*;
public class BasicGenerator<T> implements Generator<T> {
public class BasicSupplier<T> implements Supplier<T> {
private Class<T> type;
public BasicGenerator(Class<T> type){ this.type = type; }
public BasicSupplier(Class<T> type){ this.type = type; }
@Override
public T next() {
public T get() {
try {
// Assumes type is a public class:
return type.newInstance();
@ -18,7 +18,7 @@ public class BasicGenerator<T> implements Generator<T> {
}
}
// Produce a Default generator given a type token:
public static <T> Generator<T> create(Class<T> type) {
return new BasicGenerator<>(type);
public static <T> Supplier<T> create(Class<T> type) {
return new BasicSupplier<>(type);
}
}

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/BinaryFile.java
// ©2015 MindView LLC: see Copyright.txt
// Utility for reading files in binary form.
package com.mindviewinc.util;
import java.io.*;

View File

@ -1,17 +1,17 @@
// com/mindviewinc/util/CollectionData.java
// ©2015 MindView LLC: see Copyright.txt
// A Collection filled with data using a generator object.
package com.mindviewinc.util;
import java.util.*;
import java.util.function.*;
public class CollectionData<T> extends ArrayList<T> {
public CollectionData(Generator<T> gen, int quantity) {
public CollectionData(Supplier<T> gen, int quantity) {
for(int i = 0; i < quantity; i++)
add(gen.next());
add(gen.get());
}
// A generic convenience method:
public static <T> CollectionData<T>
list(Generator<T> gen, int quantity) {
list(Supplier<T> gen, int quantity) {
return new CollectionData<>(gen, quantity);
}
}

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/ContainerMethodDifferences.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
import java.lang.reflect.*;
import java.util.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/ConvertTo.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
public class ConvertTo {

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/CountingIntegerList.java
// ©2015 MindView LLC: see Copyright.txt
// List of any length, containing sample data.
package com.mindviewinc.util;
import java.util.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/CountingMapData.java
// ©2015 MindView LLC: see Copyright.txt
// Unlimited-length Map containing sample data.
package com.mindviewinc.util;
import java.util.*;

View File

@ -1,82 +1,82 @@
// com/mindviewinc/util/CountingGenerator.java
// ©2015 MindView LLC: see Copyright.txt
// com/mindviewinc/util/CountingSupplier.java
// Simple generator implementations.
package com.mindviewinc.util;
import java.util.function.*;
public class CountingGenerator {
public class CountingSupplier {
public static class
Boolean implements Generator<java.lang.Boolean> {
Boolean implements Supplier<java.lang.Boolean> {
private boolean value = false;
@Override
public java.lang.Boolean next() {
public java.lang.Boolean get() {
value = !value; // Just flips back and forth
return value;
}
}
public static class
Byte implements Generator<java.lang.Byte> {
Byte implements Supplier<java.lang.Byte> {
private byte value = 0;
@Override
public java.lang.Byte next() { return value++; }
public java.lang.Byte get() { return value++; }
}
static char[] chars = ("abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
public static class
Character implements Generator<java.lang.Character> {
Character implements Supplier<java.lang.Character> {
int index = -1;
@Override
public java.lang.Character next() {
public java.lang.Character get() {
index = (index + 1) % chars.length;
return chars[index];
}
}
public static class
String implements Generator<java.lang.String> {
String implements Supplier<java.lang.String> {
private int length = 7;
Generator<java.lang.Character> cg = new Character();
Supplier<java.lang.Character> cg = new Character();
public String() {}
public String(int length) { this.length = length; }
@Override
public java.lang.String next() {
public java.lang.String get() {
char[] buf = new char[length];
for(int i = 0; i < length; i++)
buf[i] = cg.next();
buf[i] = cg.get();
return new java.lang.String(buf);
}
}
public static class
Short implements Generator<java.lang.Short> {
Short implements Supplier<java.lang.Short> {
private short value = 0;
@Override
public java.lang.Short next() { return value++; }
public java.lang.Short get() { return value++; }
}
public static class
Integer implements Generator<java.lang.Integer> {
Integer implements Supplier<java.lang.Integer> {
private int value = 0;
@Override
public java.lang.Integer next() { return value++; }
public java.lang.Integer get() { return value++; }
}
public static class
Long implements Generator<java.lang.Long> {
Long implements Supplier<java.lang.Long> {
private long value = 0;
@Override
public java.lang.Long next() { return value++; }
public java.lang.Long get() { return value++; }
}
public static class
Float implements Generator<java.lang.Float> {
Float implements Supplier<java.lang.Float> {
private float value = 0;
@Override
public java.lang.Float next() {
public java.lang.Float get() {
float result = value;
value += 1.0;
return result;
}
}
public static class
Double implements Generator<java.lang.Double> {
Double implements Supplier<java.lang.Double> {
private double value = 0.0;
@Override
public java.lang.Double next() {
public java.lang.Double get() {
double result = value;
value += 1.0;
return result;

View File

@ -1,9 +1,7 @@
// com/mindviewinc/util/Countries.java
// ©2015 MindView LLC: see Copyright.txt
// "Flyweight" Maps and Lists of sample data.
package com.mindviewinc.util;
import java.util.*;
import static com.mindviewinc.util.Print.*;
public class Countries {
public static final String[][] DATA = {
@ -226,18 +224,18 @@ public class Countries {
return new ArrayList<>(select(size).keySet());
}
public static void main(String[] args) {
print(capitals(10));
print(names(10));
print(new HashMap<>(capitals(3)));
print(new LinkedHashMap<>(capitals(3)));
print(new TreeMap<>(capitals(3)));
print(new Hashtable<>(capitals(3)));
print(new HashSet<>(names(6)));
print(new LinkedHashSet<>(names(6)));
print(new TreeSet<>(names(6)));
print(new ArrayList<>(names(6)));
print(new LinkedList<>(names(6)));
print(capitals().get("BRAZIL"));
System.out.println(capitals(10));
System.out.println(names(10));
System.out.println(new HashMap<>(capitals(3)));
System.out.println(new LinkedHashMap<>(capitals(3)));
System.out.println(new TreeMap<>(capitals(3)));
System.out.println(new Hashtable<>(capitals(3)));
System.out.println(new HashSet<>(names(6)));
System.out.println(new LinkedHashSet<>(names(6)));
System.out.println(new TreeSet<>(names(6)));
System.out.println(new ArrayList<>(names(6)));
System.out.println(new LinkedList<>(names(6)));
System.out.println(capitals().get("BRAZIL"));
}
}
/* Output:

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/DaemonThreadFactory.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
import java.util.concurrent.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/DaemonThreadPoolExecutor.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
import java.util.concurrent.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/Deque.java
// ©2015 MindView LLC: see Copyright.txt
// Creating a Deque from a LinkedList.
package com.mindviewinc.util;
import java.util.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/Directory.java
// ©2015 MindView LLC: see Copyright.txt
// Produce a sequence of File objects that match a
// regular expression in either a local directory,
// or by walking a directory tree.
@ -81,8 +80,8 @@ public final class Directory {
/* Output: (First 20 Lines)
dirs: []
files: [
.\BasicGenerator.class
.\BasicGenerator.java
.\BasicSupplier.class
.\BasicSupplier.java
.\BinaryFile.class
.\BinaryFile.java
.\CollectionData.class
@ -93,10 +92,10 @@ files: [
.\ContainerMethodDifferences.java
.\ConvertTo.class
.\ConvertTo.java
.\CountingGenerator$Boolean.class
.\CountingGenerator$Byte.class
.\CountingGenerator$Character.class
.\CountingGenerator$Double.class
.\CountingGenerator$Float.class
.\CountingSupplier$Boolean.class
.\CountingSupplier$Byte.class
.\CountingSupplier$Character.class
.\CountingSupplier$Double.class
.\CountingSupplier$Float.class
...
*/

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/Enums.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
import java.util.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/FiveTuple.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
public class FiveTuple<A, B, C, D, E>

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/FourTuple.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
public class FourTuple<A, B, C, D>

View File

@ -1,16 +1,16 @@
// com/mindviewinc/util/Generated.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
import java.util.function.*;
public class Generated {
// Fill an existing array:
public static <T> T[] array(T[] a, Generator<T> gen) {
public static <T> T[] array(T[] a, Supplier<T> gen) {
return new CollectionData<>(gen, a.length).toArray(a);
}
// Create a new array:
@SuppressWarnings("unchecked")
public static <T> T[] array(Class<T> type,
Generator<T> gen, int size) {
Supplier<T> gen, int size) {
T[] a =
(T[])java.lang.reflect.Array.newInstance(type, size);
return new CollectionData<>(gen, size).toArray(a);

View File

@ -1,5 +0,0 @@
// com/mindviewinc/util/Generator.java
// ©2015 MindView LLC: see Copyright.txt
// A generic interface.
package com.mindviewinc.util;
public interface Generator<T> { T next(); }

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/Hex.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
import java.io.*;

View File

@ -1,34 +1,34 @@
// com/mindviewinc/util/MapData.java
// ©2015 MindView LLC: see Copyright.txt
// A Map filled with data using a generator object.
package com.mindviewinc.util;
import java.util.*;
import java.util.function.*;
public class MapData<K, V> extends LinkedHashMap<K, V> {
// A single Pair Generator:
public MapData(Generator<Pair<K, V>> gen, int quantity) {
// A single Pair Supplier:
public MapData(Supplier<Pair<K, V>> gen, int quantity) {
for(int i = 0; i < quantity; i++) {
Pair<K, V> p = gen.next();
Pair<K, V> p = gen.get();
put(p.key, p.value);
}
}
// Two separate Generators:
public MapData(Generator<K> genK, Generator<V> genV,
// Two separate Suppliers:
public MapData(Supplier<K> genK, Supplier<V> genV,
int quantity) {
for(int i = 0; i < quantity; i++) {
put(genK.next(), genV.next());
put(genK.get(), genV.get());
}
}
// A key Generator and a single value:
public MapData(Generator<K> genK, V value, int quantity){
// A key Supplier and a single value:
public MapData(Supplier<K> genK, V value, int quantity){
for(int i = 0; i < quantity; i++) {
put(genK.next(), value);
put(genK.get(), value);
}
}
// An Iterable and a value Generator:
public MapData(Iterable<K> genK, Generator<V> genV) {
// An Iterable and a value Supplier:
public MapData(Iterable<K> genK, Supplier<V> genV) {
for(K key : genK) {
put(key, genV.next());
put(key, genV.get());
}
}
// An Iterable and a single value:
@ -39,19 +39,19 @@ public class MapData<K, V> extends LinkedHashMap<K, V> {
}
// Generic convenience methods:
public static <K, V> MapData<K, V>
map(Generator<Pair<K, V>> gen, int quantity) {
map(Supplier<Pair<K, V>> gen, int quantity) {
return new MapData<>(gen, quantity);
}
public static <K, V> MapData<K, V>
map(Generator<K> genK, Generator<V> genV, int quantity) {
map(Supplier<K> genK, Supplier<V> genV, int quantity) {
return new MapData<>(genK, genV, quantity);
}
public static <K, V> MapData<K, V>
map(Generator<K> genK, V value, int quantity) {
map(Supplier<K> genK, V value, int quantity) {
return new MapData<>(genK, value, quantity);
}
public static <K, V> MapData<K, V>
map(Iterable<K> genK, Generator<V> genV) {
map(Iterable<K> genK, Supplier<V> genV) {
return new MapData<>(genK, genV);
}
public static <K, V> MapData<K, V>

View File

@ -0,0 +1,16 @@
// com/mindviewinc/util/MouseClick.java
// Helper interface to allow lambda expressions
package com.mindviewinc.util;
import java.awt.event.*;
// Default everything except mouseClicked():
public interface MouseClick extends MouseListener {
@Override
public default void mouseEntered(MouseEvent e) {}
@Override
public default void mouseExited(MouseEvent e) {}
@Override
public default void mousePressed(MouseEvent e) {}
@Override
public default void mouseReleased(MouseEvent e) {}
}

View File

@ -1,33 +0,0 @@
// com/mindviewinc/util/New.java
// ©2015 MindView LLC: see Copyright.txt
// Utilities to simplify generic container creation
// by using type argument inference.
package com.mindviewinc.util;
import java.util.*;
public class New {
public static <K, V> Map<K, V> map() {
return new HashMap<>();
}
public static <T> List<T> list() {
return new ArrayList<>();
}
public static <T> LinkedList<T> lList() {
return new LinkedList<>();
}
public static <T> Set<T> set() {
return new HashSet<>();
}
public static <T> Queue<T> queue() {
return new LinkedList<>();
}
// Examples:
public static void main(String[] args) {
Map<String, List<String>> sls = New.map();
List<String> ls = New.list();
LinkedList<String> lls = New.lList();
Set<String> ss = New.set();
Queue<String> qs = New.queue();
}
}
/* Output: (None) */

View File

@ -1,4 +1,3 @@
// com/mindviewinc/util/Null.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
public interface Null {}

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/OSExecute.java
// ©2015 MindView LLC: see Copyright.txt
// Run an operating system command
// and send the output to the console.
package com.mindviewinc.util;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/OSExecuteException.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
public class OSExecuteException extends RuntimeException {

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/PPrint.java
// ©2015 MindView LLC: see Copyright.txt
// Pretty-printer for collections
package com.mindviewinc.util;
import java.util.*;

View File

@ -1,5 +1,4 @@
// com/mindviewinc/util/Pair.java
// ©2015 MindView LLC: see Copyright.txt
package com.mindviewinc.util;
public class Pair<K, V> {

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