Line width <= 60

This commit is contained in:
Bruce Eckel 2015-06-03 23:39:44 -07:00
parent d24a98545c
commit 2327db4c15
22 changed files with 69 additions and 60 deletions

View File

@ -26,7 +26,8 @@ public class CompType implements Comparable<CompType> {
}
private static Random r = new Random(47);
public static Generator<CompType> generator() {
return () -> new CompType(r.nextInt(100),r.nextInt(100));
return () ->
new CompType(r.nextInt(100),r.nextInt(100));
}
public static void main(String[] args) {
CompType[] a =

View File

@ -18,7 +18,8 @@ class MyList(list): # Inherit from list
reversed.reverse() # Built-in list method
return reversed
list2 = MyList(aList) # No 'new' needed for object creation
# No 'new' necessary for object creation:
list2 = MyList(aList)
print(type(list2)) # <class '__main__.MyList'>
print(list2.getReversed()) # [8, 7, 6, 5, 4, 3, 2, 1]
#:~

View File

@ -12,7 +12,7 @@ public class CloseResource {
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
ServerSocket server = new ServerSocket(8080);
try (InputStream socketInput =
try(InputStream socketInput =
new Socket("localhost", 8080).getInputStream()) {
exec.execute(new IOBlocked(socketInput));
exec.execute(new IOBlocked(System.in));

View File

@ -139,10 +139,10 @@ public class GreenhouseScheduler {
if(rand.nextInt(5) == 4)
tempDirection = -tempDirection;
// Store previous value:
lastTemp += tempDirection * (1.0f + rand.nextFloat());
lastTemp += tempDirection*(1.0f+rand.nextFloat());
if(rand.nextInt(5) == 4)
humidityDirection = -humidityDirection;
lastHumidity += humidityDirection * rand.nextFloat();
lastHumidity += humidityDirection*rand.nextFloat();
// Calendar must be cloned, otherwise all
// DataPoints hold references to the same lastTime.
// For a basic object like Calendar, clone() is OK.

View File

@ -34,7 +34,7 @@ public class NIOInterruption {
InetSocketAddress isa =
new InetSocketAddress("localhost", 8080);
SocketChannel sc1 = SocketChannel.open(isa);
try (SocketChannel sc2 = SocketChannel.open(isa)) {
try(SocketChannel sc2 = SocketChannel.open(isa)) {
Future<?> f = exec.submit(new NIOBlocked(sc1));
exec.execute(new NIOBlocked(sc2));
exec.shutdown();

View File

@ -1,5 +1,5 @@
//: containers/FillingLists.java
// ©2015 MindView LLC: see Copyright.txt
// 2015 MindView LLC: see Copyright.txt
// The Collections.fill() & Collections.nCopies() methods.
import java.util.*;

View File

@ -9,8 +9,8 @@ public class BasicFileOutput {
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFile.read("BasicFileOutput.java")));
try (PrintWriter out = new PrintWriter(
new BufferedWriter(new FileWriter(file)))) {
try(PrintWriter out = new PrintWriter(
new BufferedWriter(new FileWriter(file)))) {
int lineCount = 1;
String s;
while((s = in.readLine()) != null )

View File

@ -40,8 +40,8 @@ public class Blip3 implements Externalizable {
print("Constructing objects:");
Blip3 b3 = new Blip3("A String ", 47);
print(b3);
try (ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("Blip3.out"))) {
try(ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("Blip3.out"))) {
print("Saving object:");
o.writeObject(b3);
}

View File

@ -42,8 +42,8 @@ public class Blips {
print("Constructing objects:");
Blip1 b1 = new Blip1();
Blip2 b2 = new Blip2();
try (ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("Blips.out"))) {
try(ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("Blips.out"))) {
print("Saving objects:");
o.writeObject(b1);
o.writeObject(b2);

View File

@ -6,7 +6,8 @@ import java.io.*;
public class FileLocking {
public static void main(String[] args) throws Exception {
try (FileOutputStream fos = new FileOutputStream("file.txt")) {
try(FileOutputStream fos =
new FileOutputStream("file.txt")) {
FileLock fl = fos.getChannel().tryLock();
if(fl != null) {
System.out.println("Locked File");

View File

@ -15,11 +15,11 @@ public class GZIPcompress {
System.exit(1);
}
BufferedOutputStream out;
try (InputStream in = new BufferedInputStream(
new FileInputStream(args[0]))) {
try(InputStream in = new BufferedInputStream(
new FileInputStream(args[0]))) {
out = new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream("test.gz")));
new FileOutputStream("test.gz")));
System.out.println("Writing file");
int c;
while((c = in.read()) != -1)

View File

@ -22,8 +22,8 @@ public class Logon implements Serializable {
public static void main(String[] args) throws Exception {
Logon a = new Logon("Hulk", "myLittlePony");
print("logon a = " + a);
try (ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("Logon.out"))) {
try(ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("Logon.out"))) {
o.writeObject(a);
}
TimeUnit.SECONDS.sleep(1); // Delay

View File

@ -27,9 +27,10 @@ public class MappedIO {
new Tester("Stream Write") {
@Override
public void test() throws IOException {
try (DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(new File("temp.tmp"))))) {
try(DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(
new File("temp.tmp"))))) {
for(int i = 0; i < numOfInts; i++)
dos.writeInt(i);
}
@ -38,11 +39,12 @@ public class MappedIO {
new Tester("Mapped Write") {
@Override
public void test() throws IOException {
try (FileChannel fc = new RandomAccessFile("temp.tmp", "rw")
.getChannel()) {
try(FileChannel fc =
new RandomAccessFile("temp.tmp", "rw")
.getChannel()) {
IntBuffer ib = fc.map(
FileChannel.MapMode.READ_WRITE, 0, fc.size())
.asIntBuffer();
FileChannel.MapMode.READ_WRITE, 0, fc.size())
.asIntBuffer();
for(int i = 0; i < numOfInts; i++)
ib.put(i);
}
@ -51,9 +53,9 @@ public class MappedIO {
new Tester("Stream Read") {
@Override
public void test() throws IOException {
try (DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream("temp.tmp")))) {
try(DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream("temp.tmp")))) {
for(int i = 0; i < numOfInts; i++)
dis.readInt();
}
@ -62,11 +64,11 @@ public class MappedIO {
new Tester("Mapped Read") {
@Override
public void test() throws IOException {
try (FileChannel fc = new FileInputStream(
new File("temp.tmp")).getChannel()) {
try(FileChannel fc = new FileInputStream(
new File("temp.tmp")).getChannel()) {
IntBuffer ib = fc.map(
FileChannel.MapMode.READ_ONLY, 0, fc.size())
.asIntBuffer();
FileChannel.MapMode.READ_ONLY, 0, fc.size())
.asIntBuffer();
while(ib.hasRemaining())
ib.get();
}
@ -75,8 +77,8 @@ public class MappedIO {
new Tester("Stream Read/Write") {
@Override
public void test() throws IOException {
try (RandomAccessFile raf = new RandomAccessFile(
new File("temp.tmp"), "rw")) {
try(RandomAccessFile raf = new RandomAccessFile(
new File("temp.tmp"), "rw")) {
raf.writeInt(1);
for(int i = 0; i < numOfUbuffInts; i++) {
raf.seek(raf.length() - 4);
@ -88,11 +90,11 @@ public class MappedIO {
new Tester("Mapped Read/Write") {
@Override
public void test() throws IOException {
try (FileChannel fc = new RandomAccessFile(
new File("temp.tmp"), "rw").getChannel()) {
try(FileChannel fc = new RandomAccessFile(
new File("temp.tmp"), "rw").getChannel()) {
IntBuffer ib = fc.map(
FileChannel.MapMode.READ_WRITE, 0, fc.size())
.asIntBuffer();
FileChannel.MapMode.READ_WRITE, 0, fc.size())
.asIntBuffer();
ib.put(0);
for(int i = 1; i < numOfUbuffInts; i++)
ib.put(ib.get(i - 1));

View File

@ -9,9 +9,9 @@ public class Redirecting {
PrintStream console = System.out;
BufferedInputStream in = new BufferedInputStream(
new FileInputStream("Redirecting.java"));
try (PrintStream out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream("test.out")))) {
try(PrintStream out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream("test.out")))) {
System.setIn(in);
System.setOut(out);
System.setErr(out);

View File

@ -5,9 +5,9 @@ import java.io.*;
public class StoringAndRecoveringData {
public static void main(String[] args)
throws IOException {
try (DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("Data.txt")))) {
try(DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("Data.txt")))) {
out.writeDouble(3.14159);
out.writeUTF("That was pi");
out.writeDouble(1.41413);

View File

@ -5,7 +5,8 @@ import java.io.*;
public class UsingRandomAccessFile {
static String file = "rtest.dat";
static void display() throws IOException {
try (RandomAccessFile rf = new RandomAccessFile(file, "r")) {
try(RandomAccessFile rf =
new RandomAccessFile(file, "r")) {
for(int i = 0; i < 7; i++)
System.out.println(
"Value " + i + ": " + rf.readDouble());

View File

@ -47,8 +47,8 @@ public class Worm implements Serializable {
throws ClassNotFoundException, IOException {
Worm w = new Worm(6, 'a');
print("w = " + w);
try (ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("worm.out"))) {
try(ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("worm.out"))) {
out.writeObject("Worm storage\n");
out.writeObject(w);
out.close(); // Also flushes output

View File

@ -15,7 +15,8 @@ public class ZipCompress {
CheckedOutputStream csum =
new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
try (BufferedOutputStream out = new BufferedOutputStream(zos)) {
try(BufferedOutputStream out =
new BufferedOutputStream(zos)) {
zos.setComment("A test of Java Zipping");
// No corresponding getComment(), though.
for(String arg : args) {
@ -38,7 +39,7 @@ public class ZipCompress {
CheckedInputStream csumi =
new CheckedInputStream(fi, new Adler32());
ZipInputStream in2 = new ZipInputStream(csumi);
try(BufferedInputStream bis =
try(BufferedInputStream bis =
new BufferedInputStream(in2)) {
ZipEntry ze;
while((ze = in2.getNextEntry()) != null) {
@ -48,7 +49,7 @@ public class ZipCompress {
System.out.write(x);
}
if(args.length == 1)
print("Checksum: " + csumi.getChecksum().getValue());
print("Checksum: "+csumi.getChecksum().getValue());
}
// Alternative way to open and read Zip files:
ZipFile zf = new ZipFile("test.zip");

View File

@ -6,8 +6,8 @@ import java.io.*;
public class BinaryFile {
public static byte[] read(File bFile) throws IOException{
try (BufferedInputStream bf = new BufferedInputStream(
new FileInputStream(bFile))) {
try(BufferedInputStream bf = new BufferedInputStream(
new FileInputStream(bFile))) {
byte[] data = new byte[bf.available()];
bf.read(data);
return data;

View File

@ -11,7 +11,8 @@ public class TextFile extends ArrayList<String> {
public static String read(String fileName) {
StringBuilder sb = new StringBuilder();
try {
try (BufferedReader in = new BufferedReader(new FileReader(
try(BufferedReader in =
new BufferedReader(new FileReader(
new File(fileName).getAbsoluteFile()))) {
String s;
while((s = in.readLine()) != null) {
@ -27,8 +28,8 @@ public class TextFile extends ArrayList<String> {
// Write a single file in one method call:
public static void write(String fileName, String text) {
try {
try (PrintWriter out = new PrintWriter(
new File(fileName).getAbsoluteFile())) {
try(PrintWriter out = new PrintWriter(
new File(fileName).getAbsoluteFile())) {
out.print(text);
}
} catch(IOException e) {
@ -48,8 +49,8 @@ public class TextFile extends ArrayList<String> {
}
public void write(String fileName) {
try {
try (PrintWriter out = new PrintWriter(
new File(fileName).getAbsoluteFile())) {
try(PrintWriter out = new PrintWriter(
new File(fileName).getAbsoluteFile())) {
for(String item : this)
out.println(item);
}

View File

@ -10,7 +10,7 @@ public class ParseTrash {
public static <T extends Trash> void
fillBin(String filename, Fillable<T> bin) {
try {
try (BufferedReader data = new BufferedReader(
try(BufferedReader data = new BufferedReader(
new FileReader(filename))) {
String buf;
while((buf = data.readLine())!= null) {

View File

@ -57,7 +57,8 @@ public class JUnitDemo {
// A helper method to reduce code duplication. As long
// as it isn't annotated with @Test, it will not
// be automatically executed by JUnit.
private void compare(ArrayList<String> lst, String[] strs) {
private
void compare(ArrayList<String> lst, String[] strs) {
String[] array = (String[])lst.toArray();
assertTrue("Arrays not the same length",
array.length == strs.length);