forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
89 changed files
with
826 additions
and
135 deletions.
There are no files selected for viewing
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
29 changes: 0 additions & 29 deletions
29
...modules/core-java-17/src/main/java/com/baeldung/switchpatterns/ParenthesizedPatterns.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
...-java-modules/core-java-21/src/main/java/com/baeldung/switchpatterns/GuardedPatterns.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,25 @@ | ||
package com.baeldung.switchpatterns; | ||
|
||
public class GuardedPatterns { | ||
|
||
static double getDoubleValueUsingIf(Object o) { | ||
return switch (o) { | ||
case String s -> { | ||
if (s.length() > 0) { | ||
yield Double.parseDouble(s); | ||
} else { | ||
yield 0d; | ||
} | ||
} | ||
default -> 0d; | ||
}; | ||
} | ||
|
||
static double getDoubleValueUsingGuardedPatterns(Object o) { | ||
return switch (o) { | ||
case String s when s.length() > 0 -> Double.parseDouble(s); | ||
default -> 0d; | ||
}; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...va-modules/core-java-21/src/main/java/com/baeldung/switchpatterns/HandlingNullValues.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 com.baeldung.switchpatterns; | ||
|
||
public class HandlingNullValues { | ||
|
||
static double getDoubleUsingSwitchNullCase(Object o) { | ||
return switch (o) { | ||
case String s -> Double.parseDouble(s); | ||
case null -> 0d; | ||
default -> 0d; | ||
}; | ||
} | ||
|
||
static double getDoubleUsingSwitchTotalType(Object o) { | ||
return switch (o) { | ||
case String s -> Double.parseDouble(s); | ||
case Object ob -> 0d; | ||
}; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...-java-modules/core-java-21/src/main/java/com/baeldung/switchpatterns/PatternMatching.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,14 @@ | ||
package com.baeldung.switchpatterns; | ||
|
||
public class PatternMatching { | ||
|
||
public static void main(String[] args) { | ||
Object o = args[0]; | ||
if (o instanceof String s) { | ||
System.out.printf("Object is a string %s", s); | ||
} else if(o instanceof Number n) { | ||
System.out.printf("Object is a number %n", n); | ||
} | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...-java-modules/core-java-21/src/main/java/com/baeldung/switchpatterns/SwitchStatement.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,14 @@ | ||
package com.baeldung.switchpatterns; | ||
|
||
public class SwitchStatement { | ||
|
||
public static void main(String[] args) { | ||
final String b = "B"; | ||
switch (args[0]) { | ||
case "A" -> System.out.println("Parameter is A"); | ||
case b -> System.out.println("Parameter is b"); | ||
default -> System.out.println("Parameter is unknown"); | ||
}; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
core-java-modules/core-java-21/src/main/java/com/baeldung/switchpatterns/TypePatterns.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,30 @@ | ||
package com.baeldung.switchpatterns; | ||
|
||
public class TypePatterns { | ||
|
||
static double getDoubleUsingIf(Object o) { | ||
double result; | ||
|
||
if (o instanceof Integer) { | ||
result = ((Integer) o).doubleValue(); | ||
} else if (o instanceof Float) { | ||
result = ((Float) o).doubleValue(); | ||
} else if (o instanceof String) { | ||
result = Double.parseDouble(((String) o)); | ||
} else { | ||
result = 0d; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static double getDoubleUsingSwitch(Object o) { | ||
return switch (o) { | ||
case Integer i -> i.doubleValue(); | ||
case Float f -> f.doubleValue(); | ||
case String s -> Double.parseDouble(s); | ||
default -> 0d; | ||
}; | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
...es/core-java-21/src/test/java/com/baeldung/switchpatterns/HandlingNullValuesUnitTest.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,25 @@ | ||
package com.baeldung.switchpatterns; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static com.baeldung.switchpatterns.HandlingNullValues.*; | ||
|
||
class HandlingNullValuesUnitTest { | ||
|
||
@Test | ||
void givenNullCaseInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingSwitchNullCase("10")); | ||
} | ||
|
||
@Test | ||
void givenTotalTypeInSwitch_whenUsingNullArgument_thenDoubleIsReturned() { | ||
assertEquals(0d, getDoubleUsingSwitchNullCase(null)); | ||
} | ||
|
||
@Test | ||
void givenTotalTypeInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingSwitchTotalType("10")); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...-modules/core-java-21/src/test/java/com/baeldung/switchpatterns/TypePatternsUnitTest.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,49 @@ | ||
package com.baeldung.switchpatterns; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static com.baeldung.switchpatterns.TypePatterns.*; | ||
|
||
class TypePatternsUnitTest { | ||
|
||
@Test | ||
void givenIfImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingIf(10)); | ||
} | ||
|
||
@Test | ||
void givenIfImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingIf(10.0f)); | ||
} | ||
|
||
@Test | ||
void givenIfImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingIf("10")); | ||
} | ||
|
||
@Test | ||
void givenIfImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() { | ||
assertEquals(0d, getDoubleUsingIf('c')); | ||
} | ||
|
||
@Test | ||
void givenSwitchImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingSwitch(10)); | ||
} | ||
|
||
@Test | ||
void givenSwitchImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingSwitch(10.0f)); | ||
} | ||
|
||
@Test | ||
void givenSwitchImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() { | ||
assertEquals(10d, getDoubleUsingSwitch("10")); | ||
} | ||
|
||
@Test | ||
void givenSwitchImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() { | ||
assertEquals(0d, getDoubleUsingSwitch('c')); | ||
} | ||
|
||
} |
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,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>core-java-24</artifactId> | ||
|
||
<parent> | ||
<groupId>com.baeldung.core-java-modules</groupId> | ||
<artifactId>core-java-modules</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>24</source> | ||
<target>24</target> | ||
<compilerArgs>--enable-preview</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
...es/core-java-24/src/main/java/com/baeldung/javafeatures/switchpatterns/SwitchPreview.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,14 @@ | ||
package com.baeldung.java.javafeatures; | ||
import java.util.Random; | ||
|
||
public class SwitchPreview { | ||
|
||
void primitiveTypePatternExample() { | ||
Random r=new Random(); | ||
switch (r.nextInt()) { | ||
case 1 -> System.out.println("int is 1"); | ||
case int i when i > 1 && i < 100 -> System.out.println("int is greater than 1 and less than 100"); | ||
default -> System.out.println("int is greater or equal to 100"); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,10 @@ | ||
### Relevant Articles: | ||
|
||
- [Reading the Value of ‘private’ Fields from a Different Class in Java](https://www.baeldung.com/java-reflection-read-private-field-value) | ||
- [Set Field Value With Reflection](https://www.baeldung.com/java-set-private-field-value) | ||
- [Void Type in Java](https://www.baeldung.com/java-void-type) | ||
- [Checking if a Method Is Static Using Reflection in Java](https://www.baeldung.com/java-check-method-is-static) | ||
- [Checking if a Java Class Is ‘Abstract’ Using Reflection](https://www.baeldung.com/java-reflection-is-class-abstract) | ||
- [Invoking a Private Method in Java](https://www.baeldung.com/java-call-private-method) | ||
- [Finding All Classes in a Java Package](https://www.baeldung.com/java-find-all-classes-in-package) | ||
- [Invoke a Static Method Using Java Reflection API](https://www.baeldung.com/java-invoke-static-method-reflection) | ||
- [What Is the JDK com.sun.proxy.$Proxy Class?](https://www.baeldung.com/jdk-com-sun-proxy) | ||
- [Unit Test Private Methods in Java](https://www.baeldung.com/java-unit-test-private-methods) | ||
- [Constructing Java Objects From Only the Class Name](https://www.baeldung.com/java-objects-make-using-class-name) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...urrentlyExecutedMethodFinderUnitTest.java → ...urrentlyExecutedMethodFinderUnitTest.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.java.currentmethod; | ||
package com.baeldung.currentmethod; | ||
|
||
import org.junit.Test; | ||
|
||
|
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,11 +1,8 @@ | ||
## Relevant Articles | ||
|
||
- [Void Type in Java](https://www.baeldung.com/java-void-type) | ||
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields) | ||
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection) | ||
- [Set Field Value With Reflection](https://www.baeldung.com/java-set-private-field-value) | ||
- [Finding All Classes in a Java Package](https://www.baeldung.com/java-find-all-classes-in-package) | ||
- [Unit Test Private Methods in Java](https://www.baeldung.com/java-unit-test-private-methods) | ||
- [Changing Annotation Parameters at Runtime](https://www.baeldung.com/java-reflection-change-annotation-params) | ||
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies) | ||
- [What Causes java.lang.reflect.InvocationTargetException?](https://www.baeldung.com/java-lang-reflect-invocationtargetexception) | ||
- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method) | ||
- [Getting Class Type From a String in Java](https://www.baeldung.com/java-get-class-object-from-string) | ||
- [Determine if a Class Implements an Interface in Java](https://www.baeldung.com/java-check-class-implements-interface) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.