Skip to content

Commit

Permalink
modernize for Java 21
Browse files Browse the repository at this point in the history
  • Loading branch information
MBoegers committed Jul 23, 2023
1 parent b5f0b3f commit 8b440b7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/gradleCI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 19
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '19'
java-version: '21-ea'
distribution: 'temurin'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
uses: gradle/wrapper-validation-action@v1
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
uses: gradle/gradle-build-action@v2
with:
arguments: build
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
70 changes: 67 additions & 3 deletions src/main/java/io/github/mboegers/dop/invoice/MwStRechner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private PlainOOP() {

/**
* ermöglicht einen einheitlichen Zugriff
* Java 7 kompatibel
*/
public static double calculateMwSt(Kunde kunde, double wert) {
requireNonNull(kunde);
Expand All @@ -26,7 +27,7 @@ public static double calculateMwSt(Kunde kunde, double wert) {
} else if (kunde instanceof Businesskunde) {
return calculateMwSt((Businesskunde) kunde, wert);
} else {
throw new IllegalArgumentException("Typ %s nicht implementiert".formatted(kunde));
throw new IllegalArgumentException(String.format("Typ %s nicht implementiert", kunde));
}
}

Expand All @@ -47,6 +48,7 @@ private static double calculateMwSt(Businesskunde b, double wert) {

/**
* Verwendung JEP 394: Pattern Matching for instanceof https://openjdk.org/jeps/394
* Java 16 Final
*/
final class InstanceOfPattern {
private InstanceOfPattern() {
Expand All @@ -66,7 +68,8 @@ public static double calculateMwSt(Kunde kunde, double wert) {
}

/**
* Verwendung von JEP 433: Pattern Matching for switch (Fourth Preview) https://openjdk.org/jeps/433
* Verwendung von JEP 441: Pattern Matching for switch https://openjdk.org/jeps/441
* Java 21 Final
*/
final class SwichExpression {
private SwichExpression() {
Expand All @@ -85,7 +88,8 @@ public static double calculateMwSt(Kunde kunde, double wert) {
}

/**
* Verwendung von JEP 433: Pattern Matching for switch (Fourth Preview) https://openjdk.org/jeps/433 und when-Clause
* Verwendung von JEP 441: Pattern Matching for switch https://openjdk.org/jeps/441 und when-Clause
* Java 21 Final
*/
final class SwitchExpressionWhenClause {
private SwitchExpressionWhenClause() {
Expand All @@ -101,5 +105,65 @@ public static double calculateMwSt(Kunde kunde, double wert) {
}

}

/**
* Verwendung von JEP 440: Record Patterns https://openjdk.org/jeps/440 Dekonstruktion
* Java 21 Final
*/
final class SwitchExpressionWhenClauseDeconstruct {
private SwitchExpressionWhenClauseDeconstruct() {
}

public static double calculateMwSt(Kunde kunde, double wert) {
requireNonNull(kunde);
return switch (kunde) {
case Businesskunde(String name, String mail, boolean isVorsteuerAbzugsberechtigt)
when isVorsteuerAbzugsberechtigt -> 0.0d;
case Businesskunde b -> wert * 0.1d;
case Privatkunde p -> wert * 0.1d;
};
}

}

/**
* Verwendung von JEP 440: Record Patterns https://openjdk.org/jeps/440 Dekonstruktion mit Inference
* Java 21 Final
*/
final class SwitchExpressionWhenClauseDeconstructVar {
private SwitchExpressionWhenClauseDeconstructVar() {
}

public static double calculateMwSt(Kunde kunde, double wert) {
requireNonNull(kunde);
return switch (kunde) {
case Businesskunde(var name, var mail, var isVorsteuerAbzugsberechtigt)
when isVorsteuerAbzugsberechtigt -> 0.0d;
case Businesskunde b -> wert * 0.1d;
case Privatkunde p -> wert * 0.1d;
};
}

}

/**
* Verwendung von JEP 443: Unnamed Patterns and Variables (Preview) https://openjdk.org/jeps/443 Unnamed Variable Pattern
* Java 21 Preview
*/
final class SwitchExpressionWhenClauseUnnamed {
private SwitchExpressionWhenClauseUnnamed() {
}

public static double calculateMwSt(Kunde kunde, double wert) {
requireNonNull(kunde);
return switch (kunde) {
case Businesskunde(var _, var _, var isVorsteuerAbzugsberechtigt)
when isVorsteuerAbzugsberechtigt -> 0.0d;
case Businesskunde _ -> wert * 0.1d;
case Privatkunde _ -> wert * 0.1d;
};
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class CalculateMwStMethodProvider implements ArgumentsProvider {
MwStRechner.PlainOOP::calculateMwSt,
MwStRechner.InstanceOfPattern::calculateMwSt,
MwStRechner.SwichExpression::calculateMwSt,
MwStRechner.SwitchExpressionWhenClause::calculateMwSt);
MwStRechner.SwitchExpressionWhenClause::calculateMwSt,
MwStRechner.SwitchExpressionWhenClauseDeconstruct::calculateMwSt,
MwStRechner.SwitchExpressionWhenClauseDeconstructVar::calculateMwSt,
MwStRechner.SwitchExpressionWhenClauseUnnamed::calculateMwSt);

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
Expand Down

0 comments on commit 8b440b7

Please sign in to comment.