Created
March 21, 2014 23:19
-
-
Save athieriot/9698523 to your computer and use it in GitHub Desktop.
Java7 features Overview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.AsynchronousFileChannel; | |
import java.nio.channels.CompletionHandler; | |
import java.nio.file.*; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.StringJoiner; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.concurrent.Future; | |
import java.util.concurrent.RecursiveTask; | |
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; | |
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; | |
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; | |
public class Java7 { | |
public static Object samples() { | |
/* ************* */ | |
/* Project Coins */ | |
/* ************* */ | |
/* Binary Literals */ | |
return 0b111; | |
/* Underscores in Numeric Literals */ | |
// return 756_454L; | |
/* String in Switch Statement */ | |
// switch ("test") { | |
// case "test": return "true"; | |
// case "no-test": return "false"; | |
// default: return ""; | |
// } | |
/* Automatic Resource Management | |
* Work with the new super class: AutoCloseable | |
* which use implementations of close() */ | |
// try (FileOutputStream write = new FileOutputStream("/tmp/test")) { | |
// write.write("This is a test".getBytes()); | |
// | |
// try (FileInputStream file = new FileInputStream("/tmp/test"); | |
// BufferedReader read = new BufferedReader(new InputStreamReader(file))) { | |
// | |
// return read.readLine(); | |
// } | |
// } catch (IOException e) { | |
// Throwable[] tryWithResourceExceptions = e.getSuppressed(); | |
// | |
// return e.getLocalizedMessage() + " and " + tryWithResourceExceptions.length + " hidden IO exceptions"; | |
// } | |
/* Multi-Catch */ | |
// try { | |
// if (true) { throw new IOException("IO"); } | |
// else { throw new InstantiationError("Instance"); } | |
// | |
// } catch(IOException | InstantiationError e) { | |
// return e.getLocalizedMessage(); | |
// } | |
/* Diamond Operator */ | |
// List<Map<String, List<String>>> testCollection = new ArrayList<>(); | |
// return testCollection.size(); | |
/* ***** */ | |
/* NIO 2 */ | |
/* ***** */ | |
/* Paths / Path / Files / FileSystem (Ability to create Custom like Zip for ex) */ | |
// Path src = Paths.get("/tmp", "test"); | |
// Path dst = Paths.get("/tmp", "test-new"); | |
// //src.normalize | |
// //src.resolve | |
// //src.getTotalSpace | |
// //etc... | |
// | |
// try { | |
// Files.copy(src, dst); | |
// //Files.move(src, dst); | |
// //Files.createSymbolicLink | |
// //Files.createDirectory | |
// //And many more... | |
// | |
// try (FileInputStream file = new FileInputStream("/tmp/test-new"); | |
// BufferedReader read = new BufferedReader(new InputStreamReader(file))) { | |
// | |
// return read.readLine(); | |
// } | |
// } catch (IOException e) { | |
// return e.getLocalizedMessage(); | |
// } | |
/* DirectoryStream */ | |
// Path testDir = Paths.get("/tmp"); | |
// | |
// try (DirectoryStream<Path> dirs = Files.newDirectoryStream(testDir, "test*")) { | |
// StringBuilder sb = new StringBuilder(); | |
// | |
// for (Path dir : dirs) { | |
// sb.append(dir.getFileName()); sb.append(", "); | |
// } | |
// | |
// return sb; | |
// } catch (IOException e) { return e.getLocalizedMessage(); } | |
/* Walking a File Tree */ | |
// Path testDir = Paths.get("/tmp"); | |
// SimpleFileVisitor<Path> pf = new SimpleFileVisitor<Path>() { | |
// @Override | |
// public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { | |
// System.out.println(path.getFileName()); | |
// return super.visitFile(path, attrs); | |
// } | |
// }; | |
// | |
// try { | |
// Files.walkFileTree(testDir, pf); | |
// | |
// return ""; | |
// } catch (IOException e) { return e.getLocalizedMessage(); } | |
/* Watching a directory */ | |
// Path sourcePath = Paths.get("/tmp"); | |
// | |
// try { | |
// WatchService watcher = FileSystems.getDefault().newWatchService(); | |
// | |
// WatchKey key = sourcePath.register( | |
// watcher, | |
// ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY | |
// ); | |
// | |
// System.out.println("Waiting for a file event..."); | |
// watcher.take(); | |
// for (WatchEvent<?> event : key.pollEvents()) { | |
// System.out.println(event.context() + " - " + event.kind()); | |
// } | |
// key.reset(); | |
// key.cancel(); | |
// | |
// return "Waiting is over"; | |
// | |
// } catch (IOException | InterruptedException e) { | |
// return e.getLocalizedMessage(); | |
// } | |
/* AsynchronousFileChannel, AsynchronousSocketChannel */ | |
/* With Future */ | |
// Path path = Paths.get("/tmp/test"); | |
// | |
// try(AsynchronousFileChannel ch = AsynchronousFileChannel.open(path)) { | |
// Future<Integer> future = ch.read(ByteBuffer.allocate(1024), 0); | |
// | |
// while (!future.isDone()) { | |
// System.out.println ("Waiting for completion..."); | |
// Thread.sleep(500); | |
// } | |
// | |
// return future.get(); | |
// | |
// } catch (IOException | ExecutionException | InterruptedException e) { | |
// return e.getLocalizedMessage(); | |
// } | |
/* With Callback (Can't make it work :p) */ | |
// Path path = Paths.get("/tmp/test"); | |
// | |
// try { | |
// AsynchronousFileChannel ch = AsynchronousFileChannel.open(path); | |
// // Use AsynchronousSocketChannel for network connections | |
// ByteBuffer buffer = ByteBuffer.allocate(1024); | |
// | |
// ch.read(buffer, 0, buffer, | |
// new CompletionHandler<Integer, ByteBuffer>() { | |
// public void completed(Integer result, ByteBuffer attachment) { | |
// System.out.println(result); | |
// } | |
// | |
// public void failed(Throwable exception, ByteBuffer attachment) { | |
// System.out.println(exception.getClass().getName()); | |
// } | |
// } | |
// ); | |
// } catch(IOException e) { | |
// return e.getLocalizedMessage(); | |
// } | |
// | |
// return "Waiting for completion..."; | |
/* ********* */ | |
/* Fork Join */ | |
/* ********* */ | |
/* Allow recursive calls in Parallel */ | |
/* To be used by Frameworks */ | |
// int processors = Runtime.getRuntime().availableProcessors(); | |
// | |
// class FibonacciProblem { | |
// public int n; | |
// public FibonacciProblem(int n) { this.n = n; } | |
// public long solve() { return fibonacci(n); } | |
// | |
// private long fibonacci(int n) { | |
// if (n <= 1) return n; | |
// else | |
// return fibonacci(n-1) + fibonacci(n-2); | |
// } | |
// } | |
// | |
// class FibonacciTask extends RecursiveTask<Long> { | |
// | |
// private static final int THRESHOLD = 5; | |
// | |
// private FibonacciProblem problem; | |
// public long result; | |
// | |
// public FibonacciTask(FibonacciProblem problem) { | |
// this.problem = problem; | |
// } | |
// | |
// @Override | |
// public Long compute() { | |
// if (problem.n < THRESHOLD) { // easy problem, don't bother with parallelism | |
// result = problem.solve(); | |
// } | |
// else { | |
// FibonacciTask worker1 = new FibonacciTask(new FibonacciProblem(problem.n-1)); | |
// FibonacciTask worker2 = new FibonacciTask(new FibonacciProblem(problem.n-2)); | |
// worker1.fork(); | |
// result = worker2.compute() + worker1.join(); | |
// | |
// } | |
// return result; | |
// } | |
// | |
// } | |
// | |
// FibonacciTask task = new FibonacciTask(new FibonacciProblem(20)); | |
// | |
// new ForkJoinPool(processors).invoke(task); | |
// | |
// return "Computed Result: " + task.result; | |
/* Very important features that we can't really show | |
* | |
* - Garbage Collector: G1 (Don't ask me) | |
* | |
* - InvokeDynamics: Ease the pain of running a dynamic languages on the JVM | |
* + Generating ByteCode on the fly | |
* + Less tight to the type system | |
*/ | |
/* Other: | |
* - Concurrency: Phasers, make multiple threads wait to a common stopping point | |
* - Concurrency: TransferQueue | |
* | |
* - Unicode 6.0 | |
* - Extensible Currency Code | |
* | |
* - Swing: Nimbus Look & Feel | |
* - Swing: Mixing AWT and Swing | |
* - Swing: Translucent Windows | |
* | |
* - Derby improvements | |
* - XML enhancement | |
* - Applet improvements (sic) | |
* | |
* - Many other stuff (Security, JavaDocs, etc...) | |
*/ | |
} | |
public static void main(String[] args) { | |
System.out.println(samples().toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment