Reformating and changes for new version of article

This commit is contained in:
Mateo Stjepanović
2021-11-21 14:13:25 +01:00
parent 7b1f3dd12f
commit b292d2b21d
15 changed files with 143 additions and 142 deletions

View File

@@ -3,25 +3,23 @@ package com.reflectoring.io.java10;
import java.util.List;
public class LocalTypeVar {
public static void main(String[] args) {
}
public void explicitTypes(){
public void explicitTypes() {
Person Roland = new Person("Roland", "Deschain");
Person Susan = new Person("Susan", "Delgado");
Person Eddie = new Person("Eddie", "Dean");
Person Detta = new Person("Detta", "Walker");
Person Jake = new Person("Jake", "Chambers");
List<Person> persons = List.of(Roland, Susan, Eddie, Detta, Jake);
List<Person> persons =
List.of(Roland, Susan, Eddie, Detta, Jake);
for(Person person : persons){
for (Person person : persons) {
System.out.println(person.name + " - " + person.lastname);
}
}
public void varTypes(){
public void varTypes() {
var Roland = new Person("Roland", "Deschain");
var Susan = new Person("Susan", "Delgado");
var Eddie = new Person("Eddie", "Dean");
@@ -30,16 +28,16 @@ public class LocalTypeVar {
var persons = List.of(Roland, Susan, Eddie, Detta, Jake);
for(var person : persons){
for (var person : persons) {
System.out.println(person.name + " - " + person.lastname);
}
}
public class Person{
public class Person {
String name;
String lastname;
public Person(String name, String lastname){
public Person(String name, String lastname) {
this.name = name;
this.lastname = lastname;
}

View File

@@ -7,24 +7,27 @@ import java.util.stream.Collectors;
public class LocalTypeVarLambda {
public void explicitTypes(){
public void explicitTypes() {
var Roland = new Person("Roland", "Deschain");
var Susan = new Person("Susan", "Delgado");
var Eddie = new Person("Eddie", "Dean");
var Detta = new Person("Detta", "Walker");
var Jake = new Person("Jake", "Chambers");
var filteredPersons = List.of(Roland, Susan, Eddie, Detta, Jake)
.stream().filter((var x) -> x.name.contains("a")).collect(Collectors.toList());;
var filteredPersons =
List.of(Roland, Susan, Eddie, Detta, Jake)
.stream()
.filter((var x) -> x.name.contains("a"))
.collect(Collectors.toList());
System.out.println(filteredPersons);
}
public class Person{
public class Person {
String name;
String lastname;
public Person(String name, String lastname){
public Person(String name, String lastname) {
this.name = name;
this.lastname = lastname;
}

View File

@@ -6,15 +6,30 @@ public class SwitchExpression {
int days = 0;
Month month = Month.APRIL;
days = switch (month){
case JANUARY, MARCH,MAY, JULY, AUGUST,OCTOBER,DECEMBER -> 31;
switch (month) {
case JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER,
DECEMBER:
days = 31;
break;
case FEBRUARY:
days = 28;
break;
case APRIL, JUNE, SEPTEMBER, NOVEMBER:
days = 30;
break;
default:
throw new IllegalStateException();
}
days = switch (month) {
case JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER -> 31;
case FEBRUARY -> 28;
case APRIL, JUNE, SEPTEMBER,NOVEMBER -> 30;
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
default -> throw new IllegalStateException();
};
days = switch (month){
case JANUARY, MARCH,MAY, JULY, AUGUST,OCTOBER,DECEMBER -> {
days = switch (month) {
case JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER -> {
System.out.println(month);
yield 31;
}
@@ -22,29 +37,16 @@ public class SwitchExpression {
System.out.println(month);
yield 28;
}
case APRIL, JUNE, SEPTEMBER,NOVEMBER -> {
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> {
System.out.println(month);
yield 30;
}
default -> throw new IllegalStateException();
};
switch (month){
case JANUARY, MARCH,MAY, JULY, AUGUST,OCTOBER,DECEMBER:
days=31;
break;
case FEBRUARY:
days=28;
break;
case APRIL, JUNE, SEPTEMBER,NOVEMBER:
days = 30;
break;
default:
throw new IllegalStateException();
}
}
public enum Month{
public enum Month {
JANUARY,
FEBRUARY,
MARCH,

View File

@@ -3,32 +3,30 @@ package com.reflectoring.io.java15;
public class TextBlocks {
public static void main(String[] args) {
System.out.println(
"<!DOCTYPE html>\n" +
"<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <title>Example</title>\n" +
" </head>\n" +
" <body>\n" +
" <p>This is an example of a simple HTML page with one paragraph.</p>\n" +
" <p>This is an example of a simple HTML " +
"page with one paragraph.</p>\n" +
" </body>\n" +
"</html>\n");
System.out.println(
"""
"""
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<p>This is an example of a simple HTML page with one paragraph.</p>
<p>This is an example of a simple HTML
page with one paragraph.</p>
</body>
</html>
"""
);
// System.out.println(
// """Illegal text block start: missing new line after opening quotes
// """);
}
}

View File

@@ -5,29 +5,33 @@ import java.util.Calendar;
public class PatternMatching {
public static void main(String[] args) {
Car car = new Car("Kia", "Gas", 10000l, 2021l);
Bicycle bicycle = new Bicycle("Greyp", "Electricity", "Mountain", 21l);
Bicycle bicycle =
new Bicycle("Greyp", "Electricity", "Mountain", 21l);
System.out.println("Car:" + price(car));
System.out.println("Bycicle: " + price(bicycle));
}
public static double price(Vehicle v){
if(v instanceof Car c){
return 10000 - c.kilomenters*0.01 - (Calendar.getInstance().get(Calendar.YEAR) - c.year)*100;
}else if(v instanceof Bicycle b){
return 1000 + b.wheelSize*10;
}else throw new IllegalArgumentException();
public static double price(Vehicle v) {
if (v instanceof Car c) {
return 10000 - c.kilomenters * 0.01 -
(Calendar.getInstance().get(Calendar.YEAR) -
c.year) * 100;
} else if (v instanceof Bicycle b) {
return 1000 + b.wheelSize * 10;
} else throw new IllegalArgumentException();
}
public static double priceOld(Vehicle v){
if(v instanceof Car){
Car c = (Car)v;
return 10000 - c.kilomenters*0.01 - (Calendar.getInstance().get(Calendar.YEAR) - c.year)*100;
}else if(v instanceof Bicycle){
Bicycle b = (Bicycle)v;
return 1000 + b.wheelSize*10;
}else throw new IllegalArgumentException();
public static double priceOld(Vehicle v) {
if (v instanceof Car) {
Car c = (Car) v;
return 10000 - c.kilomenters * 0.01 -
(Calendar.getInstance().get(Calendar.YEAR) -
c.year) * 100;
} else if (v instanceof Bicycle) {
Bicycle b = (Bicycle) v;
return 1000 + b.wheelSize * 10;
} else throw new IllegalArgumentException();
}
}

View File

@@ -32,7 +32,8 @@ public class Vehicle {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vehicle vehicle = (Vehicle) o;
return code.equals(vehicle.code) && engineType.equals(vehicle.engineType);
return code.equals(vehicle.code) &&
engineType.equals(vehicle.engineType);
}
@Override

View File

@@ -32,7 +32,8 @@ public sealed class Vehicle permits Bicycle, Car {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vehicle vehicle = (Vehicle) o;
return code.equals(vehicle.code) && engineType.equals(vehicle.engineType);
return code.equals(vehicle.code) &&
engineType.equals(vehicle.engineType);
}
@Override

View File

@@ -4,15 +4,15 @@ import java.util.Date;
public class DefaultMethods {
public interface Logging{
public interface Logging {
void log(String message);
default void log(String message, Date date){
default void log(String message, Date date) {
System.out.println(date.toString() + ": " + message);
}
}
public class LoggingImplementation implements Logging{
public class LoggingImplementation implements Logging {
@Override
public void log(String message) {

View File

@@ -6,45 +6,33 @@ import java.util.stream.Collectors;
public class LambdaExpressions {
public static void main(String[] args) {
List<Car> cars = new ArrayList<>();
List<Car> oldWay = findCarsOldWay(cars);
System.out.println(oldWay);
List<Car> oldWayModel = findCarsWithModelOldWay(cars);
System.out.println(oldWayModel);
List<Car> filterLambda = findCarsUsingLambda(cars);
System.out.println(filterLambda);
public static List<Car> findCarsUsingLambda(List<Car> cars) {
return cars.stream().filter(car -> car.kilometers < 50000)
.collect(Collectors.toList());
}
public static List<Car> findCarsUsingLambda(List<Car> cars){
return cars.stream().filter(car -> car.kilometers < 50000).collect(Collectors.toList());
}
public static List<Car> findCarsOldWay(List<Car> cars){
public static List<Car> findCarsOldWay(List<Car> cars) {
List<Car> selectedCars = new ArrayList<>();
for (Car car: cars) {
if(car.kilometers < 50000){
for (Car car : cars) {
if (car.kilometers < 50000) {
selectedCars.add(car);
}
}
return selectedCars;
}
public static List<Car> findCarsWithModelOldWay(List<Car> cars){
public static List<Car> findCarsWithModelOldWay(List<Car> cars) {
List<Car> selectedCars = new ArrayList<>();
for(Car car: cars){
if(car.kilometers < 50000 && car.model.equals("Mercedes")){
for (Car car : cars) {
if (car.kilometers < 50000 &&
car.model.equals("Mercedes")) {
selectedCars.add(car);
}
}
return selectedCars;
}
public class Car{
public class Car {
public String model;
public double kilometers;
}

View File

@@ -6,19 +6,16 @@ import java.util.stream.Collectors;
public class MethodReference {
public static void main(String[] args) {
List<Car> cars = new ArrayList<>();
List<Car> cars = new ArrayList<>();
List<String> withoutMethodReference = cars.stream().map(car -> car.toString()).collect(Collectors.toList());
System.out.println(withoutMethodReference);
List<String> withoutMethodReference =
cars.stream().map(car -> car.toString())
.collect(Collectors.toList());
List<String> methodReference = cars.stream().map(Car::toString).collect(Collectors.toList());
System.out.println(methodReference);
List<String> methodReference = cars.stream().map(Car::toString)
.collect(Collectors.toList());
}
public class Car{
public class Car {
public String model;
public double kilometers;

View File

@@ -4,24 +4,22 @@ import java.lang.annotation.Repeatable;
public class RepeatingAnnotations {
public static void main(String[] args) {
}
@Repeatable(Notifications.class)
public @interface Notify{
public @interface Notify {
String email();
}
public @interface Notifications{
public @interface Notifications {
Notify[] value();
}
@Notify(email="admin@company.com")
@Notify(email="owner@company.com")
public class UserNotAllowedForThisActionException extends RuntimeException{
@Notify(email = "admin@company.com")
@Notify(email = "owner@company.com")
public class UserNotAllowedForThisActionException
extends RuntimeException {
final String user;
public UserNotAllowedForThisActionException(String user){
public UserNotAllowedForThisActionException(String user) {
this.user = user;
}

View File

@@ -5,6 +5,7 @@ import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@@ -15,22 +16,27 @@ public class TypeAnnotations {
@NotNull String userName = args[0];
List<String> request = new @NotEmpty ArrayList<>(Arrays.stream(args).toList());
List<String> request =
new @NotEmpty ArrayList<>(Arrays.stream(args).collect(
Collectors.toList()));
List<@Email String> emails;
}
@Target(value={TYPE_USE})
@Retention(value=RUNTIME)
public @interface NotNull{}
@Target(value = {TYPE_USE})
@Retention(value = RUNTIME)
public @interface NotNull {
}
@Target(value={TYPE_USE})
@Retention(value=RUNTIME)
public @interface NotEmpty{}
@Target(value = {TYPE_USE})
@Retention(value = RUNTIME)
public @interface NotEmpty {
}
@Target(value={TYPE_USE})
@Retention(value=RUNTIME)
public @interface Email{}
@Target(value = {TYPE_USE})
@Retention(value = RUNTIME)
public @interface Email {
}
}

View File

@@ -1,16 +1,16 @@
package com.reflectoring.io.java9;
public class DiamondOperator {
public static void main(String[] args) {
StringAppender<String> appending = new StringAppender<String>() {
@Override
public String append(String a, String b) {
return new StringBuilder(a).append("-").append(b).toString();
}
};
}
public abstract static class StringAppender<T>{
StringAppender<String> appending = new StringAppender<>() {
@Override
public String append(String a, String b) {
return new StringBuilder(a).append("-").append(b)
.toString();
}
};
public abstract static class StringAppender<T> {
public abstract T append(String a, String b);
}
}

View File

@@ -13,13 +13,16 @@ public class PrivateInterfaceMethods {
System.out.println(names.fetchInitialData());
}
public static class TestingNames implements NamesInterface{
public TestingNames(){}
public static class TestingNames implements NamesInterface {
public TestingNames() {
}
}
public interface NamesInterface{
default List<String> fetchInitialData(){
try(BufferedReader br = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/names.txt")))) {
public interface NamesInterface {
default List<String> fetchInitialData() {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(this.getClass()
.getResourceAsStream("/names.txt")))) {
return readNames(br);
} catch (IOException e) {
e.printStackTrace();
@@ -27,14 +30,14 @@ public class PrivateInterfaceMethods {
}
}
private List<String> readNames(BufferedReader br) throws IOException {
private List<String> readNames(BufferedReader br)
throws IOException {
ArrayList<String> names = new ArrayList<>();
String name;
while((name = br.readLine()) != null){
while ((name = br.readLine()) != null) {
names.add(name);
}
return names;
}
}
}

View File

@@ -6,12 +6,13 @@ import java.io.StringReader;
public class TryWithResources {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new StringReader("Hello world example!"));
BufferedReader br = new BufferedReader(
new StringReader("Hello world example!"));
try {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}finally {
} finally {
try {
br.close();
} catch (IOException e) {
@@ -19,18 +20,19 @@ public class TryWithResources {
}
}
final BufferedReader br2 = new BufferedReader(new StringReader("Hello world example2!"));
try(br2){
final BufferedReader br2 = new BufferedReader(
new StringReader("Hello world example2!"));
try (br2) {
System.out.println(br2.readLine());
}
catch (IOException e){
} catch (IOException e) {
System.out.println("Error happened!");
}
final BufferedReader br3 = new BufferedReader(new StringReader("Hello world example3!"));
try(BufferedReader reader = br3){
final BufferedReader br3 = new BufferedReader(
new StringReader("Hello world example3!"));
try (BufferedReader reader = br3) {
System.out.println(reader.readLine());
}catch (IOException e){
} catch (IOException e) {
System.out.println("Error happened!");
}
}