├── .gitignore ├── Java11 ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── jugistanbul │ │ ├── DynamicConstant.md │ │ ├── FlightRecorder.md │ │ ├── httpclient │ │ ├── HttpClientAsyncGET.java │ │ ├── HttpClientGET.java │ │ ├── HttpClientOtherOptions.java │ │ └── HttpClientPOSTJson.java │ │ ├── lambda │ │ └── VarLambdaParameters.java │ │ ├── nested │ │ └── NestedAccessByReflection.java │ │ ├── singleclass │ │ ├── SingleClass.java │ │ └── SingleCommand.md │ │ └── string │ │ ├── method │ │ └── NewUtilityMethods.java │ │ ├── read │ │ └── ReadString.java │ │ └── write │ │ └── WriteString.java │ └── resources │ └── meetups.txt ├── Java12 ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── jugistanbul │ ├── benchmark │ └── README.md │ └── switchexpressions │ └── SwitchExpressionsExample.java ├── README.md ├── java10 ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── jugistanbul │ │ ├── unmodifiable │ │ └── UnmodifiableView.java │ │ └── var │ │ └── LocalVariableTypeInference.java │ └── resources │ └── log.txt ├── java13 ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── jugistanbul │ ├── socket │ ├── NioSocketImplExample.java │ └── run.sh │ └── text │ └── TextBlocksExample.java ├── java14 ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── jugistanbul │ ├── nullpointer │ ├── Car.java │ ├── Factory.java │ ├── HelpfulNullPointer.java │ └── Model.java │ ├── patternmatching │ ├── Boss.java │ ├── Developer.java │ └── PatternMatchingExample.java │ └── record │ ├── Developer.java │ ├── Person.java │ └── RecordExample.java ├── java9 ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── jugistanbul │ │ ├── constant │ │ └── Constants.java │ │ ├── flow │ │ └── VersionSubscriber.java │ │ ├── inputstream │ │ └── InputStreamFunctions.java │ │ ├── ipm │ │ ├── Language.java │ │ ├── Normalizer.java │ │ └── TextNormalizer.java │ │ ├── process │ │ └── ProcessService.java │ │ ├── stackwalker │ │ └── StackWalker.java │ │ └── stream │ │ └── StreamImprovements.java │ └── resources │ └── lorem.txt └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | out/ 3 | .idea/ 4 | *.iml 5 | *.log 6 | pom.xml.tag 7 | pom.xml.releaseBackup 8 | pom.xml.versionsBackup 9 | pom.xml.next 10 | release.properties 11 | dependency-reduced-pom.xml 12 | buildNumber.properties 13 | .mvn/timing.properties 14 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 15 | .mvn/wrapper/maven-wrapper.jar 16 | -------------------------------------------------------------------------------- /Java11/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java9-n 7 | org.jugistanbul 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | Java11 13 | 14 | 15 | 11 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 3.8.1 24 | 25 | 11 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/DynamicConstant.md: -------------------------------------------------------------------------------- 1 | class AlternativeDynamicConstant { 2 | // constant pool #1 = 10 3 | // constant pool #2 = 20 4 | // constant pool #3 = MethodHandle:Math.max(int,int) 5 | // constant pool #4 = constantdyamic:ConstantBootstraps.invoke/maximum/int.class/#3,#1,#2 6 | final int CONST_A = [constant #1], CONST_B = [constant #2]; 7 | void hello() { 8 | System.out.print([constant #4]); 9 | } 10 | } -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/FlightRecorder.md: -------------------------------------------------------------------------------- 1 | **Java Flight Recorder** 2 | 3 | - Open Sourced but not free. 4 | - JFR is a tool for collecting diagnostic and profiling data about a running Java application. 5 | 6 | To enable JFR for a JVM, you must add these parameters: 7 | 8 | > -XX:+UnlockCommercialFeatures -XX:+FlightRecorder 9 | 10 | Profiling 11 | 12 | > java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=duration=60s,filename=myrecording.jfr MyApp 13 | > jcmd 5368 JFR.start duration=60s filename=myrecording.jfr 14 | 15 | **JFR. Commands** 16 | 17 | JFR.start 18 | 19 | Start a recording. 20 | 21 | JFR.check 22 | 23 | Check the status of all recordings running for the specified process, including the recording identification number, file name, duration, and so on. 24 | 25 | JFR.stop 26 | 27 | Stop a recording with a specific identification number (by default, recording 1 is stopped). 28 | 29 | JFR.dump 30 | 31 | Dump the data collected so far by the recording with a specific identification number (by default, data from recording 1 is dumped). 32 | 33 | > maxsize=size // size limit 34 | > maxage=age // age limit 35 | > delay=delay // put a delay before collecting is actually started 36 | > compress=true 37 | > -XX:FlightRecorderOptions=defaultrecording=true,dumponexit=true,dumponexitpath=path // Recording on exit 38 | 39 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/httpclient/HttpClientAsyncGET.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.httpclient; 2 | 3 | import java.net.URI; 4 | import java.net.http.HttpClient; 5 | import java.net.http.HttpRequest; 6 | import java.net.http.HttpResponse; 7 | import java.util.concurrent.CompletableFuture; 8 | import java.util.concurrent.ExecutionException; 9 | import java.util.concurrent.TimeUnit; 10 | import java.util.concurrent.TimeoutException; 11 | 12 | public class HttpClientAsyncGET { 13 | private static void sendGET() throws InterruptedException, TimeoutException, ExecutionException { 14 | 15 | HttpClient httpClient = HttpClient.newBuilder() 16 | .version(HttpClient.Version.HTTP_2) 17 | .build(); 18 | 19 | HttpRequest request = HttpRequest.newBuilder() 20 | .GET() 21 | .uri(URI.create("https://httpbin.org/get")) 22 | .setHeader("X-JUG-NAME", "JUG ISTANBUL") // add request header 23 | .build(); 24 | 25 | 26 | CompletableFuture> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); 27 | 28 | String responseBody = response.thenApply(HttpResponse::body).get(15, TimeUnit.SECONDS); 29 | 30 | System.out.println(responseBody); 31 | } 32 | 33 | public static void main(String[] args) throws InterruptedException, TimeoutException, ExecutionException { 34 | sendGET(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/httpclient/HttpClientGET.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.httpclient; 2 | 3 | import java.io.IOException; 4 | import java.net.URI; 5 | import java.net.http.HttpClient; 6 | import java.net.http.HttpHeaders; 7 | import java.net.http.HttpRequest; 8 | import java.net.http.HttpResponse; 9 | 10 | public class HttpClientGET 11 | { 12 | private static void sendGET() throws IOException, InterruptedException { 13 | 14 | HttpClient httpClient = HttpClient.newBuilder() 15 | .version(HttpClient.Version.HTTP_2) 16 | .build(); 17 | 18 | HttpRequest request = HttpRequest.newBuilder() 19 | .GET() 20 | .uri(URI.create("https://httpbin.org/get")) 21 | .setHeader("X-JUG-NAME", "JUG ISTANBUL") // add request header 22 | .build(); 23 | 24 | 25 | HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); 26 | // print response headers 27 | HttpHeaders headers = response.headers(); 28 | headers.map().forEach((k, v) -> System.out.println(k + ":" + v)); 29 | 30 | // print status code 31 | System.out.println(response.statusCode()); 32 | 33 | // print response body 34 | System.out.println(response.body()); 35 | 36 | } 37 | 38 | public static void main(String[] args) throws IOException, InterruptedException { 39 | sendGET(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/httpclient/HttpClientOtherOptions.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.httpclient; 2 | 3 | import java.net.*; 4 | import java.net.http.HttpClient; 5 | import java.time.Duration; 6 | 7 | public class HttpClientOtherOptions 8 | { 9 | private static void sendPost() { 10 | 11 | // Authanticator 12 | HttpClient httpClient1 = HttpClient.newBuilder() 13 | .authenticator(new Authenticator() { 14 | @Override 15 | protected PasswordAuthentication getPasswordAuthentication() { 16 | return new PasswordAuthentication( 17 | "user", 18 | "password".toCharArray()); 19 | } 20 | }) 21 | .build(); 22 | // Connection time out 23 | HttpClient httpClient2 = HttpClient.newBuilder() 24 | .connectTimeout(Duration.ofSeconds(5)) 25 | .build(); 26 | 27 | // Set a proxy 28 | HttpClient httpClient3 = HttpClient.newBuilder() 29 | .proxy(ProxySelector.of(new InetSocketAddress("your-company-proxy.com", 8080))) 30 | .build(); 31 | 32 | } 33 | 34 | public static void main(String[] args) { 35 | sendPost(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/httpclient/HttpClientPOSTJson.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.httpclient; 2 | 3 | import java.io.IOException; 4 | import java.net.URI; 5 | import java.net.http.HttpClient; 6 | import java.net.http.HttpRequest; 7 | import java.net.http.HttpResponse; 8 | 9 | public class HttpClientPOSTJson 10 | { 11 | private static void sendPost() throws IOException, InterruptedException { 12 | 13 | HttpClient httpClient = HttpClient.newBuilder() 14 | .version(HttpClient.Version.HTTP_2) 15 | .build(); 16 | 17 | // json formatted data 18 | String json = new StringBuilder() 19 | .append("{") 20 | .append("\"jug\":\"JUG Istanbul\",") 21 | .append("\"message\":\"Hello Java Lovers\"") 22 | .append("}").toString(); 23 | 24 | // add json header 25 | HttpRequest request = HttpRequest.newBuilder() 26 | .POST(HttpRequest.BodyPublishers.ofString(json)) 27 | .uri(URI.create("https://httpbin.org/post")) 28 | .setHeader("X-JUG-NAME", "JUG ISTANBUL") // add request header 29 | .header("Content-Type", "application/json") 30 | .build(); 31 | 32 | HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); 33 | 34 | // print status code 35 | System.out.println(response.statusCode()); 36 | 37 | // print response body 38 | System.out.println(response.body()); 39 | 40 | } 41 | 42 | public static void main(String[] args) throws IOException, InterruptedException { 43 | sendPost(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/lambda/VarLambdaParameters.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.lambda; 2 | 3 | import java.util.function.Predicate; 4 | import java.util.stream.IntStream; 5 | 6 | /** 7 | * @author hakdogan (hakdogan@kodcu.com) 8 | * Created on 13.04.2020 9 | **/ 10 | 11 | public class VarLambdaParameters 12 | { 13 | public static void main(String[] args){ 14 | 15 | IntStream.of(1, 2, 3, 5, 6, 7, 8, 9, 10) 16 | .filter((var i) -> i % 3 == 0) 17 | .forEach(System.out::println); 18 | 19 | System.out.println(); 20 | 21 | Predicate pApple = (var a) -> a.getWeight() > 50; 22 | 23 | var myApple = new Apple("Green", 100); 24 | var message = pApple.test(myApple) 25 | ?"This apple weighs at least 50 grams" 26 | :"This apple is lighter than 50 grams"; 27 | 28 | System.out.println(message); 29 | 30 | // RESTRICTIONS 31 | // (var s1, s2) -> s1 + s2 //no skipping allowed 32 | // (var s1, String y) -> s1 + y //no mixing allowed 33 | // var s1 -> s1 //not allowed. Need parentheses if you use var in lambda. 34 | } 35 | } 36 | 37 | class Apple 38 | { 39 | private String type; 40 | private int weight; 41 | 42 | public Apple(String type, int weight) { 43 | this.type = type; 44 | this.weight = weight; 45 | } 46 | 47 | public String getType() { 48 | return type; 49 | } 50 | 51 | public void setType(String type) { 52 | this.type = type; 53 | } 54 | 55 | public int getWeight() { 56 | return weight; 57 | } 58 | 59 | public void setWeight(int weight) { 60 | this.weight = weight; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/nested/NestedAccessByReflection.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.nested; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | import java.lang.reflect.Method; 5 | 6 | /** 7 | * @author hakdogan (hakdogan@kodcu.com) 8 | * Created on 14.04.2020 9 | **/ 10 | public class NestedAccessByReflection 11 | { 12 | 13 | public void myPublic() { 14 | System.out.println("ParentClass->myPublic()"); 15 | } 16 | 17 | private void myPrivate() { 18 | System.out.println("ParentClass->myPrivate()"); 19 | } 20 | 21 | public Nested getNested() { 22 | return new Nested(); 23 | } 24 | 25 | public class Nested { 26 | 27 | public void callNested() { 28 | // myPrivate(); 29 | 30 | // { // not works 31 | // try { 32 | // Method privateMethod = NestedAccessByReflection.class 33 | // .getDeclaredMethod("myPrivate"); 34 | // privateMethod.invoke(privateMethod); 35 | // } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { 36 | // e.printStackTrace(); 37 | // } 38 | // } 39 | 40 | { // works 41 | try { 42 | Method publicMethod = Nested.class 43 | .getNestHost() 44 | .getDeclaredMethod("myPublic"); 45 | publicMethod.invoke(NestedAccessByReflection.this); 46 | 47 | Method privateMethod = Nested.class 48 | .getNestHost() 49 | .getDeclaredMethod("myPrivate"); 50 | privateMethod.invoke(NestedAccessByReflection.this); 51 | 52 | } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { 53 | e.printStackTrace(); 54 | } 55 | } 56 | } 57 | } 58 | 59 | public static void main(String[] args) { 60 | var main = new NestedAccessByReflection(); 61 | var nested = main.getNested(); 62 | nested.callNested(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/singleclass/SingleClass.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.singleclass; 2 | 3 | public class SingleClass { 4 | 5 | private static class StaticNestedClass { 6 | public void say() { 7 | System.out.println("Hi, I'm Staitc Nested Class!!!"); 8 | } 9 | } 10 | 11 | public void say() { 12 | System.out.println("Hi, I'm Main Class!!!"); 13 | } 14 | 15 | public static void main(String[] args) { 16 | var nested = new StaticNestedClass(); 17 | var main = new SingleClass(); 18 | main.say(); 19 | nested.say(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/singleclass/SingleCommand.md: -------------------------------------------------------------------------------- 1 | How can I run java file without compile? 2 | 3 | > cd Java11/src/main/java/org/jugistanbul/singleclass 4 | > java SingleClass.java 5 | > Hi, I'm Main Class!!! 6 | Hi, I'm Inner Class!!! 7 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/string/method/NewUtilityMethods.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.string.method; 2 | 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * @author hakdogan (hakdogan@kodcu.com) 7 | * Created on 13.04.2020 8 | **/ 9 | 10 | public class NewUtilityMethods 11 | { 12 | 13 | public static void main(String[] args){ 14 | 15 | final var names = " Altug\nHuseyin\nTaner "; 16 | final var nameList = names.strip().lines().collect(Collectors.toList()); 17 | nameList.forEach(System.out::println); 18 | 19 | System.out.println("\u2000"); 20 | 21 | final var jugPart1 = "JUG\u2000"; 22 | final var jugPart2 = " İstanbul"; 23 | 24 | System.out.println(jugPart1.stripTrailing() + jugPart2.stripLeading()); 25 | 26 | System.out.println("TB" + "M".repeat(2)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/string/read/ReadString.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.string.read; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Paths; 6 | 7 | /** 8 | * @author hakdogan (hakdogan@kodcu.com) 9 | * Created on 11.04.2020 10 | **/ 11 | 12 | public class ReadString 13 | { 14 | public static String readString(final String path) throws IOException { 15 | return Files.readString(Paths.get(path)); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /Java11/src/main/java/org/jugistanbul/string/write/WriteString.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.string.write; 2 | 3 | import org.jugistanbul.string.read.ReadString; 4 | 5 | import java.io.IOException; 6 | import java.nio.file.Files; 7 | import java.nio.file.Paths; 8 | import java.nio.file.StandardOpenOption; 9 | 10 | /** 11 | * @author hakdogan (hakdogan@kodcu.com) 12 | * Created on 17.04.2020 13 | **/ 14 | 15 | public class WriteString 16 | { 17 | private static final String FILE_PATH = "java11/src/main/resources/meetups.txt"; 18 | 19 | public static void main(String[] args) throws IOException { 20 | 21 | System.out.println(ReadString.readString(FILE_PATH)); 22 | System.out.println("****************************\n"); 23 | 24 | var path = Paths.get(FILE_PATH); 25 | Files.writeString(path, "\nHello World !!", StandardOpenOption.APPEND); 26 | 27 | System.out.println(ReadString.readString(FILE_PATH)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Java11/src/main/resources/meetups.txt: -------------------------------------------------------------------------------- 1 | Java 9 to 14 2 | Java 9 3 | Java 10 4 | Java 11 and 12 5 | Java 13 and 14 -------------------------------------------------------------------------------- /Java12/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java9-n 7 | org.jugistanbul 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | Java12 13 | 14 | 15 | 16 | 17 | org.apache.maven.plugins 18 | maven-compiler-plugin 19 | 3.8.1 20 | 21 | 13 22 | 23 | --enable-preview 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Java12/src/main/java/org/jugistanbul/benchmark/README.md: -------------------------------------------------------------------------------- 1 | mvn archetype:generate \ 2 | -DinteractiveMode=false \ 3 | -DarchetypeGroupId=org.openjdk.jmh \ 4 | -DarchetypeArtifactId=jmh-java-benchmark-archetype \ 5 | -DgroupId=org.sample \ 6 | -DartifactId=your-benchmark \ 7 | -Dversion=1.0 8 | 9 | // Read the article : https://rieckpil.de/howto-java-benchmarking-with-jmh-java-microbenchmark-harness/ -------------------------------------------------------------------------------- /Java12/src/main/java/org/jugistanbul/switchexpressions/SwitchExpressionsExample.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.switchexpressions; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | /** 6 | * @author hakdogan (hakdogan@kodcu.com) 7 | * Created on 15.04.2020 8 | **/ 9 | 10 | public class SwitchExpressionsExample 11 | { 12 | public static void main(String[] args){ 13 | 14 | System.out.println(getWorkerDefinition(Worker.JOBWORKER)); 15 | 16 | final var day = LocalDateTime.now().getDayOfWeek().getValue(); 17 | System.out.println("The day type is " + getDayDype(day) + "\n"); 18 | System.out.println("The day type is " + getDayDype(8) + "\n"); 19 | } 20 | 21 | public static String getWorkerDefinition(final Worker worker) { 22 | var workerDefinition = switch (worker) { 23 | case JOBWORKER -> 24 | "JOB Worker. This is the person who hates Mondays, lives for the weekends, and brings donuts in on Friday\n"; 25 | case CAREERWORKER -> "Career Worker. They want career status\n"; 26 | case MISSONWORKER -> "Mission Worker. They are on a mission to change something\n"; 27 | }; 28 | 29 | return workerDefinition; 30 | } 31 | 32 | public static DayType getDayDype(final int day){ 33 | return switch (day) { 34 | case 1, 2, 3, 4, 5 -> DayType.WEEKDAY; 35 | case 6, 7 -> DayType.WEEKEND; 36 | default -> { 37 | System.out.println("Unknown value"); 38 | yield DayType.UNKNOWN; 39 | } 40 | }; 41 | } 42 | } 43 | 44 | enum Worker 45 | { 46 | JOBWORKER, CAREERWORKER, MISSONWORKER 47 | } 48 | 49 | enum DayType 50 | { 51 | WEEKDAY, WEEKEND, UNKNOWN 52 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # java9-N 2 | This repository is created for getting experience on new features coming since Java-9. 3 | 4 | Videos can be found here : 5 | 6 | Java 9 Module API : https://www.youtube.com/watch?v=aY1O4XIgpnw 7 | 8 | What's New in Java 10 : https://www.youtube.com/watch?v=keuTbrHe7Wo 9 | 10 | Java 11 & 12 : https://www.youtube.com/watch?v=eyyxO28Ifxs 11 | 12 | Java 13 & 14 : https://www.youtube.com/watch?v=3K-QMxgDY4I 13 | -------------------------------------------------------------------------------- /java10/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java9-n 7 | org.jugistanbul 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | java10 13 | 14 | 15 | 10 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 24 | ${java.version} 25 | ${java.version} 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /java10/src/main/java/org/jugistanbul/unmodifiable/UnmodifiableView.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.unmodifiable; 2 | 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * @author hakdogan (hakdogan@kodcu.com) 8 | * Created on 4.04.2020 9 | **/ 10 | 11 | public class UnmodifiableView 12 | { 13 | 14 | public static void main(String[] args){ 15 | 16 | var cities = getListCities(); 17 | var unmodifiableCities = Collections.unmodifiableList(cities); 18 | var citiesPopulation = getCitiesPopulationMap(cities); 19 | citiesPopulation.forEach((k, v) -> System.out.println(k + " : " + v)); 20 | System.out.println("\n"); 21 | 22 | //This change will affect unmodifiableCities 23 | cities.set(0, new City("Adana", fetchPopulation("Adana"))); 24 | 25 | try { 26 | unmodifiableCities.forEach(c -> System.out.println(c.getName() + " : " + citiesPopulation.get(c.getName()).intValue())); 27 | } catch (NullPointerException nlp){ 28 | System.out.println("Thrown NullPointerException! \n"); 29 | } 30 | 31 | //With java 10 for the unmodifiable view collections, 32 | //three new methods came in the Collectors class 33 | //nd one method in the List class. 34 | //Collections returned by them methods are not affected by the change in the backing collection. 35 | cities = getListCities(); 36 | unmodifiableCities = List.copyOf(cities); 37 | var newCitiesPopulation = getCitiesPopulationMap(cities); 38 | 39 | cities.set(0, new City("Adana", fetchPopulation("Adana"))); 40 | unmodifiableCities.forEach(c -> 41 | System.out.println(c.getName() + " : " + newCitiesPopulation.get(c.getName()).intValue())); 42 | 43 | System.out.println("\n"); 44 | 45 | var numberList = Arrays.asList(1, 2, 3, 4); 46 | var numberListView = numberList.stream().collect(Collectors.toUnmodifiableList()); 47 | numberList.set(0, 9); 48 | numberListView.forEach(System.out::println); 49 | } 50 | 51 | private static List getListCities(){ 52 | return Arrays.asList(new City("İstanbul", fetchPopulation("İstanbul")), 53 | new City("İzmir", fetchPopulation("İzmir")), new City("Ankara", fetchPopulation("Ankara"))); 54 | } 55 | 56 | private static Map getCitiesPopulationMap(final List cities){ 57 | return cities.stream().collect(Collectors.toMap(City::getName, City::getPopulation)); 58 | } 59 | 60 | private static Integer fetchPopulation(final String city){ 61 | switch (city){ 62 | case "İstanbul": 63 | return 15_520_00; 64 | case "İzmir": 65 | return 4_280_000; 66 | case "Ankara": 67 | return 5_445_000; 68 | default: 69 | return 0; 70 | } 71 | } 72 | } 73 | 74 | class City 75 | { 76 | private String name; 77 | private Integer population; 78 | 79 | public City(String name, Integer population) { 80 | this.name = name; 81 | this.population = population; 82 | } 83 | 84 | public String getName() { 85 | return name; 86 | } 87 | 88 | public void setName(String name) { 89 | this.name = name; 90 | } 91 | 92 | public Integer getPopulation() { 93 | return population; 94 | } 95 | 96 | public void setPopulation(Integer population) { 97 | this.population = population; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /java10/src/main/java/org/jugistanbul/var/LocalVariableTypeInference.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.var; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Paths; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * @author hakdogan (hakdogan@kodcu.com) 11 | * Created on 4.04.2020 12 | **/ 13 | 14 | public class LocalVariableTypeInference 15 | { 16 | public static void main(String[] args) throws IOException { 17 | 18 | var text = "Hello Java 10"; 19 | if(text instanceof String){ 20 | System.out.println("The type is String!"); 21 | } 22 | 23 | System.out.println("\n"); 24 | 25 | var personList = new ArrayList<>(); 26 | personList.add(new Person("Hüseyin", "Akdoğan")); 27 | personList.add(new Person("Taner", "Diler")); 28 | 29 | //In this way, we can't access the person class attributes 30 | //personList.stream().filter(e -> e.getFirstName()... 31 | 32 | var explicitPersonList = List.of(new Person("Hüseyin", "Akdoğan"), new Person("Taner", "Diler")); 33 | explicitPersonList.stream().filter(e -> e.getFirstName().contains("e")).forEach(e -> System.out.println(e.getFirstName())); 34 | 35 | System.out.println("\n"); 36 | 37 | //This is a bad use 38 | var result = Helper.process(); 39 | } 40 | } 41 | 42 | class Person 43 | { 44 | private String firstName; 45 | private String lastName; 46 | 47 | public Person(String firstName, String lastName) { 48 | this.firstName = firstName; 49 | this.lastName = lastName; 50 | } 51 | 52 | public String getFirstName() { 53 | return firstName; 54 | } 55 | 56 | public void setFirstName(String firstName) { 57 | this.firstName = firstName; 58 | } 59 | 60 | public String getLastName() { 61 | return lastName; 62 | } 63 | 64 | public void setLastName(String lastName) { 65 | this.lastName = lastName; 66 | } 67 | } 68 | 69 | class Helper 70 | { 71 | public static long process() throws IOException { 72 | var lines = Files.lines(Paths.get("java10/src/main/resources/log.txt")); 73 | return lines.count(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /java10/src/main/resources/log.txt: -------------------------------------------------------------------------------- 1 | Hello world! -------------------------------------------------------------------------------- /java13/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java9-n 7 | org.jugistanbul 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | java13 13 | 14 | 15 | 13 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 3.8.1 24 | 25 | 13 26 | --enable-preview 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /java13/src/main/java/org/jugistanbul/socket/NioSocketImplExample.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.socket; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.net.ServerSocket; 7 | 8 | /** 9 | * @author hakdogan (hakdogan@kodcu.com) 10 | * Created on 21.04.2020 11 | **/ 12 | 13 | public class NioSocketImplExample 14 | { 15 | public static final byte[] RESPONSE = ( 16 | """ 17 | HTTP/1.1 200 OK 18 | Content-length: 2 19 | 20 | OK 21 | """).getBytes(); 22 | 23 | public static void main(String[] args){ 24 | 25 | try (ServerSocket serverSocket = new ServerSocket(8080, 100)){ 26 | 27 | while(!Thread.currentThread().isInterrupted()){ 28 | final var client = serverSocket.accept(); 29 | try(BufferedReader reader = new BufferedReader( 30 | new InputStreamReader(client.getInputStream()))){ 31 | 32 | var line = reader.readLine(); 33 | while (line != null && !line.isEmpty()) { 34 | line = reader.readLine(); 35 | System.out.println(line); 36 | } 37 | client.getOutputStream().write(RESPONSE); 38 | client.close(); 39 | } 40 | } 41 | 42 | } catch (IOException e) { 43 | e.printStackTrace(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /java13/src/main/java/org/jugistanbul/socket/run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | java -XX:+TraceClassLoading --enable-preview --source 14 NioSocketImplExample.java | grep 'NioSocketImpl' 4 | 5 | #If you want to switch back to the old implementation PlainSocketImpl 6 | # you can use jdk.net.usePlainSocketImpl system property setting 7 | # java -Djdk.net.usePlainSocketImpl -XX:+TraceClassLoading --enable-preview --source 14 NioSocketImplExample.java | grep 'NioSocketImpl' 8 | -------------------------------------------------------------------------------- /java13/src/main/java/org/jugistanbul/text/TextBlocksExample.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.text; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 21.04.2020 6 | **/ 7 | 8 | public class TextBlocksExample 9 | { 10 | public static void main(String[] args){ 11 | 12 | var html = """ 13 | 14 | 15 |

Hello, World

16 | 17 | 18 | """; 19 | 20 | var json = """ 21 | { 22 | "organization":"JUG İstanbul", 23 | "event": "Java 9 to 14", 24 | "session": 4 25 | "name": "Java 13 and 14" 26 | } 27 | """; 28 | 29 | System.out.println(html + "\n" + "*".repeat(20) + "\n\n" + json); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /java14/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | java9-n 7 | org.jugistanbul 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | java14 13 | 14 | 15 | 14 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 3.8.1 24 | 25 | 14 26 | --enable-preview 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/nullpointer/Car.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.nullpointer; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 21.04.2020 6 | **/ 7 | 8 | public class Car 9 | { 10 | private String color; 11 | private Model model; 12 | 13 | public Car(String color, Model model) { 14 | this.color = color; 15 | this.model = model; 16 | } 17 | 18 | public String getColor() { 19 | return color; 20 | } 21 | 22 | public void setColor(String color) { 23 | this.color = color; 24 | } 25 | 26 | public Model getModel() { 27 | return model; 28 | } 29 | 30 | public void setModel(Model model) { 31 | this.model = model; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/nullpointer/Factory.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.nullpointer; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 21.04.2020 6 | **/ 7 | 8 | public class Factory 9 | { 10 | private String name; 11 | 12 | public Factory(String name) { 13 | this.name = name; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/nullpointer/HelpfulNullPointer.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.nullpointer; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 21.04.2020 6 | **/ 7 | 8 | public class HelpfulNullPointer 9 | { 10 | public static void main(String[] args){ 11 | 12 | var model = new Model(); 13 | var car = new Car("White", model); 14 | 15 | System.out.println(car.getModel().getFactory().getName()); 16 | 17 | /* 18 | Before Java 14, we were getting the following message in such this situation 19 | 20 | Exception in thread "main" java.lang.NullPointerException at 21 | org.jugistanbul.nullpointer.HelpfulNullPointer.main(HelpfulNullPointer.java:15) 22 | 23 | With Java 14 Helpful NullPointerExceptions feature, we receive the message below 24 | 25 | Exception in thread "main" java.lang.NullPointerException: 26 | Cannot invoke "org.jugistanbul.nullpointer.Factory.getName()" 27 | because the return value of "org.jugistanbul.nullpointer.Model.getFactory()" is null 28 | at org.jugistanbul.nullpointer.HelpfulNullPointer.main(HelpfulNullPointer.java:15) 29 | 30 | You must use the -XX:+ShowCodeDetailsInExceptionMessages command-line option 31 | to enable detailed exception message feature because it's disabled by default. 32 | */ 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/nullpointer/Model.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.nullpointer; 2 | 3 | import org.jugistanbul.nullpointer.Factory; 4 | 5 | /** 6 | * @author hakdogan (hakdogan@kodcu.com) 7 | * Created on 21.04.2020 8 | **/ 9 | 10 | public class Model 11 | { 12 | private int year; 13 | private Factory factory; 14 | 15 | public Model(){ } 16 | 17 | public Model(int year) { 18 | this.year = year; 19 | } 20 | 21 | public int getYear() { 22 | return year; 23 | } 24 | 25 | public void setYear(int year) { 26 | this.year = year; 27 | } 28 | 29 | public Factory getFactory() { 30 | return factory; 31 | } 32 | 33 | public void setFactory(Factory factory) { 34 | this.factory = factory; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/patternmatching/Boss.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.patternmatching; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 22.04.2020 6 | **/ 7 | 8 | public class Boss 9 | { 10 | public int profit() { 11 | return 50_000; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/patternmatching/Developer.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.patternmatching; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 22.04.2020 6 | **/ 7 | 8 | public class Developer 9 | { 10 | public int salary() { 11 | return 5_000; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/patternmatching/PatternMatchingExample.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.patternmatching; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 22.04.2020 6 | **/ 7 | 8 | public class PatternMatchingExample 9 | { 10 | public static void main(String[] args){ 11 | System.out.println(howMuch(new Developer())); 12 | } 13 | 14 | private static int howMuch(Object o){ 15 | 16 | if(o instanceof Developer dev){ 17 | return dev.salary(); 18 | } else if(o instanceof Boss boss) { 19 | return boss.profit(); 20 | } 21 | 22 | return 0; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/record/Developer.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.record; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 22.04.2020 6 | **/ 7 | 8 | public interface Developer 9 | { 10 | String getFullName(); 11 | } 12 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/record/Person.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.record; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 22.04.2020 6 | **/ 7 | 8 | record Person(String firstName, String lastName) implements Developer { 9 | 10 | public Person(){ 11 | this("", ""); 12 | } 13 | 14 | //Redefining an Accessor 15 | /* 16 | public String firstName(){ 17 | return "The first name is " + firstName; 18 | } 19 | */ 20 | 21 | @Override 22 | public String getFullName() { 23 | return String.join(" ", firstName, lastName); 24 | } 25 | 26 | public String specialMethod(){ 27 | return "This is a special method"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java14/src/main/java/org/jugistanbul/record/RecordExample.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.record; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 22.04.2020 6 | **/ 7 | 8 | public class RecordExample 9 | { 10 | public static void main(String[] args){ 11 | 12 | var huseyin = new Person("Huseyin", "Akdogan"); 13 | var taner = new Person("Taner", "Diler"); 14 | var noname = new Person(); 15 | 16 | if(!huseyin.getFullName().equals(taner.getFullName())){ 17 | System.out.println("Names are different"); 18 | } 19 | 20 | System.out.println(huseyin.getFullName()); 21 | System.out.println(taner.getFullName()); 22 | System.out.println(noname.getFullName()); 23 | 24 | System.out.println(huseyin.toString()); 25 | System.out.println(taner.toString()); 26 | System.out.println(noname.toString()); 27 | 28 | System.out.println(huseyin.hashCode()); 29 | System.out.println(taner.hashCode()); 30 | System.out.println(noname.hashCode()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /java9/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | org.jugistanbul 7 | java9-n 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | java9 13 | 14 | 15 | 9 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 24 | ${java.version} 25 | ${java.version} 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/constant/Constants.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.constant; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author hakdogan (hakdogan@kodcu.com) 10 | * Created on 31.03.2020 11 | **/ 12 | 13 | public class Constants 14 | { 15 | public static Map turkishCharacters = new HashMap<>(); 16 | public static Map> versionsAndFutures = new HashMap<>(); 17 | 18 | static { 19 | 20 | turkishCharacters.put("ç", "c"); 21 | turkishCharacters.put("Ç", "c"); 22 | turkishCharacters.put("ğ", "g"); 23 | turkishCharacters.put("Ğ", "g"); 24 | turkishCharacters.put("ı", "i"); 25 | turkishCharacters.put("ş", "s"); 26 | turkishCharacters.put("Ş", "s"); 27 | turkishCharacters.put("ü", "u"); 28 | turkishCharacters.put("Ü", "u"); 29 | 30 | versionsAndFutures.put("Java 9", Arrays.asList("Modularity", "JShell", "Stream Improvements")); 31 | versionsAndFutures.put("Java 10", Arrays.asList("Local Variable Type Inference", 32 | "Unmodifiable Collection Enhancements", "Container Awareness")); 33 | versionsAndFutures.put("Java 11", Arrays.asList("Running Java File with single command", 34 | "New utility methods in String class", "JEP 321: HTTP Client")); 35 | versionsAndFutures.put("Java 12", Arrays.asList("Switch Expressions(Preview)", "Default CDS Archives", "JVM constants API")); 36 | versionsAndFutures.put("Java 13", Arrays.asList("Reimplement the Legacy Socket API", "Switch Expressions(Preview)", "Text Blocks(Preview)")); 37 | versionsAndFutures.put("Java 14", Arrays.asList(" Pattern Matching for instanceof", "Records", "Helpful NullPointerExceptions")); 38 | versionsAndFutures.put("Java 15", null); 39 | } 40 | private Constants(){} 41 | } 42 | -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/flow/VersionSubscriber.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.flow; 2 | 3 | import static org.jugistanbul.constant.Constants.versionsAndFutures; 4 | 5 | import java.util.Optional; 6 | import java.util.concurrent.Flow.Subscriber; 7 | import java.util.concurrent.Flow.Subscription; 8 | import java.util.concurrent.SubmissionPublisher; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | /** 12 | * @author hakdogan (hakdogan@kodcu.com) 13 | * Created on 31.03.2020 14 | **/ 15 | 16 | public class VersionSubscriber implements Subscriber 17 | { 18 | 19 | private Subscription subscription; 20 | 21 | @Override 22 | public void onSubscribe(Subscription subscription) { 23 | this.subscription = subscription; 24 | subscription.request(1); 25 | System.out.println("Subscription started!"); 26 | } 27 | 28 | @Override 29 | public void onNext(T item) { 30 | System.out.println("Got feature: " + item); 31 | subscription.request(1); 32 | } 33 | 34 | @Override 35 | public void onError(Throwable throwable) { 36 | throwable.printStackTrace(); 37 | } 38 | 39 | @Override 40 | public void onComplete() { 41 | System.out.println("Emission is completed!"); 42 | } 43 | } 44 | 45 | class VersionPublisher 46 | { 47 | 48 | public static void main(String[] args) throws InterruptedException { 49 | 50 | final SubmissionPublisher publisher = new SubmissionPublisher<>(); 51 | final VersionSubscriber subscriber = new VersionSubscriber<>(); 52 | publisher.subscribe(subscriber); 53 | 54 | versionsAndFutures.entrySet().stream().forEach(version -> 55 | Optional.ofNullable(version.getValue()).ifPresent(v -> v.forEach(publisher::submit)) 56 | ); 57 | 58 | TimeUnit.MILLISECONDS.sleep(100); 59 | publisher.close(); 60 | 61 | } 62 | } -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/inputstream/InputStreamFunctions.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.inputstream; 2 | 3 | import java.io.*; 4 | import java.net.URL; 5 | 6 | /** 7 | * Created by taner on 06.04.2017. 8 | */ 9 | public class InputStreamFunctions 10 | { 11 | public static void main(String[] args) throws IOException 12 | { 13 | 14 | URL url = InputStreamFunctions.class.getClassLoader().getResource("lorem.txt"); 15 | String filePath = url.getPath(); 16 | InputStream is = new BufferedInputStream( 17 | new FileInputStream(new File(filePath))); 18 | 19 | // Read all bytes. While doing, blocks until the requested number of bytes have been read, end 20 | // of stream is detected, or an exception is thrown 21 | byte[] allBytes = is.readAllBytes(); 22 | 23 | 24 | 25 | is = new BufferedInputStream( 26 | new FileInputStream(new File(filePath))); 27 | 28 | // Transfer data from inputStream to outputstream 29 | is.transferTo(System.out); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/ipm/Language.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.ipm; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 1.04.2020 6 | **/ 7 | 8 | public enum Language 9 | { 10 | TURKISH, FRENCH 11 | } 12 | -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/ipm/Normalizer.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.ipm; 2 | 3 | import static org.jugistanbul.constant.Constants.turkishCharacters; 4 | /** 5 | * @author hakdogan (hakdogan@kodcu.com) 6 | * Created on 1.04.2020 7 | **/ 8 | 9 | public interface Normalizer 10 | { 11 | default String normalizeByLanguage(final String text, final Language language){ 12 | return language.equals(Language.TURKISH) 13 | ? normalizerByTurkish(text): normalizerByFrench(text); 14 | } 15 | 16 | private String normalizerByTurkish(final String text){ 17 | final StringBuffer buffer = new StringBuffer(); 18 | text.chars().mapToObj(Character::toChars).forEach(c -> { 19 | if(turkishCharacters.get(String.valueOf(c)) != null){ 20 | buffer.append(turkishCharacters.get(String.valueOf(c))); 21 | } else { 22 | buffer.append(String.valueOf(c)); 23 | } 24 | }); 25 | return buffer.toString(); 26 | } 27 | 28 | private String normalizerByFrench(final String text){ 29 | return null; 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/ipm/TextNormalizer.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.ipm; 2 | 3 | /** 4 | * @author hakdogan (hakdogan@kodcu.com) 5 | * Created on 1.04.2020 6 | **/ 7 | 8 | public class TextNormalizer implements Normalizer 9 | { 10 | 11 | public static void main(String[] args){ 12 | String text = "Bu meetup serisinde Java 9'dan başlayarak, 14'e kadar gelen yenilik ve değişimleri ele almayı amaçlıyoruz."; 13 | TextNormalizer textNormalizer = new TextNormalizer(); 14 | System.out.println(textNormalizer.normalizeByLanguage(text, Language.TURKISH)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/process/ProcessService.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.process; 2 | 3 | import java.io.IOException; 4 | import java.time.Duration; 5 | import java.util.Comparator; 6 | import java.util.Optional; 7 | 8 | public class ProcessService 9 | { 10 | public static void sortByDurationAndListAllProcesses() 11 | { 12 | ProcessHandle.allProcesses() 13 | .map(ProcessHandle::info) 14 | .sorted(Comparator.comparing(info -> info.totalCpuDuration().orElse(Duration.ZERO))) 15 | .forEach(info -> info.command() 16 | .ifPresent(command -> info.totalCpuDuration() 17 | .ifPresent(duration -> System.out.println(command + " has been running for " + duration)))); 18 | } 19 | 20 | public static void listOfMyProcesses() 21 | { 22 | final String userName = System.getProperty("user.name"); 23 | ProcessHandle.allProcesses().map(ProcessHandle::info) 24 | .filter(info -> info.user().filter(name -> name.contains(userName)).isPresent()) 25 | .forEach(info -> info.command() 26 | .ifPresent(command -> info.totalCpuDuration() 27 | .ifPresent(duration -> System.out.println(command + " has been running for " + duration)))); 28 | } 29 | 30 | public static void findProcesses(String text) 31 | { 32 | ProcessHandle.allProcesses() 33 | .filter(p -> p.info().command() 34 | .filter(cmd -> cmd.contains(text)).isPresent()) 35 | .forEach(p -> p.info().command() 36 | .ifPresent(command -> System.out.printf("Found Process : %d %s\n", p.pid(), command))); 37 | } 38 | 39 | public static void killProcess(long pid, Runnable doSomething) 40 | { 41 | Optional pho = ProcessHandle.of(pid); 42 | pho.ifPresent(ph -> { 43 | Optional.of(doSomething).ifPresent( act -> ph.onExit().thenRunAsync(act)); 44 | ph.destroyForcibly(); 45 | } 46 | ); 47 | } 48 | 49 | public static Process exec(String command) throws IOException 50 | { 51 | return Runtime.getRuntime().exec(command); 52 | } 53 | 54 | public static void main(String[] args) throws IOException, InterruptedException 55 | { 56 | System.out.println("************ LIST OF ALL PROCESSES ***********"); 57 | ProcessService.sortByDurationAndListAllProcesses(); 58 | 59 | System.out.println("************ LIST OF MY PROCESSES ***********"); 60 | ProcessService.listOfMyProcesses(); 61 | 62 | ProcessService.findProcesses("chrome"); 63 | 64 | Process process = ProcessService.exec("sleep 1h"); 65 | ProcessService.killProcess(process.pid(), () -> System.out.printf("Process{%d} killed!\n", process.pid())); 66 | 67 | Thread.sleep(1000); 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/stackwalker/StackWalker.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.stackwalker; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Optional; 6 | import java.util.stream.Collectors; 7 | import java.lang.StackWalker.*; 8 | 9 | public class StackWalker { 10 | 11 | public void stackWalk(){ 12 | new StackWalker1().stackWalk1(); 13 | } 14 | 15 | public void print(){ 16 | System.out.println("Hello World."); 17 | } 18 | 19 | private class StackWalker1{ 20 | public void stackWalk1(){ 21 | new StackWalker2().stackWalk2(); 22 | } 23 | } 24 | 25 | private class StackWalker2{ 26 | public void stackWalk2(){ 27 | new StackWalker3().stackWalk3(); 28 | } 29 | } 30 | 31 | private class StackWalker3{ 32 | public void stackWalk3(){ 33 | new StackWalker4().stackWalk4(); 34 | } 35 | } 36 | 37 | private static List walkAndFilterStackframe() { 38 | return java.lang.StackWalker.getInstance().walk(s -> 39 | s.map( frame-> frame.getClassName()+"/"+frame.getMethodName()) 40 | //.filter(name -> name.contains("StackWalker")) 41 | .limit(10) 42 | .collect(Collectors.toList())); 43 | } 44 | 45 | private class StackWalker4{ 46 | public void stackWalk4(){ 47 | System.out.println(walkAndFilterStackframe().toString()); 48 | List stack = 49 | java.lang.StackWalker.getInstance(java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE).walk((s) -> s.collect(Collectors.toList())); 50 | 51 | 52 | System.out.println("***** All frames *****"); 53 | 54 | for(StackFrame stackFrame : stack){ 55 | System.out.println(stackFrame.toString()); 56 | } 57 | 58 | System.out.print("\n\n"); 59 | 60 | // Filter for interesting classes 61 | final List interestingClasses = new ArrayList<>(); 62 | interestingClasses.add(StackWalker3.class); 63 | 64 | Optional framesWithInterestingClass = java.lang.StackWalker.getInstance( 65 | java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE).walk((s) -> 66 | s.filter(f -> interestingClasses.contains(f.getDeclaringClass())).findFirst() 67 | ); 68 | 69 | System.out.println("Frame with interseting class : \n"+framesWithInterestingClass.toString()); 70 | 71 | System.out.print("\n\n"); 72 | 73 | // Frames with skip 74 | List framesAfterSkip = java.lang.StackWalker.getInstance(java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE).walk((s) -> 75 | s.skip(2).collect(Collectors.toList()) 76 | ); 77 | System.out.println("Frames after skip"); 78 | for(StackFrame stackFrame : framesAfterSkip){ 79 | System.out.println(stackFrame.toString()); 80 | } 81 | } 82 | } 83 | 84 | public static void main(String args[]){ 85 | System.out.println("StackWalkerExample..."); 86 | new StackWalker().stackWalk(); 87 | } 88 | } -------------------------------------------------------------------------------- /java9/src/main/java/org/jugistanbul/stream/StreamImprovements.java: -------------------------------------------------------------------------------- 1 | package org.jugistanbul.stream; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import java.util.Map; 6 | import java.util.stream.Collectors; 7 | import java.util.stream.IntStream; 8 | import java.util.stream.Stream; 9 | import static org.jugistanbul.constant.Constants.versionsAndFutures; 10 | 11 | /** 12 | * @author hakdogan (hakdogan@kodcu.com) 13 | * Created on 30.03.2020 14 | **/ 15 | 16 | public class StreamImprovements { 17 | 18 | private static final List TURKISH_CHARACTERS = Arrays.asList("ç", "Ç", "ğ", "Ğ", "ı", "İ", "ş", "Ş", "ü", "Ü"); 19 | 20 | public static void main(String[] args) 21 | { 22 | 23 | Stream numbers = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 24 | numbers.takeWhile(n -> n < 6).forEach(System.out::print); 25 | 26 | System.out.println(); 27 | 28 | Stream alphabet = Stream.of("a", "b", "c", "ç", "e", "f", "g"); 29 | alphabet.dropWhile(StreamImprovements::checkTurkishCharacter).forEach(System.out::print); 30 | 31 | System.out.println(); 32 | IntStream.iterate(3, x -> x < 10, x -> x + 3).forEach(r -> System.out.print(r + " ")); 33 | System.out.println("\n"); 34 | 35 | System.out.println("JAVA 8 - Streaming null"); 36 | try { 37 | 38 | versionsAndFutures.entrySet() 39 | .stream() 40 | .flatMap(s -> s.getValue().stream()) 41 | .collect(Collectors.toList()) 42 | .forEach(System.out::println); 43 | 44 | } catch(NullPointerException e) { 45 | System.out.println("Throwed NullPointerException while streaming null"); 46 | } 47 | 48 | System.out.println("JAVA 9 - Streaming null with ofNullable() method"); 49 | versionsAndFutures.entrySet() 50 | .stream() 51 | .flatMap(s -> Stream.ofNullable(s.getValue())) 52 | .collect(Collectors.toList()) 53 | .forEach(System.out::println); 54 | 55 | System.out.println("\n"); 56 | 57 | System.out.println("JAVA 8 - Not support Flat Mapping"); 58 | List owners = List.of(new ListOwner("Taner", List.of(1,2,3,4)), 59 | new ListOwner("Altug", List.of(21,22,23,24)), 60 | new ListOwner("Taner", List.of(31,32,33,34))); 61 | 62 | Map>> ownerPoints = 63 | owners.stream().collect( 64 | Collectors.groupingBy( 65 | ListOwner::getName, 66 | Collectors.mapping( 67 | ListOwner::getPoints, 68 | Collectors.toList()))); 69 | System.out.println(ownerPoints); 70 | 71 | System.out.println("JAVA 9 - Collectors has flatMapping() method"); 72 | Map> ownerPoints2 = 73 | owners.stream().collect( 74 | Collectors.groupingBy( 75 | ListOwner::getName, 76 | Collectors.flatMapping( 77 | owner -> owner.getPoints().stream(), 78 | Collectors.toList()))); 79 | System.out.println(ownerPoints2); 80 | System.out.println("\n"); 81 | 82 | List numberList = List.of(1, 2, 3, 4, 4, 4, 4, 5, 5); 83 | 84 | System.out.println("JAVA 8 - Not support filtering while grouping"); 85 | Map result = numberList.stream() 86 | .filter(val -> val > 3) 87 | .collect(Collectors.groupingBy(i -> i, Collectors.counting())); 88 | 89 | System.out.println(result.toString()); 90 | System.out.println("JAVA 9 - puts entries not matched to filter also in grouping result"); 91 | 92 | result = numberList.stream() 93 | .collect(Collectors.groupingBy(i -> i, 94 | Collectors.filtering(val -> val > 3, Collectors.counting()))); 95 | 96 | System.out.println(result.toString()); 97 | } 98 | 99 | private static boolean checkTurkishCharacter(String character){ 100 | return !TURKISH_CHARACTERS.contains(character); 101 | } 102 | } 103 | 104 | class ListOwner 105 | { 106 | public String name; 107 | public List points; 108 | 109 | public ListOwner(String name, List points) 110 | { 111 | this.name = name; 112 | this.points = points; 113 | } 114 | 115 | public String getName() 116 | { 117 | return name; 118 | } 119 | 120 | public List getPoints(){ 121 | return points; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /java9/src/main/resources/lorem.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus in libero sed purus maximus eleifend. Nullam ut lorem tincidunt, efficitur nisi vitae, iaculis diam. Donec ut felis sit amet tellus efficitur vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut a sollicitudin massa. Pellentesque tempor dui ac arcu molestie tristique. Maecenas sagittis turpis eget libero posuere, nec tristique magna ultricies. Aliquam lobortis leo eu urna tempor dignissim. Ut leo dolor, molestie eget tristique ut, suscipit vitae sem. Duis nec sapien orci. Nam et tortor id sem eleifend condimentum nec vitae ex. -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.jugistanbul 8 | java9-n 9 | pom 10 | 1.0-SNAPSHOT 11 | 12 | 13 | java9 14 | java10 15 | Java11 16 | Java12 17 | java13 18 | java14 19 | 20 | 21 | 22 | 23 | --------------------------------------------------------------------------------