cleanup work

This commit is contained in:
eugenp
2016-10-25 11:09:51 +03:00
parent a44be73deb
commit 2ab201d639
49 changed files with 489 additions and 653 deletions

View File

@@ -5,12 +5,12 @@ import java.time.LocalTime;
import java.time.Period;
public class UseDuration {
public LocalTime modifyDates(LocalTime localTime,Duration duration){
public LocalTime modifyDates(LocalTime localTime, Duration duration) {
return localTime.plus(duration);
}
public Duration getDifferenceBetweenDates(LocalTime localTime1,LocalTime localTime2){
public Duration getDifferenceBetweenDates(LocalTime localTime1, LocalTime localTime2) {
return Duration.between(localTime1, localTime2);
}
}

View File

@@ -7,39 +7,39 @@ import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
public class UseLocalDate {
public LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth){
public LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth) {
return LocalDate.of(year, month, dayOfMonth);
}
public LocalDate getLocalDateUsingParseMethod(String representation){
public LocalDate getLocalDateUsingParseMethod(String representation) {
return LocalDate.parse(representation);
}
public LocalDate getLocalDateFromClock(){
public LocalDate getLocalDateFromClock() {
LocalDate localDate = LocalDate.now();
return localDate;
}
public LocalDate getNextDay(LocalDate localDate){
public LocalDate getNextDay(LocalDate localDate) {
return localDate.plusDays(1);
}
public LocalDate getPreviousDay(LocalDate localDate){
public LocalDate getPreviousDay(LocalDate localDate) {
return localDate.minus(1, ChronoUnit.DAYS);
}
public DayOfWeek getDayOfWeek(LocalDate localDate){
public DayOfWeek getDayOfWeek(LocalDate localDate) {
DayOfWeek day = localDate.getDayOfWeek();
return day;
}
public LocalDate getFirstDayOfMonth(){
public LocalDate getFirstDayOfMonth() {
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
return firstDayOfMonth;
}
public LocalDateTime getStartOfDay(LocalDate localDate){
public LocalDateTime getStartOfDay(LocalDate localDate) {
LocalDateTime startofDay = localDate.atStartOfDay();
return startofDay;
}

View File

@@ -3,8 +3,8 @@ package com.baeldung.datetime;
import java.time.LocalDateTime;
public class UseLocalDateTime {
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation){
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation) {
return LocalDateTime.parse(representation);
}

View File

@@ -4,32 +4,32 @@ import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class UseLocalTime {
public LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds){
public LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds) {
LocalTime localTime = LocalTime.of(hour, min, seconds);
return localTime;
}
public LocalTime getLocalTimeUsingParseMethod(String timeRepresentation){
public LocalTime getLocalTimeUsingParseMethod(String timeRepresentation) {
LocalTime localTime = LocalTime.parse(timeRepresentation);
return localTime;
}
public LocalTime getLocalTimeFromClock(){
public LocalTime getLocalTimeFromClock() {
LocalTime localTime = LocalTime.now();
return localTime;
}
public LocalTime addAnHour(LocalTime localTime){
LocalTime newTime = localTime.plus(1,ChronoUnit.HOURS);
public LocalTime addAnHour(LocalTime localTime) {
LocalTime newTime = localTime.plus(1, ChronoUnit.HOURS);
return newTime;
}
public int getHourFromLocalTime(LocalTime localTime){
public int getHourFromLocalTime(LocalTime localTime) {
return localTime.getHour();
}
public LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute){
public LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute) {
return localTime.withMinute(minute);
}
}

View File

@@ -4,12 +4,12 @@ import java.time.LocalDate;
import java.time.Period;
public class UsePeriod {
public LocalDate modifyDates(LocalDate localDate,Period period){
public LocalDate modifyDates(LocalDate localDate, Period period) {
return localDate.plus(period);
}
public Period getDifferenceBetweenDates(LocalDate localDate1,LocalDate localDate2){
public Period getDifferenceBetweenDates(LocalDate localDate1, LocalDate localDate2) {
return Period.between(localDate1, localDate2);
}
}

View File

@@ -6,13 +6,13 @@ import java.util.Calendar;
import java.util.Date;
public class UseToInstant {
public LocalDateTime convertDateToLocalDate(Date date){
public LocalDateTime convertDateToLocalDate(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
return localDateTime;
}
public LocalDateTime convertDateToLocalDate(Calendar calendar){
public LocalDateTime convertDateToLocalDate(Calendar calendar) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
return localDateTime;
}

View File

@@ -5,8 +5,8 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
public class UseZonedDateTime {
public ZonedDateTime getZonedDateTime(LocalDateTime localDateTime,ZoneId zoneId){
public ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) {
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
return zonedDateTime;
}

View File

@@ -7,8 +7,7 @@ import java.util.stream.Collectors;
public class Pizza {
private static EnumSet<PizzaStatusEnum> deliveredPizzaStatuses =
EnumSet.of(PizzaStatusEnum.DELIVERED);
private static EnumSet<PizzaStatusEnum> deliveredPizzaStatuses = EnumSet.of(PizzaStatusEnum.DELIVERED);
private PizzaStatusEnum status;
@@ -76,9 +75,7 @@ public class Pizza {
}
public static EnumMap<PizzaStatusEnum, List<Pizza>> groupPizzaByStatus(List<Pizza> pzList) {
return pzList.stream().collect(
Collectors.groupingBy(Pizza::getStatus,
() -> new EnumMap<>(PizzaStatusEnum.class), Collectors.toList()));
return pzList.stream().collect(Collectors.groupingBy(Pizza::getStatus, () -> new EnumMap<>(PizzaStatusEnum.class), Collectors.toList()));
}
public void deliver() {

View File

@@ -1,6 +1,5 @@
package com.baeldung.enums;
public enum PizzaDeliverySystemConfiguration {
INSTANCE;

View File

@@ -30,8 +30,7 @@ public class CustomRecursiveAction extends RecursiveAction {
private Collection<CustomRecursiveAction> createSubtasks() {
List<CustomRecursiveAction> subtasks =
new ArrayList<>();
List<CustomRecursiveAction> subtasks = new ArrayList<>();
String partOne = workLoad.substring(0, workLoad.length() / 2);
String partTwo = workLoad.substring(workLoad.length() / 2, workLoad.length());

View File

@@ -1,6 +1,5 @@
package com.baeldung.forkjoin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -23,10 +22,7 @@ public class CustomRecursiveTask extends RecursiveTask<Integer> {
if (arr.length > THRESHOLD) {
return ForkJoinTask.invokeAll(createSubtasks())
.stream()
.mapToInt(ForkJoinTask::join)
.sum();
return ForkJoinTask.invokeAll(createSubtasks()).stream().mapToInt(ForkJoinTask::join).sum();
} else {
return processing(arr);
@@ -35,17 +31,12 @@ public class CustomRecursiveTask extends RecursiveTask<Integer> {
private Collection<CustomRecursiveTask> createSubtasks() {
List<CustomRecursiveTask> dividedTasks = new ArrayList<>();
dividedTasks.add(new CustomRecursiveTask(
Arrays.copyOfRange(arr, 0, arr.length / 2)));
dividedTasks.add(new CustomRecursiveTask(
Arrays.copyOfRange(arr, arr.length / 2, arr.length)));
dividedTasks.add(new CustomRecursiveTask(Arrays.copyOfRange(arr, 0, arr.length / 2)));
dividedTasks.add(new CustomRecursiveTask(Arrays.copyOfRange(arr, arr.length / 2, arr.length)));
return dividedTasks;
}
private Integer processing(int[] arr) {
return Arrays.stream(arr)
.filter(a -> a > 10 && a < 27)
.map(a -> a * 10)
.sum();
return Arrays.stream(arr).filter(a -> a > 10 && a < 27).map(a -> a * 10).sum();
}
}

View File

@@ -1,6 +1,5 @@
package com.baeldung.forkjoin.util;
import java.util.concurrent.ForkJoinPool;
public class PoolUtil {

View File

@@ -15,7 +15,7 @@ public class EchoServer {
public static void main(String[] args)
throws IOException {
throws IOException {
Selector selector = Selector.open();
ServerSocketChannel serverSocket = ServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress("localhost", 5454));

View File

@@ -9,7 +9,7 @@ public interface Vehicle {
}
default long[] startPosition() {
return new long[]{23, 15};
return new long[] { 23, 15 };
}
default String getOverview() {

View File

@@ -1,9 +1,9 @@
package com.baeldung.java_8_features;
public class VehicleImpl implements Vehicle {
public class VehicleImpl implements Vehicle {
@Override
public void moveTo(long altitude, long longitude) {
//do nothing
// do nothing
}
}

View File

@@ -4,39 +4,38 @@ import java.io.*;
import java.net.*;
public class EchoClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void startConnection(String ip, int port) {
try {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
} catch (IOException e) {
System.out.print(e);
}
public void startConnection(String ip, int port) {
try {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
System.out.print(e);
}
}
}
public String sendMessage(String msg) {
try {
out.println(msg);
return in.readLine();
} catch (Exception e) {
return null;
}
}
public String sendMessage(String msg) {
try {
out.println(msg);
return in.readLine();
} catch (Exception e) {
return null;
}
}
public void stopConnection() {
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
public void stopConnection() {
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -4,68 +4,67 @@ import java.net.*;
import java.io.*;
public class EchoMultiServer {
private ServerSocket serverSocket;
private ServerSocket serverSocket;
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
while (true)
new EchoClientHandler(serverSocket.accept()).run();
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
while (true)
new EchoClientHandler(serverSocket.accept()).run();
} catch (IOException e) {
e.printStackTrace();
} finally {
stop();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
stop();
}
}
}
public void stop() {
try {
public void stop() {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class EchoClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private static class EchoClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public EchoClientHandler(Socket socket) {
this.clientSocket = socket;
}
public EchoClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (".".equals(inputLine)) {
out.println("bye");
break;
}
out.println(inputLine);
}
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (".".equals(inputLine)) {
out.println("bye");
break;
}
out.println(inputLine);
}
in.close();
out.close();
clientSocket.close();
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
EchoMultiServer server = new EchoMultiServer();
server.start(5555);
}
public static void main(String[] args) {
EchoMultiServer server = new EchoMultiServer();
server.start(5555);
}
}

View File

@@ -4,47 +4,46 @@ import java.net.*;
import java.io.*;
public class EchoServer {
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (".".equals(inputLine)) {
out.println("good bye");
break;
}
out.println(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (".".equals(inputLine)) {
out.println("good bye");
break;
}
out.println(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stop() {
try {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
public void stop() {
try {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
EchoServer server = new EchoServer();
server.start(4444);
}
public static void main(String[] args) {
EchoServer server = new EchoServer();
server.start(4444);
}
}

View File

@@ -7,39 +7,38 @@ import java.io.PrintWriter;
import java.net.Socket;
public class GreetClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void startConnection(String ip, int port) {
try {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
} catch (IOException e) {
}
public void startConnection(String ip, int port) {
try {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
}
}
public String sendMessage(String msg) {
try {
out.println(msg);
return in.readLine();
} catch (Exception e) {
return null;
}
}
}
public void stopConnection() {
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String sendMessage(String msg) {
try {
out.println(msg);
return in.readLine();
} catch (Exception e) {
return null;
}
}
public void stopConnection() {
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -4,44 +4,43 @@ import java.net.*;
import java.io.*;
public class GreetServer {
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String greeting = in.readLine();
if ("hello server".equals(greeting))
out.println("hello client");
else
out.println("unrecognised greeting");
} catch (IOException e) {
e.printStackTrace();
}
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String greeting = in.readLine();
if ("hello server".equals(greeting))
out.println("hello client");
else
out.println("unrecognised greeting");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stop() {
try {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
public void stop() {
try {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
GreetServer server=new GreetServer();
server.start(6666);
}
public static void main(String[] args) {
GreetServer server = new GreetServer();
server.start(6666);
}
}

View File

@@ -44,7 +44,6 @@ public class Product {
this.name = name;
}
public static Stream<String> streamOf(List<String> list) {
return (list == null || list.isEmpty()) ? Stream.empty() : list.stream();
}

View File

@@ -14,9 +14,7 @@ public class CountingTask extends RecursiveTask<Integer> {
@Override
protected Integer compute() {
return node.value + node.children.stream()
.map(childNode -> new CountingTask(childNode).fork())
.collect(Collectors.summingInt(ForkJoinTask::join));
return node.value + node.children.stream().map(childNode -> new CountingTask(childNode).fork()).collect(Collectors.summingInt(ForkJoinTask::join));
}
}