Commit ecf36f46 authored by vikram singh's avatar vikram singh

added specified imports, reallocated the variables to class level and instance...

added specified imports, reallocated the variables to class level and instance variable as per the requirement,Used logger for logging in almost every classes
parent 21bb2eca
java9Features
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA">
<option name="configuration">
<map>
<entry key="checkstyle-version" value="8.29" />
<entry key="copy-libs" value="false" />
<entry key="location-0" value="BUNDLED:(bundled):Sun Checks" />
<entry key="location-1" value="BUNDLED:(bundled):Google Checks" />
<entry key="scan-before-checkin" value="false" />
<entry key="scanscope" value="JavaOnly" />
<entry key="suppress-errors" value="false" />
</map>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="delegatedBuild" value="true" />
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules"> <option name="modules">
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_9" default="false" project-jdk-name="11" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_9" default="false" project-jdk-name="9" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../" vcs="Git" /> <mapping directory="$PROJECT_DIR$/../" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component> </component>
</project> </project>
\ No newline at end of file
This diff is collapsed.
...@@ -12,5 +12,7 @@ repositories { ...@@ -12,5 +12,7 @@ repositories {
} }
dependencies { dependencies {
compileOnly 'org.projectlombok:lombok:1.18.12'
testCompile group: 'junit', name: 'junit', version: '4.12' testCompile group: 'junit', name: 'junit', version: '4.12'
} }
#Fri Apr 03 12:57:31 IST 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
package com.nisum.java9Features.DiamondOperator; package com.nisum.java9Features.DiamondOperator;
import java.util.logging.Logger;
public class MyGenClass<T> { public class MyGenClass<T> {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
T obj; T obj;
public MyGenClass(T obj) public MyGenClass(T obj)
{ {
...@@ -12,6 +16,6 @@ public class MyGenClass<T> { ...@@ -12,6 +16,6 @@ public class MyGenClass<T> {
} }
public void process() public void process()
{ {
System.out.println("Processing obj..."); log.info("Processing obj...");
} }
} }
package com.nisum.java9Features.DiamondOperator; package com.nisum.java9Features.DiamondOperator;
import java.util.logging.Logger;
public class Test { public class Test {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) { public static void main(String[] args) {
MyGenClass<String> c1 = new MyGenClass<String>("Durga") { MyGenClass<String> c1 = new MyGenClass<String>("Durga") {
public void process() { public void process() {
System.out.println("Processing... " + getObj()); log.info("Processing... " + getObj());
} }
}; };
c1.process(); c1.process();
MyGenClass<String> c2 = new MyGenClass<>("Pavan") { MyGenClass<String> c2 = new MyGenClass<>("Pavan") {
public void process() { public void process() {
System.out.println("Processing... " + getObj()); log.info("Processing... " + getObj());
} }
}; };
c2.process(); c2.process();
......
package com.nisum.java9Features.FactoryMethodsForUnmodifiableCollections; package com.nisum.java9Features.FactoryMethodsForUnmodifiableCollections;
import java.util.*;
import java.util.List;
import java.util.logging.Logger;
public class UnmodifiableList { public class UnmodifiableList {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static Employee e1,e2,e3,e5;
public static void main(String[] args) { public static void main(String[] args) {
Employee e1=new Employee(100,"sunny"); e1=new Employee(100,"sunny");
Employee e2=new Employee(101, "Bunny"); e2=new Employee(101, "Bunny");
Employee e3=new Employee(102,"Chinny"); e3=new Employee(102,"Chinny");
// Employee e4=null; // Employee e4=null;
//List<Employee> emplist=List.of(e1,e2,e3,e4); //List<Employee> emplist=List.of(e1,e2,e3,e4);
List<Employee> emplist=List.of(e1,e2,e3); List<Employee> emplist=List.of(e1,e2,e3);
System.out.println(emplist); log.info("{}"+ emplist);
Employee e5=new Employee(103,"Laddu"); e5=new Employee(103,"Laddu");
//emplist.add(e5); // Unsupported Exception //emplist.add(e5); // Unsupported Exception
} }
......
package com.nisum.java9Features.FactoryMethodsForUnmodifiableCollections; package com.nisum.java9Features.FactoryMethodsForUnmodifiableCollections;
import java.sql.*;
import java.util.*;
import java.util.Map;
import java.util.logging.Logger;
import static java.util.Map.entry; import static java.util.Map.entry;
public class UnmodifiableMap { public class UnmodifiableMap {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static Map<String,String> map=Map.of("A","Apple","B","Banana","C","Cat","D","Dog")
,m;
static Map.Entry<String,String> e1,e2,e3;
public static void main(String[] args) { public static void main(String[] args) {
Map<String,String> map=Map.of("A","Apple","B","Banana","C","Cat","D","Dog"); log.info("{}"+map);
System.out.println(map);
/* Map<String,String> map1=Map.of("A","Apple","A","Banana","C","Cat","D","Dog"); /* Map<String,String> map1=Map.of("A","Apple","A","Banana","C","Cat","D","Dog");
System.out.println(map1); // IllegalArgumentException*/ System.out.println(map1); // IllegalArgumentException*/
/*Map<String,String> map2=Map.of("A",null,"B","Banana"); // NullPointerException /*Map<String,String> map2=Map.of("A",null,"B","Banana"); // NullPointerException
System.out.println(map2);*/ System.out.println(map2);*/
Map.Entry<String,String> e1= entry("A","Apple"); e1= entry("A","Apple");
Map.Entry<String,String> e2= entry("B","Banana"); e2= entry("B","Banana");
Map.Entry<String,String> e3= entry("C","Cat"); e3= entry("C","Cat");
Map<String,String> m=Map.ofEntries(e1,e2,e3); m=Map.ofEntries(e1,e2,e3);
System.out.println(m); log.info("{}"+m);
/* Map<String,String> map3=Map.ofEntries(entry(null,"Apple"),entry("B","Banana")); // NullPointerException /* Map<String,String> map3=Map.ofEntries(entry(null,"Apple"),entry("B","Banana")); // NullPointerException
System.out.println(map3);*/ System.out.println(map3);*/
......
package com.nisum.java9Features.FactoryMethodsForUnmodifiableCollections; package com.nisum.java9Features.FactoryMethodsForUnmodifiableCollections;
import java.util.*;
import java.util.Set;
import java.util.logging.Logger;
public class UnmodifiableSet { public class UnmodifiableSet {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static Employee e1,e2,e3,e5;
static Set<Employee> emplist;
public static void main(String[] args) { public static void main(String[] args) {
Employee e1=new Employee(100,"sunny"); e1=new Employee(100,"sunny");
Employee e2=new Employee(101, "Bunny"); e2=new Employee(101, "Bunny");
Employee e3=new Employee(102,"Chinny"); e3=new Employee(102,"Chinny");
// Employee e4=null; // Employee e4=null;
//List<Employee> emplist=List.of(e1,e2,e3,e4); //List<Employee> emplist=List.of(e1,e2,e3,e4);
Set<Employee> emplist=Set.of(e1,e2,e3,e3); emplist=Set.of(e1,e2,e3,e3);
System.out.println(emplist); log.info("{} "+ emplist);
Employee e5=new Employee(103,"Laddu"); Employee e5=new Employee(103,"Laddu");
//emplist.add(e5); // Unsupported Exception //emplist.add(e5); // Unsupported Exception
......
package com.nisum.java9Features.PrivateMethods; package com.nisum.java9Features.PrivateMethods;
import java.util.logging.Logger;
public interface Java9Interf public interface Java9Interf
{ {
final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
default void m1() default void m1()
{ {
m3(); m3();
...@@ -12,6 +16,6 @@ public interface Java9Interf ...@@ -12,6 +16,6 @@ public interface Java9Interf
} }
private void m3() private void m3()
{ {
System.out.println("common functionality of methods m1 & m2"); log.info("common functionality of methods m1 & m2");
} }
} }
package com.nisum.java9Features.PrivateMethods; package com.nisum.java9Features.PrivateMethods;
import java.util.logging.Logger;
public interface Java9InterfStatic { public interface Java9InterfStatic {
final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void m1() public static void m1()
{ {
m3(); m3();
...@@ -11,6 +15,6 @@ public interface Java9InterfStatic { ...@@ -11,6 +15,6 @@ public interface Java9InterfStatic {
} }
private static void m3() private static void m3()
{ {
System.out.println("common functionality of methods m1 & m2"); log.info("common functionality of methods m1 & m2");
} }
} }
package com.nisum.java9Features.ProcessAPI; package com.nisum.java9Features.ProcessAPI;
import java.util.logging.Logger;
public class CurrentProcessInfo { public class CurrentProcessInfo {
public static void main(String[] args) throws Exception { private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
ProcessHandle p = ProcessHandle.current(); static ProcessHandle p;
ProcessHandle.Info info = p.info(); static ProcessHandle.Info info;
System.out.println("Complete Process Inforamtion:\n" + info); public static void main(String[] args) throws Exception {
System.out.println("User: " + info.user().get()); p = ProcessHandle.current();
System.out.println("Command: " + info.command().get()); info = p.info();
System.out.println("Start Time: " + info.startInstant().get()); log.info("Complete Process Inforamtion:\n" + info);
System.out.println("Total CPU Time Acquired: " + info.totalCpuDuration().get()); log.info("User: " + info.user().get());
log.info("Command: " + info.command().get());
log.info("Start Time: " + info.startInstant().get());
log.info("Total CPU Time Acquired: " + info.totalCpuDuration().get());
} }
} }
package com.nisum.java9Features.ProcessAPI; package com.nisum.java9Features.ProcessAPI;
import lombok.extern.slf4j.Slf4j;
import java.util.logging.Logger;
public class DestroyProcessFromProcessBuilder { public class DestroyProcessFromProcessBuilder {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static ProcessBuilder pb;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("java", "sample"); pb= new ProcessBuilder("java", "sample");
Process p = pb.start(); Process p = pb.start();
System.out.println("Process Started with id:" + p.pid()); log.info("Process Started with id:" + p.pid());
Thread.sleep(10000); Thread.sleep(10000);
System.out.println("Destroying the process with id:" + p.pid()); log.info("Destroying the process with id:" + p.pid());
p.destroy(); p.destroy();
} }
......
package com.nisum.java9Features.ProcessAPI; package com.nisum.java9Features.ProcessAPI;
import java.util.*;
import java.util.Optional;
import java.util.logging.Logger;
public class ProcessBasedOnID { public class ProcessBasedOnID {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
//need to get the pid from task manager //need to get the pid from task manager
Optional<ProcessHandle> opt = ProcessHandle.of(8408); Optional<ProcessHandle> opt = ProcessHandle.of(8408);
ProcessHandle p = opt.get(); ProcessHandle p = opt.get();
ProcessHandle.Info info = p.info(); ProcessHandle.Info info = p.info();
System.out.println("Complete Process Inforamtion:\n" + info); log.info("Complete Process Inforamtion:\n" + info);
System.out.println("User: " + info.user().get()); log.info("User: " + info.user().get());
System.out.println("Command: " + info.command().get()); log.info("Command: " + info.command().get());
System.out.println("Start Time: " + info.startInstant().get()); log.info("Start Time: " + info.startInstant().get());
System.out.println("Total CPU Time Acquired: " + info.totalCpuDuration().get()); log.info("Total CPU Time Acquired: " + info.totalCpuDuration().get());
} }
} }
package com.nisum.java9Features.ProcessAPI; package com.nisum.java9Features.ProcessAPI;
import java.util.logging.Logger;
public class ProcessID { public class ProcessID {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ProcessHandle p = ProcessHandle.current(); ProcessHandle p = ProcessHandle.current();
long pid = p.pid(); long pid = p.pid();
System.out.println("The PID of current running JVM instance :" + pid); log.info("The PID of current running JVM instance :" + pid);
Thread.sleep(100000); Thread.sleep(100000);
} }
} }
package com.nisum.java9Features.ReactiveStreams; package com.nisum.java9Features.ReactiveStreams;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
public class EmpHelper { public class EmpHelper {
public static List<Employee> getEmps() { public static List<Employee> getEmps() {
......
package com.nisum.java9Features.ReactiveStreams; package com.nisum.java9Features.ReactiveStreams;
import java.util.*;
import java.util.concurrent.*; import java.util.List;
import java.util.concurrent.SubmissionPublisher;
import java.util.logging.Logger;
public class MyReactiveApp { public class MyReactiveApp {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String args[]) throws InterruptedException { public static void main(String args[]) throws InterruptedException {
// Create Publisher // Create Publisher
SubmissionPublisher<Employee> publisher = new SubmissionPublisher<>(); SubmissionPublisher<Employee> publisher = new SubmissionPublisher<>();
...@@ -15,7 +19,7 @@ public class MyReactiveApp { ...@@ -15,7 +19,7 @@ public class MyReactiveApp {
List<Employee> emps = EmpHelper.getEmps(); List<Employee> emps = EmpHelper.getEmps();
// Publish items // Publish items
System.out.println("Publishing Items to Subscriber"); log.info("Publishing Items to Subscriber");
emps.stream().forEach(i -> publisher.submit(i)); emps.stream().forEach(i -> publisher.submit(i));
// logic to wait till processing of all messages are over // logic to wait till processing of all messages are over
...@@ -25,7 +29,7 @@ public class MyReactiveApp { ...@@ -25,7 +29,7 @@ public class MyReactiveApp {
// close the Publisher // close the Publisher
publisher.close(); publisher.close();
System.out.println("Exiting the app"); log.info("Exiting the app");
} }
} }
package com.nisum.java9Features.ReactiveStreams; package com.nisum.java9Features.ReactiveStreams;
import java.util.concurrent.*;
import java.util.concurrent.Flow.Subscriber; import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription; import java.util.concurrent.Flow.Subscription;
import java.util.logging.Logger;
public class MySubscriber implements Subscriber<Employee> { public class MySubscriber implements Subscriber<Employee> {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private Subscription subscription; private Subscription subscription;
private int counter = 0; private int counter = 0;
@Override @Override
public void onSubscribe(Subscription subscription) { public void onSubscribe(Subscription subscription) {
System.out.println("Subscribed"); log.info("Subscribed");
this.subscription = subscription; this.subscription = subscription;
this.subscription.request(1); //requesting data from publisher this.subscription.request(1); //requesting data from publisher
System.out.println("onSubscribe requested 1 item"); log.info("onSubscribe requested 1 item");
} }
@Override @Override
public void onNext(Employee item) { public void onNext(Employee item) {
System.out.println("Processing Employee " + item); log.info("Processing Employee " + item);
counter++; counter++;
this.subscription.request(1); this.subscription.request(1);
} }
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
System.out.println("Some error happened"); log.info("Some error happened");
e.printStackTrace(); e.printStackTrace();
} }
@Override @Override
public void onComplete() { public void onComplete() {
System.out.println("All Processing Done"); log.info("All Processing Done");
} }
public int getCounter() { public int getCounter() {
......
package com.nisum.java9Features.StreamApi; package com.nisum.java9Features.StreamApi;
import java.util.*;
import java.util.stream.*; import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class TestDropWhile { public class TestDropWhile {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Integer> l1 = new ArrayList<Integer>(); ArrayList<Integer> l1 = new ArrayList<Integer>();
l1.add(2); l1.add(2);
...@@ -13,9 +19,8 @@ public class TestDropWhile { ...@@ -13,9 +19,8 @@ public class TestDropWhile {
l1.add(6); l1.add(6);
l1.add(5); l1.add(5);
l1.add(8); l1.add(8);
System.out.println("Initial List:" + l1); log.info("Initial List:" + l1);
List<Integer> l2 = l1.stream().dropWhile(i -> i % 2 == 0).collect(Collectors.toList()); List<Integer> l2 = l1.stream().dropWhile(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println("After dropWhile:" + l2); log.info("After dropWhile:" + l2);
} }
} }
package com.nisum.java9Features.StreamApi; package com.nisum.java9Features.StreamApi;
import java.util.stream.*;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class TestIterate { public class TestIterate {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) { public static void main(String[] args) {
//Stream.iterate(1, x->x+1).forEach(System.out::println); // return infinite elements //Stream.iterate(1, x->x+1).forEach(System.out::println); // return infinite elements
Stream.iterate(1, x->x+1).limit(5).forEach(System.out::println); Stream.iterate(1, x->x+1).limit(5).forEach(System.out::println);
System.out.println("3argument iterate"); log.info("3argument iterate");
Stream.iterate(1,x->x<10, x->x+1).forEach(System.out::println); Stream.iterate(1,x->x<10, x->x+1).forEach(System.out::println);
} }
} }
package com.nisum.java9Features.StreamApi; package com.nisum.java9Features.StreamApi;
import java.util.*;
import java.util.stream.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestOfNullable { public class TestOfNullable {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) { public static void main(String[] args) {
List<String> l = new ArrayList<String>(); List<String> l = new ArrayList<String>();
l.add("A"); l.add("A");
...@@ -12,12 +20,12 @@ public class TestOfNullable { ...@@ -12,12 +20,12 @@ public class TestOfNullable {
l.add("C"); l.add("C");
l.add("D"); l.add("D");
l.add(null); l.add(null);
System.out.println(l); log.info("l {}"+l);
List<String> l2 = l.stream().filter(o -> o != null).collect(Collectors.toList()); List<String> l2 = l.stream().filter(o -> o != null).collect(Collectors.toList());
System.out.println(l2); log.info("l2 {}"+l2);
List<String> l3 = l.stream().flatMap(o -> Stream.ofNullable(o)).collect(Collectors.toList()); List<String> l3 = l.stream().flatMap(o -> Stream.ofNullable(o)).collect(Collectors.toList());
System.out.println(l3); log.info("l3 {}"+l3);
Map<String, String> m = new HashMap<>(); Map<String, String> m = new HashMap<>();
m.put("A", "Apple"); m.put("A", "Apple");
...@@ -26,9 +34,9 @@ public class TestOfNullable { ...@@ -26,9 +34,9 @@ public class TestOfNullable {
m.put("D", "Dog"); m.put("D", "Dog");
m.put("E", null); m.put("E", null);
List<String> l4 = m.entrySet().stream().map(e -> e.getKey()).collect(Collectors.toList()); List<String> l4 = m.entrySet().stream().map(e -> e.getKey()).collect(Collectors.toList());
System.out.println(l4); log.info("l4 {}"+l4);
List<String> l5 = m.entrySet().stream().flatMap(e -> Stream.ofNullable(e.getValue())).collect(Collectors.toList()); List<String> l5 = m.entrySet().stream().flatMap(e -> Stream.ofNullable(e.getValue())).collect(Collectors.toList());
System.out.println(l5); log.info("l5 {}"+l5);
} }
} }
package com.nisum.java9Features.StreamApi; package com.nisum.java9Features.StreamApi;
import java.util.*; import java.util.ArrayList;
import java.util.stream.*;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class TestTakeWhile { public class TestTakeWhile {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Integer> l1 = new ArrayList<Integer>(); ArrayList<Integer> l1 = new ArrayList<Integer>();
l1.add(2); l1.add(2);
l1.add(4); l1.add(4);
...@@ -13,10 +21,10 @@ public class TestTakeWhile { ...@@ -13,10 +21,10 @@ public class TestTakeWhile {
l1.add(6); l1.add(6);
l1.add(5); l1.add(5);
l1.add(8); l1.add(8);
System.out.println("Initial List:" + l1); log.info("Initial List:" + l1);
List<Integer> l2 = l1.stream().filter(i -> i % 2 == 0).collect(Collectors.toList()); List<Integer> l2 = l1.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println("After Filtering:" + l2); log.info("After Filtering:" + l2);
List<Integer> l3 = l1.stream().takeWhile(i -> i % 2 == 0).collect(Collectors.toList()); List<Integer> l3 = l1.stream().takeWhile(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println("After takeWhile:" + l3); log.info("After takeWhile:" + l3);
} }
} }
package com.nisum.java9Features.TryWithResourcesEnhancements; package com.nisum.java9Features.TryWithResourcesEnhancements;
import java.util.logging.Logger;
public class MyResources implements AutoCloseable { public class MyResources implements AutoCloseable {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
MyResources() MyResources()
{ {
System.out.println("Resource Creation..."); log.info("Resource Creation...");
} }
public void doProcess() public void doProcess()
{ {
System.out.println("Resource Processing..."); log.info("Resource Processing...");
} }
public void close() public void close()
{ {
System.out.println("Resource Closing..."); log.info("Resource Closing...");
} }
} }
package com.nisum.java9Features.TryWithResourcesEnhancements; package com.nisum.java9Features.TryWithResourcesEnhancements;
import lombok.extern.flogger.Flogger;
import lombok.extern.slf4j.Slf4j;
import java.util.logging.Logger;
//@Slf4j
public class TryWithResourceApp { public class TryWithResourceApp {
private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void preJDK7() public static void preJDK7()
{ {
MyResources r=null; MyResources r=null;
...@@ -11,7 +18,7 @@ public class TryWithResourceApp { ...@@ -11,7 +18,7 @@ public class TryWithResourceApp {
} }
catch (Exception e) catch (Exception e)
{ {
System.out.println("Handling:"+e); log.info("Handling:"+e);
} }
finally finally
{ {
...@@ -24,7 +31,7 @@ public class TryWithResourceApp { ...@@ -24,7 +31,7 @@ public class TryWithResourceApp {
} }
catch (Exception e) catch (Exception e)
{ {
System.out.println("Handling:"+e); log.info("Handling:"+e);
} }
} }
} }
...@@ -36,7 +43,7 @@ public class TryWithResourceApp { ...@@ -36,7 +43,7 @@ public class TryWithResourceApp {
} }
catch(Exception e) catch(Exception e)
{ {
System.out.println("Handling:"+e); log.info("Handling:"+e);
} }
} }
public static void JDK9() public static void JDK9()
...@@ -48,7 +55,7 @@ public class TryWithResourceApp { ...@@ -48,7 +55,7 @@ public class TryWithResourceApp {
} }
catch(Exception e) catch(Exception e)
{ {
System.out.println("Handling:"+e); log.info("Handling:"+e);
} }
} }
public static void multipleJDK9() public static void multipleJDK9()
...@@ -66,21 +73,23 @@ public class TryWithResourceApp { ...@@ -66,21 +73,23 @@ public class TryWithResourceApp {
} }
catch(Exception e) catch(Exception e)
{ {
System.out.println("Handling:"+e); log.info("Handling:"+e);
} }
} }
public static void main(String[] args) public static void main(String[] args)
{ {
System.out.println("Program Execution With PreJDK7");
preJDK7();
System.out.println("Program Execution With JDK7"); log.info("Program Execution With PreJDK7");
JDK7();
preJDK7();
log.info("Program Execution With JDK7");
JDK7();
System.out.println("Program Execution With JDK9"); log.info("Program Execution With JDK9");
JDK9(); JDK9();
System.out.println("Program Execution Multiple Resources With JDK9"); log.info("Program Execution Multiple Resources With JDK9");
multipleJDK9(); multipleJDK9();
} }
} }
package com.nisum.java9Features.http2; //package com.nisum.java9Features.http2;
//
import com.nisum.java9Features.DiamondOperator.*; //import jdk.incubator.http.HttpClient;
//import jdk.incubator.http.HttpHeaders;
import java.net.*; //import jdk.incubator.http.HttpRequest;
import java.net.http.*; //import jdk.incubator.http.HttpResponse;
import java.util.*; //
import java.util.concurrent.*; //import java.net.URI;
//import java.util.List;
public class TestAsync { //import java.util.Map;
public static void main(String[] args) throws Exception { //import java.util.concurrent.CompletableFuture;
String url = "https://www.redbus.in/info/aboutus"; //import java.util.logging.Logger;
sendGetAsyncRequest(url); //
} //
//public class TestAsync {
public static void sendGetAsyncRequest(String url) throws Exception { // private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
HttpClient client = HttpClient.newHttpClient(); //
HttpRequest req = HttpRequest.newBuilder(new URI(url)).GET().build(); // public static void main(String[] args) throws Exception {
System.out.println("Sending Asynchronous Request..."); // String url = "https://www.redbus.in/info/aboutus";
CompletableFuture<HttpResponse<String>> cf = client.sendAsync(req, HttpResponse.BodyHandlers.ofString()); // sendGetAsyncRequest(url);
int count = 0; // }
while (!cf.isDone()) { //
System.out.println("Processing not done and doing other activity:" + ++count); // public static void sendGetAsyncRequest(String url) throws Exception {
} // HttpClient client = HttpClient.newHttpClient();
processResponse(cf.get()); // HttpRequest req = HttpRequest.newBuilder(new URI(url)).GET().build();
} // log.info("Sending Asynchronous Request...");
// CompletableFuture<HttpResponse<String>> cf = client.sendAsync(req, HttpResponse.BodyHandler.asString());
public static void processResponse(HttpResponse resp) { // int count = 0;
System.out.println("Status Code:" + resp.statusCode()); // while (!cf.isDone()) {
//System.out.println("Response Body:"+resp.body()); // log.info("Processing not done and doing other activity:" + ++count);
HttpHeaders header = resp.headers(); // }
Map<String, List<String>> map = header.map(); // processResponse(cf.get());
System.out.println("Response Headers"); // }
map.forEach((k, v) -> System.out.println("\t" + k + ":" + v)); //
} // public static void processResponse(HttpResponse resp) {
// log.info("Status Code:" + resp.statusCode());
} // //System.out.println("Response Body:"+resp.body());
// HttpHeaders header = resp.headers();
// Map<String, List<String>> map = header.map();
// log.info("Response Headers");
// map.forEach((k, v) -> System.out.println("\t" + k + ":" + v));
// }
//
//}
package com.nisum.java9Features.http2; //package com.nisum.java9Features.http2;
//
//
import java.net.*; //import jdk.incubator.http.HttpClient;
import java.net.http.*; //import jdk.incubator.http.HttpHeaders;
import java.nio.file.*; //import jdk.incubator.http.HttpRequest;
import java.util.*; //import jdk.incubator.http.HttpResponse;
//
public class TestSync { //import java.net.URI;
public static void main(String[] args) throws Exception { //import java.util.List;
String url = "https://www.redbus.in/info/aboutus"; //import java.util.Map;
sendGetSyncRequest(url); //import java.util.concurrent.CompletableFuture;
} //import java.util.logging.Logger;
//
public static void sendGetSyncRequest(String url) throws Exception { //public class TestSync {
HttpClient client = HttpClient.newHttpClient(); // private final static Logger log =Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
HttpRequest req = HttpRequest.newBuilder(new URI(url)).GET().build(); // public static void main(String[] args) throws Exception {
HttpResponse resp = client.send(req, HttpResponse.BodyHandlers.ofString()); // String url = "https://www.redbus.in/info/aboutus";
// HttpResponse resp1=client.send(req,HttpResponse.BodyHandlers.ofFile(Paths.get("abc.html")) ); // sendGetSyncRequest(url);
processResponse(resp); // }
} //
// public static void sendGetSyncRequest(String url) throws Exception {
public static void processResponse(HttpResponse resp) { // HttpClient client = HttpClient.newHttpClient();
System.out.println("Status Code:" + resp.statusCode()); // HttpRequest req = HttpRequest.newBuilder(new URI(url)).GET().build();
//System.out.println("Response Body:" + resp.body()); // HttpResponse resp = client.send(req, HttpResponse.BodyHandler.asString());
HttpHeaders header = resp.headers(); // // HttpResponse resp1=client.send(req,HttpResponse.BodyHandlers.ofFile(Paths.get("abc.html")) );
Map<String, List<String>> map = header.map(); // processResponse(resp);
System.out.println("Response Headers"); // }
map.forEach((k, v) -> System.out.println("\t" + k + ":" + v)); //
} // public static void processResponse(HttpResponse resp) {
// log.info("Status Code: {}" + resp.statusCode());
} // //System.out.println("Response Body:" + resp.body());
\ No newline at end of file // HttpHeaders header = resp.headers();
// Map<String, List<String>> map = header.map();
// log.info("Response Headers");
// map.forEach((k, v) -> log.info("\t" + k + ":" + v));
// }
//
//}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment