Skip to content

Commit 905a9b8

Browse files
Java 21 Programs
1 parent d159665 commit 905a9b8

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@
203203
<plugin>
204204
<groupId>org.apache.maven.plugins</groupId>
205205
<artifactId>maven-compiler-plugin</artifactId>
206-
<version>3.8.1</version>
206+
<version>3.11.0</version>
207207
<configuration>
208-
<source>17</source>
209-
<target>17</target>
208+
<source>21</source>
209+
<target>21</target>
210210
<annotationProcessorPaths>
211211
<path>
212212
<groupId>org.projectlombok</groupId>
213213
<artifactId>lombok</artifactId>
214-
<version>1.18.22</version>
214+
<version>1.18.30</version>
215215
</path>
216216
<path>
217217
<groupId>io.soabase.record-builder</groupId>

src/main/java/com/howtodoinjava/core/string/StartsWith.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.howtodoinjava.core.string;
22

3+
import java.util.regex.Pattern;
34
import org.apache.commons.lang3.ObjectUtils.Null;
5+
import org.apache.commons.lang3.StringUtils;
46
import org.junit.jupiter.api.Assertions;
57

68
public class StartsWith {
@@ -21,5 +23,33 @@ public static void main(String[] args) {
2123
Assertions.assertTrue(blogName.startsWith("h", 0));
2224
Assertions.assertFalse(blogName.startsWith("do", 0));
2325
Assertions.assertTrue(blogName.startsWith("do", 5));
26+
27+
String text = "The quick brown fox jumps over the lazy dog";
28+
29+
boolean result = StringUtils.startsWithAny(text,"The", "A", "An");
30+
System.out.println(result);
31+
32+
//3
33+
String[] prefixes = { "The", "A", "An" };
34+
String regex = "^(?:" + String.join("|", prefixes) + ").*";
35+
36+
if (Pattern.compile(regex).matcher(text).matches()) {
37+
System.out.println("The string starts with one of the specified values.");
38+
} else {
39+
System.out.println("The string does not start with any of the specified values.");
40+
}
41+
42+
//4
43+
for (String prefix : prefixes) {
44+
if (text.startsWith(prefix)) {
45+
System.out.println("The string starts with '" + prefix + "'.");
46+
break; // Exit the loop when a match is found
47+
}
48+
}
49+
50+
result = StringUtils.startsWithIgnoreCase(text, "the");
51+
52+
System.out.println(result); // true
53+
2454
}
2555
}

src/main/java/com/howtodoinjava/newFeatures/java21/ScopedValueTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public static void main(String[] args) {
2929
}
3030

3131
static void doSomething() {
32-
System.out.println("Scoped Value in doSomething(): " + CONTEXT.get());
32+
System.out.println(STR."Scoped Value in doSomething(): \{CONTEXT.get()}");
3333
}
3434

3535
static String doSomethingInChildThread() {
36-
System.out.println("Scoped Value in insideChildThread(): " + CONTEXT.get());
36+
System.out.println(STR."Scoped Value in insideChildThread(): \{CONTEXT.get()}");
3737
return CONTEXT.get();
3838
}
3939

src/main/java/com/howtodoinjava/newFeatures/java21/StructuredConcurrency.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public Response getClientById(Long id) {
4747

4848
System.out.println("Response is received from all workers...");
4949
//The subtasks have completed by now so process the result
50-
return new Response(accountDetailsFuture.get(),
51-
linkedAccountsFuture.get(),
50+
return new Response(accountDetailsFuture.get(), linkedAccountsFuture.get(),
5251
userDetailsFuture.get());
5352
} catch (InterruptedException e) {
5453
throw new RuntimeException(e);

src/main/java/com/howtodoinjava/newFeatures/java21/UnnamedPatterns.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@ public static void main(String[] args) {
99
Employee employee = new Employee(101L, "John Doe", LocalDate.of(1990, 5, 15), true, department);
1010

1111
//With unused pattern variables
12-
if (employee instanceof Employee(Long id, String name, LocalDate dateOfBirth, Boolean status,
13-
Department(Long depId, String depName, Boolean depStatus))) {
12+
if (employee instanceof Employee(
13+
Long id, String name, LocalDate dateOfBirth, Boolean status,
14+
Department(Long depId, String depName, Boolean depStatus)
15+
)) {
1416

1517
System.out.printf("Employee %d works in department %s.", id, depName);
1618
}
1719

1820
if (department instanceof Department(_, String name, _)) {
19-
System.out.println("Department Name is : " + name); // Prints 'Department Name is : HR'
21+
System.out.println(STR."Department Name is : \{name}"); // Prints 'Department Name is : HR'
2022
}
2123

2224
if (employee instanceof Employee(Long id, _, _, _, Department(_, String depName, _))) {
2325
System.out.printf("Employee %d works in department %s.", id, depName);
2426
}
2527

2628
if (employee instanceof Employee(Long id, _, _, _, _)) {
27-
System.out.println("Employee Id is : " + id); // Prints 'Employee Id is : 101'
29+
System.out.println(STR."Employee Id is : \{id}"); // Prints 'Employee Id is : 101'
2830
}
2931
}
3032
}
@@ -33,6 +35,7 @@ record Department(Long id, String name, Boolean status) {
3335
// Constructor and methods (if any)
3436
}
3537

36-
record Employee(Long id, String name, LocalDate dateOfBirth, Boolean status, Department department) {
38+
record Employee(Long id, String name, LocalDate dateOfBirth, Boolean status,
39+
Department department) {
3740
// Constructor and methods (if any)
3841
}

0 commit comments

Comments
 (0)