Skip to content

Commit

Permalink
Java features (#86)
Browse files Browse the repository at this point in the history
* upgrading gradle

* string templates example

* more examples
  • Loading branch information
mageddo committed Apr 24, 2024
1 parent d2940a7 commit 8c014b0
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
12 changes: 6 additions & 6 deletions java-features/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ repositories {
mavenCentral()
}

sourceCompatibility = JavaVersion.VERSION_20
targetCompatibility = JavaVersion.VERSION_20
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
compileOnly 'org.projectlombok:lombok:1.18.32'
annotationProcessor 'org.projectlombok:lombok:1.18.32'
implementation('org.apache.commons:commons-lang3:3.12.0')
implementation('ch.qos.logback:logback-classic:1.4.7')

testCompileOnly 'org.projectlombok:lombok:1.18.28'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.28'
testCompileOnly 'org.projectlombok:lombok:1.18.32'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.32'
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
testImplementation("org.mockito:mockito-junit-jupiter:5.1.1")

Expand Down
2 changes: 1 addition & 1 deletion java-features/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-8.2-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 4 additions & 0 deletions java-features/src/main/java/com/mageddo/java21/Response.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mageddo.java21;

public record Response(String user, Integer order) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mageddo.java21.stringtemplates;

public class StringTemplatesMain {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mageddo.java21.structured_concurrency;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.function.Supplier;

import com.mageddo.java21.Response;

public class StructuredConcurrencyMain {
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println(handle());
}

static Response handle() throws ExecutionException, InterruptedException {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
Supplier<String> user = scope.fork(() -> findUser());
Supplier<Integer> order = scope.fork(() -> fetchOrder());

scope.join() // Join both subtasks
.throwIfFailed(); // ... and propagate errors

// Here, both subtasks have succeeded, so compose their results
return new Response(user.get(), order.get());
}
}

static Integer fetchOrder() {
return 46114;
}

static String findUser() {
return "Jose";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mageddo.java21.stringtemplates;

import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* https://openjdk.org/jeps/430
*/
class StringTemplatesTest {

@Test
void mustEvaluateString() {

// arrange

final var name = "John";
final var age = 23;
final var pair = Pair.of("color", "green");

// act
final String tpl = StringTemplate.STR."""
Hello \{name}, your age is \{age}, in the next year you will be \{age + 1}.
\{pair.getKey()}=\{pair.getValue()}
""";

// assert
assertEquals("""
Hello John, your age is 23, in the next year you will be 24.
color=green
""", tpl);
}
}

0 comments on commit 8c014b0

Please sign in to comment.