-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples of usage of Java 8 features
Moved from repo: https://github.com/andrei-punko/java-interview-coding
- Loading branch information
1 parent
79e76d2
commit fbe4f56
Showing
20 changed files
with
501 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
common/src/main/java/by/andd3dfx/java8/annotations/Hint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package by.andd3dfx.java8.annotations; | ||
|
||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Repeatable(Hints.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Hint { | ||
|
||
String value(); | ||
} |
10 changes: 10 additions & 0 deletions
10
common/src/main/java/by/andd3dfx/java8/annotations/Hints.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package by.andd3dfx.java8.annotations; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Hints { | ||
|
||
Hint[] value(); | ||
} |
6 changes: 6 additions & 0 deletions
6
common/src/main/java/by/andd3dfx/java8/annotations/NewApproachPerson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package by.andd3dfx.java8.annotations; | ||
|
||
@Hint("hint1") | ||
@Hint("hint2") | ||
public class NewApproachPerson { | ||
} |
5 changes: 5 additions & 0 deletions
5
common/src/main/java/by/andd3dfx/java8/annotations/OldApproachPerson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package by.andd3dfx.java8.annotations; | ||
|
||
@Hints({@Hint("hint1"), @Hint("hint2")}) | ||
public class OldApproachPerson { | ||
} |
7 changes: 7 additions & 0 deletions
7
common/src/main/java/by/andd3dfx/java8/functionalinterface/Converter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package by.andd3dfx.java8.functionalinterface; | ||
|
||
@FunctionalInterface | ||
public interface Converter<F, T> { | ||
|
||
T convert(F from); | ||
} |
10 changes: 10 additions & 0 deletions
10
common/src/main/java/by/andd3dfx/java8/interfacebydefault/Formula.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package by.andd3dfx.java8.interfacebydefault; | ||
|
||
public interface Formula { | ||
|
||
double calculate(int a); | ||
|
||
default double sqrt(int a) { | ||
return Math.sqrt(a); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package by.andd3dfx.java8.lambda; | ||
|
||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class Lambda { | ||
|
||
public void usualApproach(List<String> names) { | ||
Collections.sort(names, new Comparator<String>() { | ||
@Override | ||
public int compare(String a, String b) { | ||
return a.compareTo(b); | ||
} | ||
}); | ||
} | ||
|
||
public void lambda1(List<String> names) { | ||
Collections.sort(names, (String a, String b) -> { | ||
return a.compareTo(b); | ||
}); | ||
} | ||
|
||
public void lambda2(List<String> names) { | ||
Collections.sort(names, (String a, String b) -> a.compareTo(b)); | ||
} | ||
|
||
public void lambda3(List<String> names) { | ||
Collections.sort(names, (a, b) -> a.compareTo(b)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
common/src/main/java/by/andd3dfx/java8/linkstomethodsandconstructors/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package by.andd3dfx.java8.linkstomethodsandconstructors; | ||
|
||
public class Person { | ||
|
||
private String firstName; | ||
private String lastName; | ||
|
||
public Person(String firstName, String lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
common/src/main/java/by/andd3dfx/java8/linkstomethodsandconstructors/PersonFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package by.andd3dfx.java8.linkstomethodsandconstructors; | ||
|
||
public interface PersonFactory<P extends Person> { | ||
|
||
P create(String firstName, String lastName); | ||
} |
8 changes: 8 additions & 0 deletions
8
common/src/main/java/by/andd3dfx/java8/linkstomethodsandconstructors/Something.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package by.andd3dfx.java8.linkstomethodsandconstructors; | ||
|
||
public class Something { | ||
|
||
public String startsWith(String s) { | ||
return String.valueOf(s.charAt(0)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
common/src/test/java/by/andd3dfx/java8/annotations/HintsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package by.andd3dfx.java8.annotations; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class HintsTest { | ||
|
||
@Test | ||
public void testOldApproach() { | ||
Hint hint = OldApproachPerson.class.getAnnotation(Hint.class); | ||
assertThat(hint).isNull(); | ||
|
||
Hints hints1 = OldApproachPerson.class.getAnnotation(Hints.class); | ||
assertThat(hints1.value().length).isEqualTo(2); | ||
|
||
Hint[] hints2 = OldApproachPerson.class.getAnnotationsByType(Hint.class); | ||
assertThat(hints2.length).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
public void testNewApproach() { | ||
Hint hint = NewApproachPerson.class.getAnnotation(Hint.class); | ||
assertThat(hint).isNull(); | ||
|
||
Hints hints1 = NewApproachPerson.class.getAnnotation(Hints.class); | ||
assertThat(hints1.value().length).isEqualTo(2); | ||
|
||
Hint[] hints2 = NewApproachPerson.class.getAnnotationsByType(Hint.class); | ||
assertThat(hints2.length).isEqualTo(2); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
common/src/test/java/by/andd3dfx/java8/datetime/DateTimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package by.andd3dfx.java8.datetime; | ||
|
||
import org.junit.Test; | ||
|
||
import java.time.Clock; | ||
import java.time.DayOfWeek; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.Month; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.FormatStyle; | ||
import java.time.temporal.ChronoField; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class DateTimeTest { | ||
|
||
@Test | ||
public void clock() { | ||
Clock clock = Clock.systemDefaultZone(); | ||
long millis = clock.millis(); | ||
|
||
Instant instant = clock.instant(); | ||
Date legacyDate = Date.from(instant); // legacy java.util.Date | ||
} | ||
|
||
@Test | ||
public void zoneId() { | ||
var ids = ZoneId.getAvailableZoneIds(); // all available timezone ids | ||
|
||
ZoneId zone1 = ZoneId.of("Europe/Berlin"); | ||
ZoneId zone2 = ZoneId.of("Brazil/East"); | ||
var zoneRules1 = zone1.getRules(); // ZoneRules[currentStandardOffset=+01:00] | ||
var zoneRules2 = zone2.getRules(); // ZoneRules[currentStandardOffset=-03:00] | ||
} | ||
|
||
@Test | ||
public void localTime() { | ||
ZoneId zone1 = ZoneId.of("Europe/Berlin"); | ||
ZoneId zone2 = ZoneId.of("Brazil/East"); | ||
|
||
LocalTime now1 = LocalTime.now(zone1); | ||
LocalTime now2 = LocalTime.now(zone2); | ||
|
||
var isNow1BeforeNow2 = now1.isBefore(now2); | ||
assertThat(isNow1BeforeNow2).isFalse(); | ||
|
||
long hoursBetween = ChronoUnit.HOURS.between(now1, now2); //4 | ||
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2); | ||
assertThat(minutesBetween).isEqualTo(-299); | ||
|
||
LocalTime late = LocalTime.of(23, 59, 59); // 23:59:59 | ||
|
||
DateTimeFormatter germanFormatter = DateTimeFormatter | ||
.ofLocalizedTime(FormatStyle.SHORT) | ||
.withLocale(Locale.GERMAN); | ||
|
||
LocalTime germanTime = LocalTime.parse("13:37", germanFormatter); // 13:37 | ||
} | ||
|
||
@Test | ||
public void localDate() { | ||
LocalDate today = LocalDate.now(); | ||
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); | ||
LocalDate yesterday = tomorrow.minusDays(2); | ||
|
||
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); | ||
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); | ||
assertThat(dayOfWeek).isEqualTo(DayOfWeek.FRIDAY); | ||
|
||
// Create LocalDate by parsing string: | ||
DateTimeFormatter germanFormatter = | ||
DateTimeFormatter | ||
.ofLocalizedDate(FormatStyle.MEDIUM) | ||
.withLocale(Locale.GERMAN); | ||
|
||
LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter); // 2014-12-24 | ||
} | ||
|
||
@Test | ||
public void localDateTime() { | ||
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59); | ||
|
||
DayOfWeek dayOfWeek = sylvester.getDayOfWeek(); | ||
assertThat(dayOfWeek).isEqualTo(DayOfWeek.WEDNESDAY); | ||
|
||
Month month = sylvester.getMonth(); | ||
assertThat(month).isEqualTo(Month.DECEMBER); | ||
|
||
long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY); | ||
assertThat(minuteOfDay).isEqualTo(1439); | ||
|
||
// Get an instant: | ||
Instant instant = sylvester | ||
.atZone(ZoneId.systemDefault()) | ||
.toInstant(); | ||
|
||
Date legacyDate = Date.from(instant); // Wed Dec 31 23:59:59 CET 2014 | ||
|
||
// Formatting: | ||
DateTimeFormatter formatter = DateTimeFormatter | ||
.ofPattern("MMM dd, yyyy - HH:mm", Locale.US); | ||
|
||
LocalDateTime parsedLocalDateTime = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter); | ||
String localDateTimeString = formatter.format(parsedLocalDateTime); | ||
assertThat(localDateTimeString).isEqualTo("Nov 03, 2014 - 07:13"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
common/src/test/java/by/andd3dfx/java8/functionalinterface/ConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package by.andd3dfx.java8.functionalinterface; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class ConverterTest { | ||
|
||
@Test | ||
public void convert() { | ||
Converter<String, Integer> converter = (from) -> Integer.valueOf(from); | ||
Integer converted = converter.convert("123"); | ||
|
||
assertThat("Right number expected", converted, is(123)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
common/src/test/java/by/andd3dfx/java8/interfacebydefault/FormulaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package by.andd3dfx.java8.interfacebydefault; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class FormulaTest { | ||
|
||
private Formula formula; | ||
|
||
@Before | ||
public void setup() { | ||
formula = new Formula() { | ||
@Override | ||
public double calculate(int a) { | ||
return sqrt(a * 100); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void calculate() { | ||
assertThat("Square root of argument expected", formula.calculate(1), is(10.0)); | ||
} | ||
|
||
@Test | ||
public void sqrt() { | ||
assertThat("Square root of 16 expected", formula.sqrt(16), is(4.0)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
common/src/test/java/by/andd3dfx/java8/lambda/LambdaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package by.andd3dfx.java8.lambda; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class LambdaTest { | ||
|
||
private Lambda lambda; | ||
|
||
@Before | ||
public void setUp() { | ||
lambda = new Lambda(); | ||
} | ||
|
||
@Test | ||
public void usualApproach() { | ||
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); | ||
|
||
lambda.usualApproach(names); | ||
|
||
assertThat(names).isEqualTo(List.of("anna", "mike", "peter", "xenia")); | ||
} | ||
|
||
@Test | ||
public void lambda1() { | ||
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); | ||
|
||
lambda.lambda1(names); | ||
|
||
assertThat(names).isEqualTo(List.of("anna", "mike", "peter", "xenia")); | ||
} | ||
|
||
@Test | ||
public void lambda2() { | ||
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); | ||
|
||
lambda.lambda2(names); | ||
|
||
assertThat(names).isEqualTo(List.of("anna", "mike", "peter", "xenia")); | ||
} | ||
|
||
@Test | ||
public void lambda3() { | ||
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); | ||
|
||
lambda.lambda3(names); | ||
|
||
assertThat(names).isEqualTo(List.of("anna", "mike", "peter", "xenia")); | ||
} | ||
} |
Oops, something went wrong.