Checkstyle passing

This commit is contained in:
Bruce Eckel 2017-05-01 14:33:10 -06:00
parent b2f63b666c
commit 97dcdebc0c
97 changed files with 173 additions and 188 deletions

View File

@ -9,7 +9,7 @@ import static onjava.ArrayShow.*;
class Sup { // Superclass
private int id;
public Sup(int n) { id = n; }
Sup(int n) { id = n; }
@Override
public String toString() {
return getClass().getSimpleName() + id;
@ -17,7 +17,7 @@ class Sup { // Superclass
}
class Sub extends Sup { // Subclass
public Sub(int n) { super(n); }
Sub(int n) { super(n); }
}
public class ArrayCopying {

View File

@ -7,7 +7,7 @@ import static onjava.ArrayShow.*;
class Bob {
final int id;
public Bob(int n) { id = n; }
Bob(int n) { id = n; }
@Override
public String toString() { return "Bob" + id; }
}

View File

@ -1,2 +1,2 @@
cp ..\..\OnJava-Tools\verify_output.py .
python verify_output.py
py -3 verify_output.py

View File

@ -1,2 +1,2 @@
gradlew clean
@echo run gradlew clean first!
gradlew checkstyleMain 1> checkstyleout.txt 2>&1

View File

@ -7,7 +7,7 @@
import java.util.*;
class ReversibleArrayList<T> extends ArrayList<T> {
public ReversibleArrayList(Collection<T> c) {
ReversibleArrayList(Collection<T> c) {
super(c);
}
public Iterable<T> reversed() {

View File

@ -7,7 +7,7 @@ import java.util.*;
class Element {
private String ident;
public Element(String id) { ident = id; }
Element(String id) { ident = id; }
@Override
public String toString() { return ident; }
@Override
@ -27,11 +27,11 @@ class Element {
}
class Key extends Element {
public Key(String id) { super(id); }
Key(String id) { super(id); }
}
class Value extends Element {
public Value(String id) { super(id); }
Value(String id) { super(id); }
}
public class CanonicalMapping {

View File

@ -7,7 +7,7 @@ import java.util.*;
class StringAddress {
private String s;
public StringAddress(String s) { this.s = s; }
StringAddress(String s) { this.s = s; }
@Override
public String toString() {
return super.toString() + " " + s;

View File

@ -10,7 +10,7 @@ class VeryBig {
private static final int SIZE = 10000;
private long[] la = new long[SIZE];
private String ident;
public VeryBig(String id) { ident = id; }
VeryBig(String id) { ident = id; }
@Override
public String toString() { return ident; }
@Override

View File

@ -9,8 +9,8 @@ import java.util.function.*;
class CountString implements Supplier<String> {
private int n = 0;
public CountString() {}
public CountString(int start) { n = start; }
CountString() {}
CountString(int start) { n = start; }
@Override
public String get() {
return Integer.toString(n++);

View File

@ -9,7 +9,7 @@ class ToDoItem implements Comparable<ToDoItem> {
private char primary;
private int secondary;
private String item;
public ToDoItem(String td, char pri, int sec) {
ToDoItem(String td, char pri, int sec) {
primary = pri;
secondary = sec;
item = td;

View File

@ -6,11 +6,10 @@
import java.util.*;
import java.util.function.*;
import java.util.Objects;
import onjava.CountMap;
class SetType {
protected int i;
public SetType(int n) { i = n; }
SetType(int n) { i = n; }
@Override
public boolean equals(Object o) {
return o instanceof SetType &&
@ -23,7 +22,7 @@ class SetType {
}
class HashType extends SetType {
public HashType(int n) { super(n); }
HashType(int n) { super(n); }
@Override
public int hashCode() {
return Objects.hashCode(i);
@ -32,7 +31,7 @@ class HashType extends SetType {
class TreeType extends SetType
implements Comparable<TreeType> {
public TreeType(int n) { super(n); }
TreeType(int n) { super(n); }
@Override
public int compareTo(TreeType arg) {
return Integer.compare(arg.i, i);

View File

@ -3,7 +3,6 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.concurrent.*;
import onjava.Nap;
public class CatchCompletableExceptions {
static void handleException(int failcount) {

View File

@ -3,7 +3,6 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.concurrent.*;
import onjava.Nap;
public class CompletableExceptions {
static CompletableFuture<Breakable>

View File

@ -8,7 +8,7 @@ import java.util.stream.*;
import onjava.Timer;
public class CompletablePizza {
static int QUANTITY = 5;
static final int QUANTITY = 5;
public static CompletableFuture<Pizza>
makeCF(Pizza za) {
return CompletableFuture

View File

@ -6,7 +6,7 @@ import java.util.concurrent.*;
import java.util.stream.*;
import onjava.Nap;
class Frosting {
final class Frosting {
private Frosting() {}
static CompletableFuture<Frosting> make() {
new Nap(0.1);

View File

@ -9,11 +9,11 @@ import java.util.concurrent.*;
import com.google.common.collect.Sets;
public class IDChecker {
public static int SIZE = 100_000;
public static final int SIZE = 100_000;
static class MakeObjects
implements Supplier<List<Integer>> {
private Supplier<HasID> gen;
public MakeObjects(Supplier<HasID> gen) {
MakeObjects(Supplier<HasID> gen) {
this.gen = gen;
}
@Override

View File

@ -7,7 +7,7 @@ import java.util.stream.*;
import onjava.Timer;
public class PizzaParallelSteps {
static int QUANTITY = 5;
static final int QUANTITY = 5;
public static void main(String[] args) {
Timer timer = new Timer();
IntStream.range(0, QUANTITY)

View File

@ -7,7 +7,7 @@ import java.util.stream.*;
import onjava.Timer;
public class PizzaStreams {
static int QUANTITY = 5;
static final int QUANTITY = 5;
public static void main(String[] args) {
Timer timer = new Timer();
IntStream.range(0, QUANTITY)

View File

@ -23,7 +23,7 @@ class Safe implements SharedArg {
class SharedUser implements HasID {
private final int id;
public SharedUser(SharedArg sa) {
SharedUser(SharedArg sa) {
id = sa.get();
}
@Override

View File

@ -4,7 +4,6 @@
// Visit http://OnJava8.com for more book information.
import java.util.concurrent.*;
import java.util.stream.*;
import onjava.Nap;
public class StreamExceptions {
static Stream<Breakable>

View File

@ -8,7 +8,7 @@ class SyncConstructor implements HasID {
private final int id;
private static Object
constructorLock = new Object();
public SyncConstructor(SharedArg sa) {
SyncConstructor(SharedArg sa) {
synchronized(constructorLock) {
id = sa.get();
}

View File

@ -4,7 +4,7 @@
// Visit http://OnJava8.com for more book information.
import java.util.concurrent.atomic.*;
class SyncFactory implements HasID {
final class SyncFactory implements HasID {
private final int id;
private SyncFactory(SharedArg sa) {
id = sa.get();

View File

@ -133,7 +133,7 @@ class RandomInputSupplier implements Supplier<Input> {
// Create Inputs from a file of ';'-separated strings:
class FileInputSupplier implements Supplier<Input> {
private Iterator<String> input;
public FileInputSupplier(String fileName) {
FileInputSupplier(String fileName) {
try {
input = Files.lines(Paths.get(fileName))
.skip(1) // Skip the comment line

View File

@ -7,7 +7,7 @@ import java.util.*;
class Part {
String ss;
double dd;
public Part(String ss, double dd) {
Part(String ss, double dd) {
this.ss = ss;
this.dd = dd;
}

View File

@ -5,7 +5,7 @@
class DefaultComparison {
private int i, j, k;
public DefaultComparison(int i, int j, int k) {
DefaultComparison(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;

View File

@ -11,7 +11,7 @@ class Animal {
private final int id = counter++;
private final String name;
private final Size size;
public Animal(String name, Size size) {
Animal(String name, Size size) {
this.name = name;
this.size = size;
}
@ -36,13 +36,13 @@ class Animal {
}
class Dog extends Animal {
public Dog(String name, Size size) {
Dog(String name, Size size) {
super(name, size);
}
}
class Pig extends Animal {
public Pig(String name, Size size) {
Pig(String name, Size size) {
super(name, size);
}
}

View File

@ -5,7 +5,7 @@
import java.util.*;
class Dog2 extends Animal {
public Dog2(String name, Size size) {
Dog2(String name, Size size) {
super(name, size);
}
@Override
@ -16,7 +16,7 @@ class Dog2 extends Animal {
}
class Pig2 extends Animal {
public Pig2(String name, Size size) {
Pig2(String name, Size size) {
super(name, size);
}
@Override

View File

@ -5,7 +5,7 @@
class Reporter implements AutoCloseable {
String name = getClass().getSimpleName();
public Reporter() {
Reporter() {
System.out.println("Creating " + name);
}
public void close() {

View File

@ -17,7 +17,7 @@ class ConstructionException extends Exception {}
class NeedsCleanup2 extends NeedsCleanup {
// Construction can fail:
public NeedsCleanup2() throws ConstructionException {}
NeedsCleanup2() throws ConstructionException {}
}
public class CleanupIdiom {

View File

@ -7,7 +7,7 @@ class CloseException extends Exception {}
class Reporter2 implements AutoCloseable {
String name = getClass().getSimpleName();
public Reporter2() {
Reporter2() {
System.out.println("Creating " + name);
}
public void close() throws CloseException {

View File

@ -6,7 +6,7 @@
class CE extends Exception {}
class SecondExcept extends Reporter {
public SecondExcept() throws CE {
SecondExcept() throws CE {
super();
throw new CE();
}

View File

@ -6,9 +6,9 @@
class MyException2 extends Exception {
private int x;
public MyException2() {}
public MyException2(String msg) { super(msg); }
public MyException2(String msg, int x) {
MyException2() {}
MyException2(String msg) { super(msg); }
MyException2(String msg, int x) {
super(msg);
this.x = x;
}

View File

@ -4,8 +4,8 @@
// Visit http://OnJava8.com for more book information.
class MyException extends Exception {
public MyException() {}
public MyException(String msg) { super(msg); }
MyException() {}
MyException(String msg) { super(msg); }
}
public class FullConstructors {

View File

@ -10,7 +10,7 @@ import java.io.*;
class LoggingException extends Exception {
private static Logger logger =
Logger.getLogger("LoggingException");
public LoggingException() {
LoggingException() {
StringWriter trace = new StringWriter();
printStackTrace(new PrintWriter(trace));
logger.severe(trace.toString());

View File

@ -5,11 +5,11 @@
// Rethrow a different object from the one you caught
class OneException extends Exception {
public OneException(String s) { super(s); }
OneException(String s) { super(s); }
}
class TwoException extends Exception {
public TwoException(String s) { super(s); }
TwoException(String s) { super(s); }
}
public class RethrowNew {

View File

@ -11,7 +11,7 @@ class Foul extends BaseballException {}
class Strike extends BaseballException {}
abstract class Inning {
public Inning() throws BaseballException {}
Inning() throws BaseballException {}
public void event() throws BaseballException {
// Doesn't actually have to throw anything
}

View File

@ -8,26 +8,26 @@ class Foo {}
class Bar {
Foo f;
public Bar(Foo f) { this.f = f; }
Bar(Foo f) { this.f = f; }
}
class IBaz {
int i;
public IBaz(int i) {
IBaz(int i) {
this.i = i;
}
}
class LBaz {
long l;
public LBaz(long l) {
LBaz(long l) {
this.l = l;
}
}
class DBaz {
double d;
public DBaz(double d) {
DBaz(double d) {
this.d = d;
}
}

View File

@ -20,7 +20,7 @@ public class MethodReferences {
}
static class Description {
String about;
public Description(String desc) { about = desc; }
Description(String desc) { about = desc; }
void help(String msg) { // [4]
System.out.println(about + " " + msg);
}

View File

@ -10,7 +10,7 @@ import static onjava.Tuple.*;
class MixinProxy implements InvocationHandler {
Map<String, Object> delegatesByMethod;
@SuppressWarnings("unchecked")
public MixinProxy(Tuple2<Object, Class<?>>... pairs) {
MixinProxy(Tuple2<Object, Class<?>>... pairs) {
delegatesByMethod = new HashMap<>();
for(Tuple2<Object, Class<?>> pair : pairs) {
for(Method method : pair.a2.getMethods()) {

View File

@ -16,7 +16,7 @@ class IntegerFactory implements Supplier<Integer> {
class Widget {
private int id;
public Widget(int n) { id = n; }
Widget(int n) { id = n; }
@Override
public String toString() {
return "Widget " + id;
@ -38,7 +38,7 @@ class Fudge {
class Foo2<T> {
private List<T> x = new ArrayList<>();
public Foo2(Supplier<T> factory) {
Foo2(Supplier<T> factory) {
Suppliers.fill(x, factory, 5);
}
@Override

View File

@ -6,8 +6,8 @@ import java.util.*;
import java.util.function.*;
import onjava.*;
class FilledList<T> extends ArrayList<T> {
public FilledList(Supplier<T> gen, int size) {
public class FilledList<T> extends ArrayList<T> {
FilledList(Supplier<T> gen, int size) {
Suppliers.fill(this, gen, size);
}
public FilledList(T t, int size) {

View File

@ -9,7 +9,7 @@ class FixedSizeStack<T> {
private final int size;
private Object[] storage;
private int index = 0;
public FixedSizeStack(int size) {
FixedSizeStack(int size) {
this.size = size;
storage = new Object[size];
}

View File

@ -6,7 +6,7 @@ import java.util.function.*;
class ClassAsFactory<T> implements Supplier<T> {
Class<T> kind;
public ClassAsFactory(Class<T> kind) {
ClassAsFactory(Class<T> kind) {
this.kind = kind;
}
@Override

View File

@ -6,7 +6,7 @@
class Manipulator<T> {
private T obj;
public Manipulator(T x) { obj = x; }
Manipulator(T x) { obj = x; }
// Error: cannot find symbol: method f():
public void manipulate() { obj.f(); }
}

View File

@ -5,6 +5,6 @@
class Manipulator2<T extends HasF> {
private T obj;
public Manipulator2(T x) { obj = x; }
Manipulator2(T x) { obj = x; }
public void manipulate() { obj.f(); }
}

View File

@ -5,6 +5,6 @@
class Manipulator3 {
private HasF obj;
public Manipulator3(HasF x) { obj = x; }
Manipulator3(HasF x) { obj = x; }
public void manipulate() { obj.f(); }
}

View File

@ -8,7 +8,7 @@ interface TimeStamped { long getStamp(); }
class TimeStampedImp implements TimeStamped {
private final long timeStamp;
public TimeStampedImp() {
TimeStampedImp() {
timeStamp = new Date().getTime();
}
@Override

View File

@ -5,6 +5,6 @@
class ReturnGenericType<T extends HasF> {
private T obj;
public ReturnGenericType(T x) { obj = x; }
ReturnGenericType(T x) { obj = x; }
public T get() { return obj; }
}

View File

@ -11,7 +11,6 @@ class Product {
private final int id;
private String description;
private double price;
public
Product(int idNumber, String descr, double price) {
id = idNumber;
description = descr;
@ -37,13 +36,13 @@ class Product {
}
class Shelf extends ArrayList<Product> {
public Shelf(int nProducts) {
Shelf(int nProducts) {
Suppliers.fill(this, Product.generator, nProducts);
}
}
class Aisle extends ArrayList<Shelf> {
public Aisle(int nShelves, int nProducts) {
Aisle(int nShelves, int nProducts) {
for(int i = 0; i < nShelves; i++)
add(new Shelf(nProducts));
}

View File

@ -14,7 +14,7 @@ class Basic {
class Decorator extends Basic {
protected Basic basic;
public Decorator(Basic basic) { this.basic = basic; }
Decorator(Basic basic) { this.basic = basic; }
@Override
public void set(String val) { basic.set(val); }
@Override
@ -23,7 +23,7 @@ class Decorator extends Basic {
class TimeStamped extends Decorator {
private final long timeStamp;
public TimeStamped(Basic basic) {
TimeStamped(Basic basic) {
super(basic);
timeStamp = new Date().getTime();
}
@ -33,7 +33,7 @@ class TimeStamped extends Decorator {
class SerialNumbered extends Decorator {
private static long counter = 1;
private final long serialNumber = counter++;
public SerialNumbered(Basic basic) { super(basic); }
SerialNumbered(Basic basic) { super(basic); }
public long getSerialNumber() { return serialNumber; }
}

View File

@ -5,7 +5,7 @@
// Creating a constructor for an anonymous inner class
abstract class Base {
public Base(int i) {
Base(int i) {
System.out.println("Base constructor, i = " + i);
}
public abstract void f();

View File

@ -11,7 +11,7 @@ class Egg {
System.out.println("Egg.Yolk()");
}
}
public Egg() {
Egg() {
System.out.println("New Egg()");
y = new Yolk();
}

View File

@ -14,7 +14,7 @@ class Egg2 {
}
}
private Yolk y = new Yolk();
public Egg2() { System.out.println("New Egg2()"); }
Egg2() { System.out.println("New Egg2()"); }
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}

View File

@ -13,7 +13,7 @@ public class LocalInnerClass {
Counter getCounter(final String name) {
// A local inner class:
class LocalCounter implements Counter {
public LocalCounter() {
LocalCounter() {
// Local inner class can have a constructor
System.out.println("LocalCounter()");
}

View File

@ -40,9 +40,8 @@ public class Applicator {
System.out.println("Using Processor " + p.name());
System.out.println(p.process(s));
}
public static final String s =
"Disagreement with beliefs is by definition incorrect";
public static void main(String[] args) {
String s = "We are such stuff as dreams are made on";
apply(new Upcase(), s);
apply(new Downcase(), s);
apply(new Splitter(), s);
@ -50,10 +49,9 @@ public class Applicator {
}
/* Output:
Using Processor Upcase
DISAGREEMENT WITH BELIEFS IS BY DEFINITION INCORRECT
WE ARE SUCH STUFF AS DREAMS ARE MADE ON
Using Processor Downcase
disagreement with beliefs is by definition incorrect
we are such stuff as dreams are made on
Using Processor Splitter
[Disagreement, with, beliefs, is, by, definition,
incorrect]
[We, are, such, stuff, as, dreams, are, made, on]
*/

View File

@ -8,7 +8,7 @@ import interfaces.filters.*;
class FilterAdapter implements Processor {
Filter filter;
public FilterAdapter(Filter filter) {
FilterAdapter(Filter filter) {
this.filter = filter;
}
@Override

View File

@ -14,7 +14,7 @@ class DelayedTask implements Runnable, Delayed {
private final long trigger;
protected static List<DelayedTask> sequence =
new ArrayList<>();
public DelayedTask(int delayInMilliseconds) {
DelayedTask(int delayInMilliseconds) {
delta = delayInMilliseconds;
trigger = System.nanoTime() +
NANOSECONDS.convert(delta, MILLISECONDS);
@ -45,7 +45,7 @@ class DelayedTask implements Runnable, Delayed {
return String.format("(%d:%d)", id, delta);
}
public static class EndTask extends DelayedTask {
public EndTask(int delay) { super(delay); }
EndTask(int delay) { super(delay); }
@Override
public void run() {
sequence.forEach(dt ->

View File

@ -15,7 +15,7 @@ class Prioritized implements Comparable<Prioritized> {
private final int priority;
private static List<Prioritized> sequence =
new CopyOnWriteArrayList<>();
public Prioritized(int priority) {
Prioritized(int priority) {
this.priority = priority;
sequence.add(this);
}
@ -38,7 +38,7 @@ class Prioritized implements Comparable<Prioritized> {
}
}
public static class EndSentinel extends Prioritized {
public EndSentinel() { super(-1); }
EndSentinel() { super(-1); }
}
}
@ -48,7 +48,7 @@ class Producer implements Runnable {
private SplittableRandom rand =
new SplittableRandom(seed.getAndAdd(10));
private Queue<Prioritized> queue;
public Producer(Queue<Prioritized> q) {
Producer(Queue<Prioritized> q) {
queue = q;
}
@Override
@ -65,7 +65,6 @@ class Consumer implements Runnable {
private PriorityBlockingQueue<Prioritized> q;
private SplittableRandom rand =
new SplittableRandom(47);
public
Consumer(PriorityBlockingQueue<Prioritized> q) {
this.q = q;
}

View File

@ -4,7 +4,6 @@
// Visit http://OnJava8.com for more book information.
import java.util.function.*;
import java.util.concurrent.*;
import onjava.TimedAbort;
public class SafeReturn extends IntTestable {
private int i = 0;

View File

@ -38,7 +38,7 @@ class CriticalSection extends Guarded {
class Caller implements Runnable {
private Guarded g;
public Caller(Guarded g) { this.g = g; }
Caller(Guarded g) { this.g = g; }
private AtomicLong successfulCalls =
new AtomicLong();
private AtomicBoolean stop =

View File

@ -4,7 +4,6 @@
// Visit http://OnJava8.com for more book information.
import java.util.function.*;
import java.util.concurrent.*;
import onjava.TimedAbort;
public class UnsafeReturn extends IntTestable {
private int i = 0;

View File

@ -12,7 +12,7 @@ public class MappedIO {
private static int numOfUbuffInts = 100_000;
private abstract static class Tester {
private String name;
public Tester(String name) {
Tester(String name) {
this.name = name;
}
public void runTest() {

View File

@ -9,7 +9,7 @@ import java.util.function.*;
import static onjava.ConvertTo.*;
public interface Count {
public static class Boolean
class Boolean
implements Supplier<java.lang.Boolean> {
private boolean b = true;
@Override
@ -27,7 +27,7 @@ public interface Count {
return result;
}
}
public static class Pboolean {
class Pboolean {
private boolean b = true;
public boolean get() {
b = !b;
@ -38,7 +38,7 @@ public interface Count {
return primitive(new Boolean().array(sz));
}
}
public static class Byte
class Byte
implements Supplier<java.lang.Byte> {
private byte b;
@Override
@ -53,7 +53,7 @@ public interface Count {
return result;
}
}
public static class Pbyte {
class Pbyte {
private byte b;
public byte get() { return b++; }
public byte get(int n) { return get(); }
@ -63,7 +63,7 @@ public interface Count {
}
char[] CHARS =
"abcdefghijklmnopqrstuvwxyz".toCharArray();
public static class Character
class Character
implements Supplier<java.lang.Character> {
private int i;
@Override
@ -81,7 +81,7 @@ public interface Count {
return result;
}
}
public static class Pchar {
class Pchar {
private int i;
public char get() {
i = (i + 1) % CHARS.length;
@ -92,7 +92,7 @@ public interface Count {
return primitive(new Character().array(sz));
}
}
public static class Short
class Short
implements Supplier<java.lang.Short> {
short s;
@Override
@ -107,7 +107,7 @@ public interface Count {
return result;
}
}
public static class Pshort {
class Pshort {
short s;
public short get() { return s++; }
public short get(int n) { return get(); }
@ -115,7 +115,7 @@ public interface Count {
return primitive(new Short().array(sz));
}
}
public static class Integer
class Integer
implements Supplier<java.lang.Integer> {
int i;
@Override
@ -130,7 +130,7 @@ public interface Count {
return result;
}
}
public static class Pint implements IntSupplier {
class Pint implements IntSupplier {
int i;
public int get() { return i++; }
public int get(int n) { return get(); }
@ -140,7 +140,7 @@ public interface Count {
return primitive(new Integer().array(sz));
}
}
public static class Long
class Long
implements Supplier<java.lang.Long> {
private long l;
@Override
@ -155,7 +155,6 @@ public interface Count {
return result;
}
}
public static
class Plong implements LongSupplier {
private long l;
public long get() { return l++; }
@ -166,7 +165,7 @@ public interface Count {
return primitive(new Long().array(sz));
}
}
public class Float
class Float
implements Supplier<java.lang.Float> {
private int i;
@Override
@ -183,7 +182,7 @@ public interface Count {
return result;
}
}
public class Pfloat {
class Pfloat {
private int i;
public float get() { return i++; }
public float get(int n) { return get(); }
@ -191,7 +190,7 @@ public interface Count {
return primitive(new Float().array(sz));
}
}
public class Double
class Double
implements Supplier<java.lang.Double> {
private int i;
@Override
@ -208,7 +207,7 @@ public interface Count {
return result;
}
}
public class Pdouble implements DoubleSupplier {
class Pdouble implements DoubleSupplier {
private int i;
public double get() { return i++; }
public double get(int n) { return get(); }

View File

@ -58,15 +58,15 @@ extends AbstractMap<Integer,String> {
.toCollection(LinkedHashSet::new));
}
public static void main(String[] args) {
final int LIM = 6;
final int size = 6;
CountMap cm = new CountMap(60);
System.out.println(cm);
System.out.println(cm.get(500));
cm.values().stream()
.limit(LIM)
.limit(size)
.forEach(System.out::println);
System.out.println();
new Random(47).ints(LIM, 0, 1000)
new Random(47).ints(size, 0, 1000)
.mapToObj(cm::get)
.forEach(System.out::println);
}

View File

@ -10,7 +10,7 @@ import static onjava.ConvertTo.*;
public interface Rand {
int MOD = 10_000;
public static class Boolean
class Boolean
implements Supplier<java.lang.Boolean> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -27,12 +27,12 @@ public interface Rand {
return result;
}
}
public static class Pboolean {
class Pboolean {
public boolean[] array(int sz) {
return primitive(new Boolean().array(sz));
}
}
public static class Byte
class Byte
implements Supplier<java.lang.Byte> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -49,12 +49,12 @@ public interface Rand {
return result;
}
}
public static class Pbyte {
class Pbyte {
public byte[] array(int sz) {
return primitive(new Byte().array(sz));
}
}
public static class Character
class Character
implements Supplier<java.lang.Character> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -71,12 +71,12 @@ public interface Rand {
return result;
}
}
public static class Pchar {
class Pchar {
public char[] array(int sz) {
return primitive(new Character().array(sz));
}
}
public static class Short
class Short
implements Supplier<java.lang.Short> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -93,12 +93,12 @@ public interface Rand {
return result;
}
}
public static class Pshort {
class Pshort {
public short[] array(int sz) {
return primitive(new Short().array(sz));
}
}
public static class Integer
class Integer
implements Supplier<java.lang.Integer> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -117,7 +117,7 @@ public interface Rand {
return result;
}
}
public static class Pint implements IntSupplier {
class Pint implements IntSupplier {
SplittableRandom r = new SplittableRandom(47);
@Override
public int getAsInt() {
@ -128,7 +128,7 @@ public interface Rand {
return r.ints(sz, 0, MOD).toArray();
}
}
public static class Long
class Long
implements Supplier<java.lang.Long> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -147,7 +147,6 @@ public interface Rand {
return result;
}
}
public static
class Plong implements LongSupplier {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -159,7 +158,7 @@ public interface Rand {
return r.longs(sz, 0, MOD).toArray();
}
}
public static class Float
class Float
implements Supplier<java.lang.Float> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -176,7 +175,7 @@ public interface Rand {
return result;
}
}
public static class Pfloat {
class Pfloat {
public float[] array(int sz) {
return primitive(new Float().array(sz));
}
@ -185,7 +184,7 @@ public interface Rand {
return
((double)Math.round(d * 1000.0)) / 100.0;
}
public static class Double
class Double
implements Supplier<java.lang.Double> {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -205,7 +204,6 @@ public interface Rand {
return result;
}
}
public static
class Pdouble implements DoubleSupplier {
SplittableRandom r = new SplittableRandom(47);
@Override
@ -222,7 +220,7 @@ public interface Rand {
return result;
}
}
public static class String
class String
implements Supplier<java.lang.String> {
SplittableRandom r = new SplittableRandom(47);
private int strlen = 7; // Default length

View File

@ -3,9 +3,9 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
class A { public A(int x) {} }
class B { public B(long x) {} }
class C { public C(double x) {} }
class A { A(int x) {} }
class B { B(long x) {} }
class C { C(double x) {} }
// Other classes that aren't exposed by the
// facade go here ...

View File

@ -12,7 +12,7 @@ interface ProxyBase {
class Proxy implements ProxyBase {
private ProxyBase implementation;
public Proxy() {
Proxy() {
implementation = new Implementation();
}
// Pass method calls to the implementation:

View File

@ -15,7 +15,6 @@ interface PolymorphicFactory {
class RandomShapes implements Supplier<Shape> {
private final PolymorphicFactory[] factories;
private Random rand = new Random(42);
public
RandomShapes(PolymorphicFactory... factories) {
this.factories = factories;
}

View File

@ -15,7 +15,7 @@ interface Resource {
// implements thread-safe lazy initialization:
final class Singleton {
private static class
private static final class
ResourceImpl implements Resource {
private int i;
private ResourceImpl(int i) {

View File

@ -13,7 +13,7 @@ interface StateBase {
class State implements StateBase {
private StateBase implementation;
public State(StateBase imp) {
State(StateBase imp) {
implementation = imp;
}
@Override

View File

@ -6,7 +6,7 @@
import java.util.stream.*;
abstract class ApplicationFramework {
public ApplicationFramework() {
ApplicationFramework() {
templateMethod();
}
abstract void customize1();

View File

@ -17,7 +17,7 @@ interface WhatIWant {
class ProxyAdapter implements WhatIWant {
WhatIHave whatIHave;
public ProxyAdapter(WhatIHave wih) {
ProxyAdapter(WhatIHave wih) {
whatIHave = wih;
}
@Override

View File

@ -11,11 +11,11 @@ import java.util.function.*;
class Result {
boolean success;
List<Double> line;
public Result(List<Double> data) {
Result(List<Double> data) {
success = true;
line = data;
}
public Result() {
Result() {
success = false;
line = Collections.<Double>emptyList();
}

View File

@ -33,7 +33,7 @@ class TypeMap<T> {
// from ParseTrash.fillBin():
class TypeMapAdapter implements Fillable {
TypeMap<Trash> map;
public TypeMapAdapter(TypeMap<Trash> tm) {
TypeMapAdapter(TypeMap<Trash> tm) {
map = tm;
}
@Override

View File

@ -11,7 +11,7 @@ class Flower {
private boolean isOpen;
private boolean alreadyOpen;
private boolean alreadyClosed;
public Flower() { isOpen = false; }
Flower() { isOpen = false; }
OpenNotifier opening = new OpenNotifier();
CloseNotifier closing = new CloseNotifier();
public void open() { // Opens its petals
@ -48,7 +48,7 @@ class Flower {
class Bee {
private String name;
public Bee(String nm) { name = nm; }
Bee(String nm) { name = nm; }
// Observe openings:
public Observer openObserver() {
return (ob, a) -> System.out.println(
@ -63,7 +63,7 @@ class Bee {
class Hummingbird {
private String name;
public Hummingbird(String nm) { name = nm; }
Hummingbird(String nm) { name = nm; }
public Observer openObserver() {
return (ob, a) -> System.out.println(
"Hummingbird " + name +

View File

@ -54,7 +54,7 @@ class Washer extends StateMachine {
new Wash(), new Spin(),
new Rinse(), new Spin(),
};
public Washer() { runAll(); }
Washer() { runAll(); }
@Override
public boolean changeState() {
if(i < states.length) {

View File

@ -35,7 +35,7 @@ class Bisection extends FindMinima {
// The "Context" controls the strategy:
class MinimaSolver {
private FindMinima strategy;
public MinimaSolver(FindMinima strat) {
MinimaSolver(FindMinima strat) {
strategy = strat;
}
List<Double> minima(List<Double> line) {

View File

@ -8,7 +8,7 @@ class Shared {
private int refcount = 0;
private static long counter = 0;
private final long id = counter++;
public Shared() {
Shared() {
System.out.println("Creating " + this);
}
public void addRef() { refcount++; }
@ -26,7 +26,7 @@ class Composing {
private Shared shared;
private static long counter = 0;
private final long id = counter++;
public Composing(Shared shared) {
Composing(Shared shared) {
System.out.println("Creating " + this);
this.shared = shared;
this.shared.addRef();

View File

@ -9,7 +9,7 @@ import java.util.stream.*;
class Int2 implements Cloneable {
private int i;
public Int2(int ii) { i = ii; }
Int2(int ii) { i = ii; }
public void increment() { i++; }
@Override
public String toString() {
@ -28,7 +28,7 @@ class Int2 implements Cloneable {
// Inheritance doesn't remove cloneability:
class Int3 extends Int2 {
private int j; // Automatically duplicated
public Int3(int i) { super(i); }
Int3(int i) { super(i); }
}
public class AddingClone {

View File

@ -9,7 +9,7 @@ import java.util.stream.*;
class Int {
private int i;
public Int(int ii) { i = ii; }
Int(int ii) { i = ii; }
public void increment() { i++; }
@Override
public String toString() {

View File

@ -14,13 +14,13 @@ class FruitQualities {
private int smell;
// etc.
// No-arg constructor:
public FruitQualities() {
FruitQualities() {
// Do something meaningful...
}
// Other constructors:
// ...
// Copy constructor:
public FruitQualities(FruitQualities f) {
FruitQualities(FruitQualities f) {
weight = f.weight;
color = f.color;
firmness = f.firmness;
@ -32,15 +32,15 @@ class FruitQualities {
class Seed {
// Members...
public Seed() { /* No-arg constructor */ }
public Seed(Seed s) { /* Copy constructor */ }
Seed() { /* No-arg constructor */ }
Seed(Seed s) { /* Copy constructor */ }
}
class Fruit {
private FruitQualities fq;
private int seeds;
private Seed[] s;
public Fruit(FruitQualities q, int seedCount) {
Fruit(FruitQualities q, int seedCount) {
fq = q;
seeds = seedCount;
s = new Seed[seeds];
@ -50,7 +50,7 @@ class Fruit {
// Other constructors:
// ...
// Copy constructor:
public Fruit(Fruit f) {
Fruit(Fruit f) {
fq = new FruitQualities(f.fq);
seeds = f.seeds;
s = new Seed[seeds];
@ -70,10 +70,10 @@ class Fruit {
}
class Tomato extends Fruit {
public Tomato() {
Tomato() {
super(new FruitQualities(), 100);
}
public Tomato(Tomato t) { // Copy-constructor
Tomato(Tomato t) { // Copy-constructor
super(t); // Upcast to base copy-constructor
// Other copy-construction activities...
}
@ -82,21 +82,21 @@ class Tomato extends Fruit {
class ZebraQualities extends FruitQualities {
private int stripedness;
// No-arg constructor:
public ZebraQualities() {
ZebraQualities() {
super();
// do something meaningful...
}
public ZebraQualities(ZebraQualities z) {
ZebraQualities(ZebraQualities z) {
super(z);
stripedness = z.stripedness;
}
}
class GreenZebra extends Tomato {
public GreenZebra() {
GreenZebra() {
addQualities(new ZebraQualities());
}
public GreenZebra(GreenZebra g) {
GreenZebra(GreenZebra g) {
super(g); // Calls Tomato(Tomato)
// Restore the right qualities:
addQualities(new ZebraQualities());

View File

@ -6,7 +6,7 @@
class Mutable {
private int data;
public Mutable(int initVal) {
Mutable(int initVal) {
data = initVal;
}
public Mutable add(int x) {

View File

@ -6,7 +6,7 @@
class Duplo implements Cloneable {
private int n;
public Duplo(int n) { this.n = n; }
Duplo(int n) { this.n = n; }
@Override
public Duplo clone() { // [1]
try {

View File

@ -8,7 +8,7 @@ import java.util.stream.*;
class IntValue {
private int n;
public IntValue(int x) { n = x; }
IntValue(int x) { n = x; }
public int getValue() { return n; }
public void setValue(int n) { this.n = n; }
public void increment() { n++; }

View File

@ -8,7 +8,7 @@ import java.util.stream.*;
class IntValue2 {
public int n;
public IntValue2(int n) { this.n = n; }
IntValue2(int n) { this.n = n; }
}
public class SimplerMutableInteger {

View File

@ -7,7 +7,7 @@ import java.util.*;
class Value {
int i; // Package access
public Value(int i) { this.i = i; }
Value(int i) { this.i = i; }
}
public class FinalData {

View File

@ -7,7 +7,7 @@
class Villain {
private String name;
protected void set(String nm) { name = nm; }
public Villain(String name) { this.name = name; }
Villain(String name) { this.name = name; }
@Override
public String toString() {
return "I'm a Villain and my name is " + name;

View File

@ -15,7 +15,7 @@ abstract class Shape implements Serializable {
private static int counter = 0;
public abstract void setColor(Color newColor);
public abstract Color getColor();
public Shape(int xVal, int yVal, int dim) {
Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
@ -40,7 +40,7 @@ abstract class Shape implements Serializable {
class Circle extends Shape {
private static Color color = Color.RED;
public Circle(int xVal, int yVal, int dim) {
Circle(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public void setColor(Color newColor) {
@ -51,7 +51,7 @@ class Circle extends Shape {
class Square extends Shape {
private static Color color = Color.RED;
public Square(int xVal, int yVal, int dim) {
Square(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public void setColor(Color newColor) {
@ -70,7 +70,7 @@ class Line extends Shape {
throws IOException, ClassNotFoundException {
color = (Color)os.readObject();
}
public Line(int xVal, int yVal, int dim) {
Line(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public void setColor(Color newColor) {

View File

@ -6,7 +6,7 @@
import java.io.*;
class Blip1 implements Externalizable {
public Blip1() {
Blip1() {
System.out.println("Blip1 Constructor");
}
@Override

View File

@ -8,7 +8,7 @@ import java.util.*;
class Data implements Serializable {
private int n;
public Data(int n) { this.n = n; }
Data(int n) { this.n = n; }
@Override
public String toString() {
return Integer.toString(n);

View File

@ -8,7 +8,7 @@ import java.util.stream.*;
class Pair {
public final Character c;
public final Integer i;
public Pair(Character c, Integer i) {
Pair(Character c, Integer i) {
this.c = c;
this.i = i;
}

View File

@ -12,7 +12,6 @@ class Person {
public final Optional<String> address;
// etc.
public final boolean empty;
public
Person(String first, String last, String address) {
this.first = Optional.ofNullable(first);
this.last = Optional.ofNullable(last);
@ -21,11 +20,11 @@ class Person {
&& !this.last.isPresent()
&& !this.address.isPresent();
}
public Person(String first, String last) {
Person(String first, String last) {
this(first, last, null);
}
public Person(String last) { this(null, last, null); }
public Person() { this(null, null, null); }
Person(String last) { this(null, last, null); }
Person() { this(null, null, null); }
@Override
public String toString() {
if(empty)

View File

@ -11,7 +11,7 @@ import typeinfo.pets.*;
public class PetCount3 {
static class Counter extends
LinkedHashMap<Class<? extends Pet>, Integer> {
public Counter() {
Counter() {
super(LiteralPetCreator.ALL_TYPES.stream()
.map(lpc -> Pair.make(lpc, 0))
.collect(

View File

@ -9,11 +9,11 @@ class EmptyTitleException extends RuntimeException {}
class Position {
private String title;
private Person person;
public Position(String jobTitle, Person employee) {
Position(String jobTitle, Person employee) {
setTitle(jobTitle);
setPerson(employee);
}
public Position(String jobTitle) {
Position(String jobTitle) {
this(jobTitle, null);
}
public String getTitle() { return title; }

View File

@ -7,7 +7,7 @@ import java.lang.reflect.*;
class MethodSelector implements InvocationHandler {
private Object proxied;
public MethodSelector(Object proxied) {
MethodSelector(Object proxied) {
this.proxied = proxied;
}
@Override

View File

@ -6,7 +6,7 @@ import java.lang.reflect.*;
class DynamicProxyHandler implements InvocationHandler {
private Object proxied;
public DynamicProxyHandler(Object proxied) {
DynamicProxyHandler(Object proxied) {
this.proxied = proxied;
}
@Override

View File

@ -21,7 +21,7 @@ class RealObject implements Interface {
class SimpleProxy implements Interface {
private Interface proxied;
public SimpleProxy(Interface proxied) {
SimpleProxy(Interface proxied) {
this.proxied = proxied;
}
@Override