[BAEL-9555] - Created a core-java-modules folder
This commit is contained in:
13
core-java-modules/core-java-9/src/main/java/.gitignore
vendored
Normal file
13
core-java-modules/core-java-9/src/main/java/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.java9.aot;
|
||||
|
||||
public class JaotCompilation {
|
||||
|
||||
public static void main(String[] argv) {
|
||||
System.out.println(message());
|
||||
}
|
||||
|
||||
public static String message() {
|
||||
return "The JAOT compiler says 'Hello'";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.java9.language;
|
||||
|
||||
public interface PrivateInterface {
|
||||
|
||||
private static String staticPrivate() {
|
||||
return "static private";
|
||||
}
|
||||
|
||||
private String instancePrivate() {
|
||||
return "instance private";
|
||||
}
|
||||
|
||||
public default void check() {
|
||||
String result = staticPrivate();
|
||||
if (!result.equals("static private"))
|
||||
throw new AssertionError("Incorrect result for static private interface method");
|
||||
PrivateInterface pvt = new PrivateInterface() {
|
||||
};
|
||||
result = pvt.instancePrivate();
|
||||
if (!result.equals("instance private"))
|
||||
throw new AssertionError("Incorrect result for instance private interface method");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import static java.util.stream.Collectors.filtering;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class StreamsGroupingCollectionFilter {
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1;
|
||||
|
||||
return baseCollection.stream()
|
||||
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList())));
|
||||
}
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
|
||||
return baseCollection.stream()
|
||||
.filter(item -> item % 2 == 0)
|
||||
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.java9.maps.initialize;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapsInitializer {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void createMapWithMapOf() {
|
||||
Map<String, String> emptyMap = Map.of();
|
||||
Map<String, String> singletonMap = Map.of("key1", "value");
|
||||
Map<String, String> map = Map.of("key1","value1", "key2", "value2");
|
||||
}
|
||||
|
||||
public void createMapWithMapEntries() {
|
||||
Map<String, String> map = Map.ofEntries(
|
||||
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||
new AbstractMap.SimpleEntry<String, String>("city", "budapest"),
|
||||
new AbstractMap.SimpleEntry<String, String>("zip", "000000"),
|
||||
new AbstractMap.SimpleEntry<String, String>("home", "1231231231")
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void createMutableMaps() {
|
||||
Map<String, String> map = new HashMap<String, String> (Map.of("key1","value1", "key2", "value2"));
|
||||
Map<String, String> map2 = new HashMap<String, String> ( Map.ofEntries(
|
||||
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||
new AbstractMap.SimpleEntry<String, String>("city", "budapest")));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.java9.methodhandles;
|
||||
|
||||
public class Book {
|
||||
|
||||
String id;
|
||||
String title;
|
||||
|
||||
public Book(String id, String title) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private String formatBook() {
|
||||
return id + " > " + title;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
public class DatesCollectionIteration {
|
||||
|
||||
public void iteratingRangeOfDatesJava7(Collection<Date> dates) {
|
||||
|
||||
for (Date date : dates) {
|
||||
processDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
public void iteratingRangeOfDatesJava8(Collection<Date> dates) {
|
||||
dates.stream()
|
||||
.forEach(this::processDate);
|
||||
}
|
||||
|
||||
private void processDate(Date date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class RangeDatesIteration {
|
||||
|
||||
public void iterateBetweenDatesJava9(LocalDate startDate, LocalDate endDate) {
|
||||
|
||||
startDate.datesUntil(endDate)
|
||||
.forEach(this::processDate);
|
||||
}
|
||||
|
||||
public void iterateBetweenDatesJava8(LocalDate start, LocalDate end) {
|
||||
for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
|
||||
processDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateBetweenDatesJava7(Date start, Date end) {
|
||||
Date current = start;
|
||||
|
||||
while (current.before(end)) {
|
||||
processDate(current);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(current);
|
||||
|
||||
calendar.add(Calendar.DATE, 1);
|
||||
|
||||
current = calendar.getTime();
|
||||
}
|
||||
}
|
||||
|
||||
private void processDate(LocalDate date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
|
||||
private void processDate(Date date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.java9.reactive;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Flow.Subscriber;
|
||||
import java.util.concurrent.Flow.Subscription;
|
||||
|
||||
public class BaeldungBatchSubscriberImpl<T> implements Subscriber<String> {
|
||||
private Subscription subscription;
|
||||
private boolean completed = false;
|
||||
private int counter;
|
||||
private ArrayList<String> buffer;
|
||||
public static final int BUFFER_SIZE = 5;
|
||||
|
||||
public BaeldungBatchSubscriberImpl() {
|
||||
buffer = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(boolean completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
public void setCounter(int counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
subscription.request(BUFFER_SIZE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(String item) {
|
||||
buffer.add(item);
|
||||
// if buffer is full, process the items.
|
||||
if (buffer.size() >= BUFFER_SIZE) {
|
||||
processBuffer();
|
||||
}
|
||||
//request more items.
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
private void processBuffer() {
|
||||
if (buffer.isEmpty())
|
||||
return;
|
||||
// Process all items in the buffer. Here, we just print it and sleep for 1 second.
|
||||
System.out.print("Processed items: ");
|
||||
buffer.stream()
|
||||
.forEach(item -> {
|
||||
System.out.print(" " + item);
|
||||
});
|
||||
System.out.println();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
counter = counter + buffer.size();
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
completed = true;
|
||||
// process any remaining items in buffer before
|
||||
processBuffer();
|
||||
subscription.cancel();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.java9.reactive;
|
||||
|
||||
import java.util.concurrent.Flow.Subscriber;
|
||||
import java.util.concurrent.Flow.Subscription;
|
||||
|
||||
public class BaeldungSubscriberImpl<T> implements Subscriber<String> {
|
||||
private Subscription subscription;
|
||||
private boolean completed = false;
|
||||
private int counter;
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(boolean completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
public void setCounter(int counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(String item) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
counter++;
|
||||
System.out.println("Processed item : " + item);
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
completed = true;
|
||||
subscription.cancel();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.java9.set;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class UnmodifiableSet {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("Canada");
|
||||
set.add("USA");
|
||||
|
||||
coreJDK(set);
|
||||
guavaOf();
|
||||
copyOf(set);
|
||||
java9Of();
|
||||
}
|
||||
|
||||
private static void java9Of() {
|
||||
Set<String> immutable = Set.of("Canada", "USA");
|
||||
System.out.println(immutable);
|
||||
}
|
||||
|
||||
private static void guavaOf() {
|
||||
Set<String> immutable = ImmutableSet.of("Canada", "USA");
|
||||
System.out.println(immutable);
|
||||
}
|
||||
|
||||
private static void copyOf(Set<String> set) {
|
||||
Set<String> immutable = ImmutableSet.copyOf(set);
|
||||
set.add("Costa Rica");
|
||||
System.out.println(immutable);
|
||||
}
|
||||
|
||||
private static void coreJDK(Set<String> set) {
|
||||
Set<String> unmodifiableSet = Collections.unmodifiableSet(set);
|
||||
set.add("Costa Rica");
|
||||
System.out.println(unmodifiableSet);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.baeldung.java9.stackwalker;
|
||||
|
||||
import java.lang.StackWalker.StackFrame;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StackWalkerDemo {
|
||||
|
||||
public void methodOne() {
|
||||
this.methodTwo();
|
||||
}
|
||||
|
||||
public void methodTwo() {
|
||||
this.methodThree();
|
||||
}
|
||||
|
||||
public void methodThree() {
|
||||
List<StackFrame> stackTrace = StackWalker.getInstance()
|
||||
.walk(this::walkExample);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
stackTrace = StackWalker.getInstance()
|
||||
.walk(this::walkExample2);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
String line = StackWalker.getInstance().walk(this::walkExample3);
|
||||
System.out.println(line);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
stackTrace = StackWalker.getInstance(StackWalker.Option.SHOW_REFLECT_FRAMES)
|
||||
.walk(this::walkExample);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
Runnable r = () -> {
|
||||
List<StackFrame> stackTrace2 = StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES)
|
||||
.walk(this::walkExample);
|
||||
printStackTrace(stackTrace2);
|
||||
};
|
||||
r.run();
|
||||
}
|
||||
|
||||
public List<StackFrame> walkExample(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<StackFrame> walkExample2(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.filter(frame -> frame.getClassName()
|
||||
.contains("com.baeldung"))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public String walkExample3(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.filter(frame -> frame.getClassName()
|
||||
.contains("com.baeldung")
|
||||
&& frame.getClassName()
|
||||
.endsWith("Test"))
|
||||
.findFirst()
|
||||
.map(frame -> frame.getClassName() + "#" + frame.getMethodName() + ", Line " + frame.getLineNumber())
|
||||
.orElse("Unknown caller");
|
||||
}
|
||||
|
||||
public void findCaller() {
|
||||
Class<?> caller = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass();
|
||||
System.out.println(caller.getCanonicalName());
|
||||
}
|
||||
|
||||
public void printStackTrace(List<StackFrame> stackTrace) {
|
||||
for (StackFrame stackFrame : stackTrace) {
|
||||
System.out.println(stackFrame.getClassName()
|
||||
.toString() + "#" + stackFrame.getMethodName() + ", Line " + stackFrame.getLineNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.java9.streams.reactive;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Flow.Subscriber;
|
||||
import java.util.concurrent.Flow.Subscription;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class EndSubscriber<T> implements Subscriber<T> {
|
||||
private final AtomicInteger howMuchMessagesToConsume;
|
||||
private Subscription subscription;
|
||||
public List<T> consumedElements = new LinkedList<>();
|
||||
|
||||
public EndSubscriber(Integer howMuchMessagesToConsume) {
|
||||
this.howMuchMessagesToConsume = new AtomicInteger(howMuchMessagesToConsume);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(T item) {
|
||||
howMuchMessagesToConsume.decrementAndGet();
|
||||
System.out.println("Got : " + item);
|
||||
consumedElements.add(item);
|
||||
if (howMuchMessagesToConsume.get() > 0) {
|
||||
subscription.request(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
System.out.println("Done");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.java9.streams.reactive;
|
||||
|
||||
import java.util.concurrent.Flow;
|
||||
import java.util.concurrent.SubmissionPublisher;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TransformProcessor<T, R> extends SubmissionPublisher<R> implements Flow.Processor<T, R> {
|
||||
|
||||
private Function<T, R> function;
|
||||
private Flow.Subscription subscription;
|
||||
|
||||
public TransformProcessor(Function<T, R> function) {
|
||||
super();
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Flow.Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(T item) {
|
||||
submit(function.apply(item));
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.multireleaseapp;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class App {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(App.class);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String dateToCheck = args[0];
|
||||
boolean isLeapYear = DateHelper.checkIfLeapYear(dateToCheck);
|
||||
logger.info("Date given " + dateToCheck + " is leap year: " + isLeapYear);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.multireleaseapp;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DateHelper {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DateHelper.class);
|
||||
|
||||
public static boolean checkIfLeapYear(String dateStr) throws Exception {
|
||||
logger.info("Checking for leap year using Java 1 calendar API");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
|
||||
int year = cal.get(Calendar.YEAR);
|
||||
return (new GregorianCalendar()).isLeapYear(year);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.optionals;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Optionals {
|
||||
|
||||
public static <T> Optional<T> or(Optional<T> optional, Optional<T> fallback) {
|
||||
return optional.isPresent() ? optional : fallback;
|
||||
}
|
||||
|
||||
public static Optional<String> getName(Optional<String> name) {
|
||||
return name.or(() -> getCustomMessage());
|
||||
}
|
||||
|
||||
public static com.google.common.base.Optional<String> getOptionalGuavaName(com.google.common.base.Optional<String> name) {
|
||||
return name.or(getCustomMessageGuava());
|
||||
}
|
||||
|
||||
private static Optional<String> getCustomMessage() {
|
||||
return Optional.of("Name not provided");
|
||||
}
|
||||
|
||||
private static com.google.common.base.Optional<String> getCustomMessageGuava() {
|
||||
return com.google.common.base.Optional.of("Name not provided");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.multireleaseapp;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DateHelper {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DateHelper.class);
|
||||
|
||||
public static boolean checkIfLeapYear(String dateStr) throws Exception {
|
||||
logger.info("Checking for leap year using Java 9 Date Api");
|
||||
return LocalDate.parse(dateStr)
|
||||
.isLeapYear();
|
||||
}
|
||||
|
||||
}
|
||||
19
core-java-modules/core-java-9/src/main/resources/logback.xml
Normal file
19
core-java-modules/core-java-9/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user