Line width <= 60
This commit is contained in:
parent
d24a98545c
commit
2327db4c15
@ -26,7 +26,8 @@ public class CompType implements Comparable<CompType> {
|
|||||||
}
|
}
|
||||||
private static Random r = new Random(47);
|
private static Random r = new Random(47);
|
||||||
public static Generator<CompType> generator() {
|
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) {
|
public static void main(String[] args) {
|
||||||
CompType[] a =
|
CompType[] a =
|
||||||
|
@ -18,7 +18,8 @@ class MyList(list): # Inherit from list
|
|||||||
reversed.reverse() # Built-in list method
|
reversed.reverse() # Built-in list method
|
||||||
return reversed
|
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(type(list2)) # <class '__main__.MyList'>
|
||||||
print(list2.getReversed()) # [8, 7, 6, 5, 4, 3, 2, 1]
|
print(list2.getReversed()) # [8, 7, 6, 5, 4, 3, 2, 1]
|
||||||
#:~
|
#:~
|
||||||
|
@ -12,7 +12,7 @@ public class CloseResource {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
ExecutorService exec = Executors.newCachedThreadPool();
|
ExecutorService exec = Executors.newCachedThreadPool();
|
||||||
ServerSocket server = new ServerSocket(8080);
|
ServerSocket server = new ServerSocket(8080);
|
||||||
try (InputStream socketInput =
|
try(InputStream socketInput =
|
||||||
new Socket("localhost", 8080).getInputStream()) {
|
new Socket("localhost", 8080).getInputStream()) {
|
||||||
exec.execute(new IOBlocked(socketInput));
|
exec.execute(new IOBlocked(socketInput));
|
||||||
exec.execute(new IOBlocked(System.in));
|
exec.execute(new IOBlocked(System.in));
|
||||||
|
@ -139,10 +139,10 @@ public class GreenhouseScheduler {
|
|||||||
if(rand.nextInt(5) == 4)
|
if(rand.nextInt(5) == 4)
|
||||||
tempDirection = -tempDirection;
|
tempDirection = -tempDirection;
|
||||||
// Store previous value:
|
// Store previous value:
|
||||||
lastTemp += tempDirection * (1.0f + rand.nextFloat());
|
lastTemp += tempDirection*(1.0f+rand.nextFloat());
|
||||||
if(rand.nextInt(5) == 4)
|
if(rand.nextInt(5) == 4)
|
||||||
humidityDirection = -humidityDirection;
|
humidityDirection = -humidityDirection;
|
||||||
lastHumidity += humidityDirection * rand.nextFloat();
|
lastHumidity += humidityDirection*rand.nextFloat();
|
||||||
// Calendar must be cloned, otherwise all
|
// Calendar must be cloned, otherwise all
|
||||||
// DataPoints hold references to the same lastTime.
|
// DataPoints hold references to the same lastTime.
|
||||||
// For a basic object like Calendar, clone() is OK.
|
// For a basic object like Calendar, clone() is OK.
|
||||||
|
@ -34,7 +34,7 @@ public class NIOInterruption {
|
|||||||
InetSocketAddress isa =
|
InetSocketAddress isa =
|
||||||
new InetSocketAddress("localhost", 8080);
|
new InetSocketAddress("localhost", 8080);
|
||||||
SocketChannel sc1 = SocketChannel.open(isa);
|
SocketChannel sc1 = SocketChannel.open(isa);
|
||||||
try (SocketChannel sc2 = SocketChannel.open(isa)) {
|
try(SocketChannel sc2 = SocketChannel.open(isa)) {
|
||||||
Future<?> f = exec.submit(new NIOBlocked(sc1));
|
Future<?> f = exec.submit(new NIOBlocked(sc1));
|
||||||
exec.execute(new NIOBlocked(sc2));
|
exec.execute(new NIOBlocked(sc2));
|
||||||
exec.shutdown();
|
exec.shutdown();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//: containers/FillingLists.java
|
//: containers/FillingLists.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// 2015 MindView LLC: see Copyright.txt
|
||||||
// The Collections.fill() & Collections.nCopies() methods.
|
// The Collections.fill() & Collections.nCopies() methods.
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ public class BasicFileOutput {
|
|||||||
BufferedReader in = new BufferedReader(
|
BufferedReader in = new BufferedReader(
|
||||||
new StringReader(
|
new StringReader(
|
||||||
BufferedInputFile.read("BasicFileOutput.java")));
|
BufferedInputFile.read("BasicFileOutput.java")));
|
||||||
try (PrintWriter out = new PrintWriter(
|
try(PrintWriter out = new PrintWriter(
|
||||||
new BufferedWriter(new FileWriter(file)))) {
|
new BufferedWriter(new FileWriter(file)))) {
|
||||||
int lineCount = 1;
|
int lineCount = 1;
|
||||||
String s;
|
String s;
|
||||||
while((s = in.readLine()) != null )
|
while((s = in.readLine()) != null )
|
||||||
|
@ -40,8 +40,8 @@ public class Blip3 implements Externalizable {
|
|||||||
print("Constructing objects:");
|
print("Constructing objects:");
|
||||||
Blip3 b3 = new Blip3("A String ", 47);
|
Blip3 b3 = new Blip3("A String ", 47);
|
||||||
print(b3);
|
print(b3);
|
||||||
try (ObjectOutputStream o = new ObjectOutputStream(
|
try(ObjectOutputStream o = new ObjectOutputStream(
|
||||||
new FileOutputStream("Blip3.out"))) {
|
new FileOutputStream("Blip3.out"))) {
|
||||||
print("Saving object:");
|
print("Saving object:");
|
||||||
o.writeObject(b3);
|
o.writeObject(b3);
|
||||||
}
|
}
|
||||||
|
@ -42,8 +42,8 @@ public class Blips {
|
|||||||
print("Constructing objects:");
|
print("Constructing objects:");
|
||||||
Blip1 b1 = new Blip1();
|
Blip1 b1 = new Blip1();
|
||||||
Blip2 b2 = new Blip2();
|
Blip2 b2 = new Blip2();
|
||||||
try (ObjectOutputStream o = new ObjectOutputStream(
|
try(ObjectOutputStream o = new ObjectOutputStream(
|
||||||
new FileOutputStream("Blips.out"))) {
|
new FileOutputStream("Blips.out"))) {
|
||||||
print("Saving objects:");
|
print("Saving objects:");
|
||||||
o.writeObject(b1);
|
o.writeObject(b1);
|
||||||
o.writeObject(b2);
|
o.writeObject(b2);
|
||||||
|
@ -6,7 +6,8 @@ import java.io.*;
|
|||||||
|
|
||||||
public class FileLocking {
|
public class FileLocking {
|
||||||
public static void main(String[] args) throws Exception {
|
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();
|
FileLock fl = fos.getChannel().tryLock();
|
||||||
if(fl != null) {
|
if(fl != null) {
|
||||||
System.out.println("Locked File");
|
System.out.println("Locked File");
|
||||||
|
@ -15,11 +15,11 @@ public class GZIPcompress {
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
BufferedOutputStream out;
|
BufferedOutputStream out;
|
||||||
try (InputStream in = new BufferedInputStream(
|
try(InputStream in = new BufferedInputStream(
|
||||||
new FileInputStream(args[0]))) {
|
new FileInputStream(args[0]))) {
|
||||||
out = new BufferedOutputStream(
|
out = new BufferedOutputStream(
|
||||||
new GZIPOutputStream(
|
new GZIPOutputStream(
|
||||||
new FileOutputStream("test.gz")));
|
new FileOutputStream("test.gz")));
|
||||||
System.out.println("Writing file");
|
System.out.println("Writing file");
|
||||||
int c;
|
int c;
|
||||||
while((c = in.read()) != -1)
|
while((c = in.read()) != -1)
|
||||||
|
@ -22,8 +22,8 @@ public class Logon implements Serializable {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Logon a = new Logon("Hulk", "myLittlePony");
|
Logon a = new Logon("Hulk", "myLittlePony");
|
||||||
print("logon a = " + a);
|
print("logon a = " + a);
|
||||||
try (ObjectOutputStream o = new ObjectOutputStream(
|
try(ObjectOutputStream o = new ObjectOutputStream(
|
||||||
new FileOutputStream("Logon.out"))) {
|
new FileOutputStream("Logon.out"))) {
|
||||||
o.writeObject(a);
|
o.writeObject(a);
|
||||||
}
|
}
|
||||||
TimeUnit.SECONDS.sleep(1); // Delay
|
TimeUnit.SECONDS.sleep(1); // Delay
|
||||||
|
@ -27,9 +27,10 @@ public class MappedIO {
|
|||||||
new Tester("Stream Write") {
|
new Tester("Stream Write") {
|
||||||
@Override
|
@Override
|
||||||
public void test() throws IOException {
|
public void test() throws IOException {
|
||||||
try (DataOutputStream dos = new DataOutputStream(
|
try(DataOutputStream dos = new DataOutputStream(
|
||||||
new BufferedOutputStream(
|
new BufferedOutputStream(
|
||||||
new FileOutputStream(new File("temp.tmp"))))) {
|
new FileOutputStream(
|
||||||
|
new File("temp.tmp"))))) {
|
||||||
for(int i = 0; i < numOfInts; i++)
|
for(int i = 0; i < numOfInts; i++)
|
||||||
dos.writeInt(i);
|
dos.writeInt(i);
|
||||||
}
|
}
|
||||||
@ -38,11 +39,12 @@ public class MappedIO {
|
|||||||
new Tester("Mapped Write") {
|
new Tester("Mapped Write") {
|
||||||
@Override
|
@Override
|
||||||
public void test() throws IOException {
|
public void test() throws IOException {
|
||||||
try (FileChannel fc = new RandomAccessFile("temp.tmp", "rw")
|
try(FileChannel fc =
|
||||||
.getChannel()) {
|
new RandomAccessFile("temp.tmp", "rw")
|
||||||
|
.getChannel()) {
|
||||||
IntBuffer ib = fc.map(
|
IntBuffer ib = fc.map(
|
||||||
FileChannel.MapMode.READ_WRITE, 0, fc.size())
|
FileChannel.MapMode.READ_WRITE, 0, fc.size())
|
||||||
.asIntBuffer();
|
.asIntBuffer();
|
||||||
for(int i = 0; i < numOfInts; i++)
|
for(int i = 0; i < numOfInts; i++)
|
||||||
ib.put(i);
|
ib.put(i);
|
||||||
}
|
}
|
||||||
@ -51,9 +53,9 @@ public class MappedIO {
|
|||||||
new Tester("Stream Read") {
|
new Tester("Stream Read") {
|
||||||
@Override
|
@Override
|
||||||
public void test() throws IOException {
|
public void test() throws IOException {
|
||||||
try (DataInputStream dis = new DataInputStream(
|
try(DataInputStream dis = new DataInputStream(
|
||||||
new BufferedInputStream(
|
new BufferedInputStream(
|
||||||
new FileInputStream("temp.tmp")))) {
|
new FileInputStream("temp.tmp")))) {
|
||||||
for(int i = 0; i < numOfInts; i++)
|
for(int i = 0; i < numOfInts; i++)
|
||||||
dis.readInt();
|
dis.readInt();
|
||||||
}
|
}
|
||||||
@ -62,11 +64,11 @@ public class MappedIO {
|
|||||||
new Tester("Mapped Read") {
|
new Tester("Mapped Read") {
|
||||||
@Override
|
@Override
|
||||||
public void test() throws IOException {
|
public void test() throws IOException {
|
||||||
try (FileChannel fc = new FileInputStream(
|
try(FileChannel fc = new FileInputStream(
|
||||||
new File("temp.tmp")).getChannel()) {
|
new File("temp.tmp")).getChannel()) {
|
||||||
IntBuffer ib = fc.map(
|
IntBuffer ib = fc.map(
|
||||||
FileChannel.MapMode.READ_ONLY, 0, fc.size())
|
FileChannel.MapMode.READ_ONLY, 0, fc.size())
|
||||||
.asIntBuffer();
|
.asIntBuffer();
|
||||||
while(ib.hasRemaining())
|
while(ib.hasRemaining())
|
||||||
ib.get();
|
ib.get();
|
||||||
}
|
}
|
||||||
@ -75,8 +77,8 @@ public class MappedIO {
|
|||||||
new Tester("Stream Read/Write") {
|
new Tester("Stream Read/Write") {
|
||||||
@Override
|
@Override
|
||||||
public void test() throws IOException {
|
public void test() throws IOException {
|
||||||
try (RandomAccessFile raf = new RandomAccessFile(
|
try(RandomAccessFile raf = new RandomAccessFile(
|
||||||
new File("temp.tmp"), "rw")) {
|
new File("temp.tmp"), "rw")) {
|
||||||
raf.writeInt(1);
|
raf.writeInt(1);
|
||||||
for(int i = 0; i < numOfUbuffInts; i++) {
|
for(int i = 0; i < numOfUbuffInts; i++) {
|
||||||
raf.seek(raf.length() - 4);
|
raf.seek(raf.length() - 4);
|
||||||
@ -88,11 +90,11 @@ public class MappedIO {
|
|||||||
new Tester("Mapped Read/Write") {
|
new Tester("Mapped Read/Write") {
|
||||||
@Override
|
@Override
|
||||||
public void test() throws IOException {
|
public void test() throws IOException {
|
||||||
try (FileChannel fc = new RandomAccessFile(
|
try(FileChannel fc = new RandomAccessFile(
|
||||||
new File("temp.tmp"), "rw").getChannel()) {
|
new File("temp.tmp"), "rw").getChannel()) {
|
||||||
IntBuffer ib = fc.map(
|
IntBuffer ib = fc.map(
|
||||||
FileChannel.MapMode.READ_WRITE, 0, fc.size())
|
FileChannel.MapMode.READ_WRITE, 0, fc.size())
|
||||||
.asIntBuffer();
|
.asIntBuffer();
|
||||||
ib.put(0);
|
ib.put(0);
|
||||||
for(int i = 1; i < numOfUbuffInts; i++)
|
for(int i = 1; i < numOfUbuffInts; i++)
|
||||||
ib.put(ib.get(i - 1));
|
ib.put(ib.get(i - 1));
|
||||||
|
@ -9,9 +9,9 @@ public class Redirecting {
|
|||||||
PrintStream console = System.out;
|
PrintStream console = System.out;
|
||||||
BufferedInputStream in = new BufferedInputStream(
|
BufferedInputStream in = new BufferedInputStream(
|
||||||
new FileInputStream("Redirecting.java"));
|
new FileInputStream("Redirecting.java"));
|
||||||
try (PrintStream out = new PrintStream(
|
try(PrintStream out = new PrintStream(
|
||||||
new BufferedOutputStream(
|
new BufferedOutputStream(
|
||||||
new FileOutputStream("test.out")))) {
|
new FileOutputStream("test.out")))) {
|
||||||
System.setIn(in);
|
System.setIn(in);
|
||||||
System.setOut(out);
|
System.setOut(out);
|
||||||
System.setErr(out);
|
System.setErr(out);
|
||||||
|
@ -5,9 +5,9 @@ import java.io.*;
|
|||||||
public class StoringAndRecoveringData {
|
public class StoringAndRecoveringData {
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
try (DataOutputStream out = new DataOutputStream(
|
try(DataOutputStream out = new DataOutputStream(
|
||||||
new BufferedOutputStream(
|
new BufferedOutputStream(
|
||||||
new FileOutputStream("Data.txt")))) {
|
new FileOutputStream("Data.txt")))) {
|
||||||
out.writeDouble(3.14159);
|
out.writeDouble(3.14159);
|
||||||
out.writeUTF("That was pi");
|
out.writeUTF("That was pi");
|
||||||
out.writeDouble(1.41413);
|
out.writeDouble(1.41413);
|
||||||
|
@ -5,7 +5,8 @@ import java.io.*;
|
|||||||
public class UsingRandomAccessFile {
|
public class UsingRandomAccessFile {
|
||||||
static String file = "rtest.dat";
|
static String file = "rtest.dat";
|
||||||
static void display() throws IOException {
|
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++)
|
for(int i = 0; i < 7; i++)
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"Value " + i + ": " + rf.readDouble());
|
"Value " + i + ": " + rf.readDouble());
|
||||||
|
@ -47,8 +47,8 @@ public class Worm implements Serializable {
|
|||||||
throws ClassNotFoundException, IOException {
|
throws ClassNotFoundException, IOException {
|
||||||
Worm w = new Worm(6, 'a');
|
Worm w = new Worm(6, 'a');
|
||||||
print("w = " + w);
|
print("w = " + w);
|
||||||
try (ObjectOutputStream out = new ObjectOutputStream(
|
try(ObjectOutputStream out = new ObjectOutputStream(
|
||||||
new FileOutputStream("worm.out"))) {
|
new FileOutputStream("worm.out"))) {
|
||||||
out.writeObject("Worm storage\n");
|
out.writeObject("Worm storage\n");
|
||||||
out.writeObject(w);
|
out.writeObject(w);
|
||||||
out.close(); // Also flushes output
|
out.close(); // Also flushes output
|
||||||
|
@ -15,7 +15,8 @@ public class ZipCompress {
|
|||||||
CheckedOutputStream csum =
|
CheckedOutputStream csum =
|
||||||
new CheckedOutputStream(f, new Adler32());
|
new CheckedOutputStream(f, new Adler32());
|
||||||
ZipOutputStream zos = new ZipOutputStream(csum);
|
ZipOutputStream zos = new ZipOutputStream(csum);
|
||||||
try (BufferedOutputStream out = new BufferedOutputStream(zos)) {
|
try(BufferedOutputStream out =
|
||||||
|
new BufferedOutputStream(zos)) {
|
||||||
zos.setComment("A test of Java Zipping");
|
zos.setComment("A test of Java Zipping");
|
||||||
// No corresponding getComment(), though.
|
// No corresponding getComment(), though.
|
||||||
for(String arg : args) {
|
for(String arg : args) {
|
||||||
@ -48,7 +49,7 @@ public class ZipCompress {
|
|||||||
System.out.write(x);
|
System.out.write(x);
|
||||||
}
|
}
|
||||||
if(args.length == 1)
|
if(args.length == 1)
|
||||||
print("Checksum: " + csumi.getChecksum().getValue());
|
print("Checksum: "+csumi.getChecksum().getValue());
|
||||||
}
|
}
|
||||||
// Alternative way to open and read Zip files:
|
// Alternative way to open and read Zip files:
|
||||||
ZipFile zf = new ZipFile("test.zip");
|
ZipFile zf = new ZipFile("test.zip");
|
||||||
|
@ -6,8 +6,8 @@ import java.io.*;
|
|||||||
|
|
||||||
public class BinaryFile {
|
public class BinaryFile {
|
||||||
public static byte[] read(File bFile) throws IOException{
|
public static byte[] read(File bFile) throws IOException{
|
||||||
try (BufferedInputStream bf = new BufferedInputStream(
|
try(BufferedInputStream bf = new BufferedInputStream(
|
||||||
new FileInputStream(bFile))) {
|
new FileInputStream(bFile))) {
|
||||||
byte[] data = new byte[bf.available()];
|
byte[] data = new byte[bf.available()];
|
||||||
bf.read(data);
|
bf.read(data);
|
||||||
return data;
|
return data;
|
||||||
|
@ -11,7 +11,8 @@ public class TextFile extends ArrayList<String> {
|
|||||||
public static String read(String fileName) {
|
public static String read(String fileName) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
try {
|
try {
|
||||||
try (BufferedReader in = new BufferedReader(new FileReader(
|
try(BufferedReader in =
|
||||||
|
new BufferedReader(new FileReader(
|
||||||
new File(fileName).getAbsoluteFile()))) {
|
new File(fileName).getAbsoluteFile()))) {
|
||||||
String s;
|
String s;
|
||||||
while((s = in.readLine()) != null) {
|
while((s = in.readLine()) != null) {
|
||||||
@ -27,8 +28,8 @@ public class TextFile extends ArrayList<String> {
|
|||||||
// Write a single file in one method call:
|
// Write a single file in one method call:
|
||||||
public static void write(String fileName, String text) {
|
public static void write(String fileName, String text) {
|
||||||
try {
|
try {
|
||||||
try (PrintWriter out = new PrintWriter(
|
try(PrintWriter out = new PrintWriter(
|
||||||
new File(fileName).getAbsoluteFile())) {
|
new File(fileName).getAbsoluteFile())) {
|
||||||
out.print(text);
|
out.print(text);
|
||||||
}
|
}
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
@ -48,8 +49,8 @@ public class TextFile extends ArrayList<String> {
|
|||||||
}
|
}
|
||||||
public void write(String fileName) {
|
public void write(String fileName) {
|
||||||
try {
|
try {
|
||||||
try (PrintWriter out = new PrintWriter(
|
try(PrintWriter out = new PrintWriter(
|
||||||
new File(fileName).getAbsoluteFile())) {
|
new File(fileName).getAbsoluteFile())) {
|
||||||
for(String item : this)
|
for(String item : this)
|
||||||
out.println(item);
|
out.println(item);
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ public class ParseTrash {
|
|||||||
public static <T extends Trash> void
|
public static <T extends Trash> void
|
||||||
fillBin(String filename, Fillable<T> bin) {
|
fillBin(String filename, Fillable<T> bin) {
|
||||||
try {
|
try {
|
||||||
try (BufferedReader data = new BufferedReader(
|
try(BufferedReader data = new BufferedReader(
|
||||||
new FileReader(filename))) {
|
new FileReader(filename))) {
|
||||||
String buf;
|
String buf;
|
||||||
while((buf = data.readLine())!= null) {
|
while((buf = data.readLine())!= null) {
|
||||||
|
@ -57,7 +57,8 @@ public class JUnitDemo {
|
|||||||
// A helper method to reduce code duplication. As long
|
// A helper method to reduce code duplication. As long
|
||||||
// as it isn't annotated with @Test, it will not
|
// as it isn't annotated with @Test, it will not
|
||||||
// be automatically executed by JUnit.
|
// 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();
|
String[] array = (String[])lst.toArray();
|
||||||
assertTrue("Arrays not the same length",
|
assertTrue("Arrays not the same length",
|
||||||
array.length == strs.length);
|
array.length == strs.length);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user