Add java11-features module: runnable demos and captured output for the ankurm.com java11-features article
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import java.nio.file.*;
|
||||
|
||||
/** Files.readString / writeString and Path.of, new in 11. */
|
||||
public class FilesDemo {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Path p = Files.createTempFile("demo", ".txt");
|
||||
Files.writeString(p, "one\ntwo\nthree");
|
||||
String s = Files.readString(p);
|
||||
System.out.println("read back : " + s.replace("\n", "|"));
|
||||
System.out.println("Path.of : " + Path.of("a", "b", "c.txt"));
|
||||
Files.delete(p);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.http.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/** The standard java.net.http client (JEP 321): sync, async, against a local server so the output is stable. */
|
||||
public class HttpClientDemo {
|
||||
public static void main(String[] args) throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/hello", ex -> {
|
||||
byte[] body = "hello from server".getBytes();
|
||||
ex.getResponseHeaders().add("X-Demo", "yes");
|
||||
ex.sendResponseHeaders(200, body.length);
|
||||
ex.getResponseBody().write(body); ex.close();
|
||||
});
|
||||
server.start();
|
||||
String url = "http://127.0.0.1:" + server.getAddress().getPort() + "/hello";
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(url)).GET().build();
|
||||
|
||||
HttpResponse<String> r = client.send(req, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println("sync : " + r.statusCode() + " " + r.body() + " version=" + r.version());
|
||||
System.out.println("header : X-Demo=" + r.headers().firstValue("X-Demo").orElse("?"));
|
||||
|
||||
CompletableFuture<String> f = client.sendAsync(req, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body);
|
||||
System.out.println("async : " + f.get());
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
/** JEP 181: nest-based access. Private members of nest mates no longer need synthetic accessor methods. */
|
||||
public class NestMates {
|
||||
private int secret = 42;
|
||||
static class Inner { int peek(NestMates n) { return n.secret; } }
|
||||
public static void main(String[] args) {
|
||||
System.out.println("nest host : " + Inner.class.getNestHost().getSimpleName());
|
||||
System.out.println("inner can read : " + new Inner().peek(new NestMates()));
|
||||
long synthetic = java.util.Arrays.stream(NestMates.class.getDeclaredMethods()).filter(m -> m.getName().startsWith("access$")).count();
|
||||
System.out.println("access$ methods on outer: " + synthetic);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/** Run directly with: java SingleFile.java (JEP 330, no javac step). */
|
||||
public class SingleFile {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("launched from source, java.version=" + System.getProperty("java.version"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** The String additions in Java 11: isBlank, lines, strip, repeat. */
|
||||
public class StringMethods {
|
||||
public static void main(String[] args) {
|
||||
String em = String.valueOf((char) 0x2003); // EM SPACE: whitespace to strip(), not to trim()
|
||||
String padded = em + " hello " + em;
|
||||
System.out.println("trim() length : " + padded.trim().length());
|
||||
System.out.println("strip() length : " + padded.strip().length());
|
||||
System.out.println("isBlank(\" \") : " + " ".isBlank() + " isEmpty: " + " ".isEmpty());
|
||||
System.out.println("lines() : " + "a\nb\r\nc".lines().collect(Collectors.toList()));
|
||||
System.out.println("repeat(3) : " + "ab".repeat(3));
|
||||
System.out.println("stripLeading : [" + " x ".stripLeading() + "] stripTrailing: [" + " x ".stripTrailing() + "]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/** JEP 323: var in lambda parameters. The point is annotations, not brevity. */
|
||||
public class VarLambda {
|
||||
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
|
||||
@java.lang.annotation.Target(java.lang.annotation.ElementType.PARAMETER)
|
||||
@interface NonNull {}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BiFunction<Integer, Integer, Integer> add = (var a, var b) -> a + b;
|
||||
BiFunction<String, String, String> join = (@NonNull var a, @NonNull var b) -> a + "-" + b;
|
||||
System.out.println("(var a, var b) : " + add.apply(2, 3));
|
||||
System.out.println("with annotation : " + join.apply("x", "y"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user