Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvariyath committed Feb 4, 2025
2 parents 679e67c + 489bf6e commit 25d3cdd
Show file tree
Hide file tree
Showing 89 changed files with 826 additions and 135 deletions.
4 changes: 2 additions & 2 deletions apache-kafka-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@

<properties>
<jna.version>5.7.0</jna.version>
<kafka.version>3.6.1</kafka.version>
<kafka.version>3.9.0</kafka.version>
<testcontainers-kafka.version>1.19.3</testcontainers-kafka.version>
<testcontainers-jupiter.version>1.19.3</testcontainers-jupiter.version>
<jackson.databind.version>2.15.2</jackson.databind.version>
Expand All @@ -130,4 +130,4 @@
<awaitility.version>3.0.0</awaitility.version>
</properties>

</project>
</project>

This file was deleted.

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;
};
}

}
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;
};
}

}
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);
}
}

}
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");
};
}

}
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;
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static com.baeldung.switchpatterns.ParenthesizedPatterns.*;
import static com.baeldung.switchpatterns.GuardedPatterns.*;

class ParenthesizedPatternsUnitTest {
class GuardedPatternsUnitTest {

@Test
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
Expand All @@ -17,24 +17,14 @@ void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
assertEquals(10d, getDoubleValueUsingIf("10"));
}

@Test
void givenIfImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
assertEquals(0d, getDoubleValueUsingIf("@10"));
}

@Test
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns(""));
assertEquals(0d, getDoubleValueUsingGuardedPatterns(""));
}

@Test
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
assertEquals(10d, getDoubleValueUsingParenthesizedPatterns("10"));
}

@Test
void givenPatternsImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns("@10"));
assertEquals(10d, getDoubleValueUsingGuardedPatterns("10"));
}

}
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"));
}

}
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'));
}

}
28 changes: 28 additions & 0 deletions core-java-modules/core-java-24/pom.xml
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>
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 -&gt; System.out.println("int is 1");
case int i when i &gt; 1 &amp;&amp; i &lt; 100 -&gt; System.out.println("int is greater than 1 and less than 100");
default -&gt; System.out.println("int is greater or equal to 100");
}
}
}
4 changes: 1 addition & 3 deletions core-java-modules/core-java-reflection-2/README.md
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)
4 changes: 4 additions & 0 deletions core-java-modules/core-java-reflection-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
- [Is Java Reflection Bad Practice?](https://www.baeldung.com/java-reflection-benefits-drawbacks)
- [Instantiate an Inner Class With Reflection in Java](https://www.baeldung.com/java-reflection-instantiate-inner-class)
- [Calling getClass() From a Static Context](https://www.baeldung.com/java-getclass-static-context)
- [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)
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
7 changes: 6 additions & 1 deletion core-java-modules/core-java-reflection-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
Expand Down Expand Up @@ -69,7 +74,7 @@
</build>

<properties>
<reflections.version>0.9.12</reflections.version>
<reflections.version>0.10.2</reflections.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
<spring.version>5.3.4</spring.version>
Expand Down
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;

Expand Down
9 changes: 3 additions & 6 deletions core-java-modules/core-java-reflection/README.md
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)
Loading

0 comments on commit 25d3cdd

Please sign in to comment.