Merge branch 'eugenp:master' into master
This commit is contained in:
commit
119d52f961
@ -10,8 +10,4 @@
|
||||
- [Check if Two Strings Are Rotations of Each Other](https://www.baeldung.com/java-string-check-strings-rotations)
|
||||
- [Find the Largest Prime Under the Given Number in Java](https://www.baeldung.com/java-largest-prime-lower-threshold)
|
||||
- [Count the Number of Unique Digits in an Integer using Java](https://www.baeldung.com/java-int-count-unique-digits)
|
||||
- [Generate Juggler Sequence in Java](https://www.baeldung.com/java-generate-juggler-sequence)
|
||||
- [Finding the Parent of a Node in a Binary Search Tree with Java](https://www.baeldung.com/java-find-parent-node-binary-search-tree)
|
||||
- [Check if a Number Is a Happy Number in Java](https://www.baeldung.com/java-happy-sad-number-test)
|
||||
- [Find the Largest Number Possible After Removing k Digits of a Number](https://www.baeldung.com/java-find-largest-number-remove-k-digits)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)
|
||||
|
@ -1,2 +0,0 @@
|
||||
## Relevant Articles
|
||||
- [Implement Connect 4 Game with Java](https://www.baeldung.com/java-connect-4-game)
|
@ -1,3 +1,9 @@
|
||||
### Relevant Articles:
|
||||
- [Vigenère Cipher in Java](https://www.baeldung.com/java-vigenere-cipher)
|
||||
- [Merge Overlapping Intervals in a Java Collection](https://www.baeldung.com/java-collection-merge-overlapping-intervals)
|
||||
- [Generate Juggler Sequence in Java](https://www.baeldung.com/java-generate-juggler-sequence)
|
||||
- [Finding the Parent of a Node in a Binary Search Tree with Java](https://www.baeldung.com/java-find-parent-node-binary-search-tree)
|
||||
- [Check if a Number Is a Happy Number in Java](https://www.baeldung.com/java-happy-sad-number-test)
|
||||
- [Find the Largest Number Possible After Removing k Digits of a Number](https://www.baeldung.com/java-find-largest-number-remove-k-digits)
|
||||
- [Implement Connect 4 Game with Java](https://www.baeldung.com/java-connect-4-game)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-7)
|
@ -1,6 +1,6 @@
|
||||
package com.baeldung.algorithms.largestNumberRemovingK;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LargestNumberRemoveKDigits {
|
||||
public static int findLargestNumberUsingArithmetic(int num, int k) {
|
@ -5,7 +5,9 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class BinaryTreeParentNodeFinderUnitTest {
|
||||
|
@ -11,9 +11,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@ -98,7 +98,7 @@
|
||||
</property>
|
||||
<property>
|
||||
<name>spring.datasource.password</name>
|
||||
<value></value>
|
||||
<value>test</value>
|
||||
<!--<value>replace-with-your-password</value> -->
|
||||
</property>
|
||||
</appSettings>
|
||||
@ -113,6 +113,14 @@
|
||||
<!--</resource> -->
|
||||
<!--</resources> -->
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.4.0-b180830.0359</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.baeldung.springboot.azure;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
|
@ -9,9 +9,4 @@
|
||||
- [Round the Date in Java](https://www.baeldung.com/java-round-the-date)
|
||||
- [Representing Furthest Possible Date in Java](https://www.baeldung.com/java-date-represent-max)
|
||||
- [Retrieving Unix Time in Java](https://www.baeldung.com/java-retrieve-unix-time)
|
||||
- [Calculate Months Between Two Dates in Java](https://www.baeldung.com/java-months-difference-two-dates)
|
||||
- [Format LocalDate to ISO 8601 With T and Z](https://www.baeldung.com/java-format-localdate-iso-8601-t-z)
|
||||
- [Check if Two Date Ranges Overlap](https://www.baeldung.com/java-check-two-date-ranges-overlap)
|
||||
- [Difference between ZoneOffset.UTC and ZoneId.of(“UTC”)](https://www.baeldung.com/java-zoneoffset-utc-zoneid-of)
|
||||
- [Check if a Given Time Lies Between Two Times Regardless of Date](https://www.baeldung.com/java-check-between-two-times)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1) [[Next -->]](/core-java-modules/core-java-8-datetime-3)
|
||||
|
8
core-java-modules/core-java-8-datetime-3/README.md
Normal file
8
core-java-modules/core-java-8-datetime-3/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Calculate Months Between Two Dates in Java](https://www.baeldung.com/java-months-difference-two-dates)
|
||||
- [Format LocalDate to ISO 8601 With T and Z](https://www.baeldung.com/java-format-localdate-iso-8601-t-z)
|
||||
- [Check if Two Date Ranges Overlap](https://www.baeldung.com/java-check-two-date-ranges-overlap)
|
||||
- [Difference between ZoneOffset.UTC and ZoneId.of(“UTC”)](https://www.baeldung.com/java-zoneoffset-utc-zoneid-of)
|
||||
- [Check if a Given Time Lies Between Two Times Regardless of Date](https://www.baeldung.com/java-check-between-two-times)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-8-datetime-2)
|
55
core-java-modules/core-java-8-datetime-3/pom.xml
Normal file
55
core-java-modules/core-java-8-datetime-3/pom.xml
Normal file
@ -0,0 +1,55 @@
|
||||
<?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-8-datetime-3</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-8-datetime-3</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<joda-time.version>2.12.5</joda-time.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -1,11 +1,11 @@
|
||||
package com.baeldung.daterangeoverlap;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Interval;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class DateRangeOverlapChecker {
|
||||
|
||||
public static boolean isOverlapUsingCalendarAndDuration(Calendar start1, Calendar end1, Calendar start2, Calendar end2) {
|
@ -1,20 +1,16 @@
|
||||
package com.baeldung.localdatetoiso;
|
||||
|
||||
import org.apache.commons.lang3.time.FastDateFormat;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
import org.apache.commons.lang3.time.FastDateFormat;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class LocalDateToISO {
|
||||
public String formatUsingDateTimeFormatter(LocalDate localDate) {
|
@ -1,45 +1,45 @@
|
||||
package com.baeldung.checkiftimebetweentwotimes;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CheckIfTimeBetweenTwoTimesUnitTest {
|
||||
private LocalTime startTime = LocalTime.parse("09:00:00");
|
||||
private LocalTime endTime = LocalTime.parse("17:00:00");
|
||||
private LocalTime targetTime = LocalTime.parse("12:30:00");
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_whenUsingIsAfterIsBefore_thenTimeIsBetween() {
|
||||
assertTrue(!targetTime.isBefore(startTime) && !targetTime.isAfter(endTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_whenUsingCompareTo_thenTimeIsBetween() {
|
||||
assertTrue(targetTime.compareTo(startTime) >= 0 && targetTime.compareTo(endTime) <= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenUsingAfterBefore_thenTimeIsBetween() {
|
||||
Calendar startCalendar = Calendar.getInstance();
|
||||
startCalendar.set(Calendar.HOUR_OF_DAY, 9);
|
||||
startCalendar.set(Calendar.MINUTE, 0);
|
||||
Date startTime = startCalendar.getTime();
|
||||
|
||||
Calendar endCalendar = Calendar.getInstance();
|
||||
endCalendar.set(Calendar.HOUR_OF_DAY, 17);
|
||||
endCalendar.set(Calendar.MINUTE, 0);
|
||||
Date endTime = endCalendar.getTime();
|
||||
|
||||
Calendar targetCalendar = Calendar.getInstance();
|
||||
targetCalendar.set(Calendar.HOUR_OF_DAY, 12);
|
||||
targetCalendar.set(Calendar.MINUTE, 30);
|
||||
Date targetTime = targetCalendar.getTime();
|
||||
|
||||
assertTrue(!targetTime.before(startTime) && !targetTime.after(endTime));
|
||||
}
|
||||
package com.baeldung.checkiftimebetweentwotimes;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CheckIfTimeBetweenTwoTimesUnitTest {
|
||||
private LocalTime startTime = LocalTime.parse("09:00:00");
|
||||
private LocalTime endTime = LocalTime.parse("17:00:00");
|
||||
private LocalTime targetTime = LocalTime.parse("12:30:00");
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_whenUsingIsAfterIsBefore_thenTimeIsBetween() {
|
||||
assertTrue(!targetTime.isBefore(startTime) && !targetTime.isAfter(endTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_whenUsingCompareTo_thenTimeIsBetween() {
|
||||
assertTrue(targetTime.compareTo(startTime) >= 0 && targetTime.compareTo(endTime) <= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenUsingAfterBefore_thenTimeIsBetween() {
|
||||
Calendar startCalendar = Calendar.getInstance();
|
||||
startCalendar.set(Calendar.HOUR_OF_DAY, 9);
|
||||
startCalendar.set(Calendar.MINUTE, 0);
|
||||
Date startTime = startCalendar.getTime();
|
||||
|
||||
Calendar endCalendar = Calendar.getInstance();
|
||||
endCalendar.set(Calendar.HOUR_OF_DAY, 17);
|
||||
endCalendar.set(Calendar.MINUTE, 0);
|
||||
Date endTime = endCalendar.getTime();
|
||||
|
||||
Calendar targetCalendar = Calendar.getInstance();
|
||||
targetCalendar.set(Calendar.HOUR_OF_DAY, 12);
|
||||
targetCalendar.set(Calendar.MINUTE, 30);
|
||||
Date targetTime = targetCalendar.getTime();
|
||||
|
||||
assertTrue(!targetTime.before(startTime) && !targetTime.after(endTime));
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
package com.baeldung.daterangeoverlap;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DateRangeOverlapCheckerUnitTest {
|
||||
|
@ -1,13 +1,14 @@
|
||||
package com.baeldung.localdatetoiso;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class LocalDateToISOUnitTest {
|
||||
@Test
|
||||
void givenLocalDate_whenUsingDateTimeFormatter_thenISOFormat(){
|
||||
public void givenLocalDate_whenUsingDateTimeFormatter_thenISOFormat(){
|
||||
LocalDateToISO localDateToISO = new LocalDateToISO();
|
||||
LocalDate localDate = LocalDate.of(2023, 11, 6);
|
||||
|
||||
@ -17,7 +18,7 @@ public class LocalDateToISOUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLocalDate_whenUsingSimpleDateFormat_thenISOFormat(){
|
||||
public void givenLocalDate_whenUsingSimpleDateFormat_thenISOFormat(){
|
||||
LocalDateToISO localDateToISO = new LocalDateToISO();
|
||||
LocalDate localDate = LocalDate.of(2023, 11, 6);
|
||||
|
||||
@ -27,17 +28,18 @@ public class LocalDateToISOUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLocalDate_whenUsingJodaTime_thenISOFormat() {
|
||||
public void givenLocalDate_whenUsingJodaTime_thenISOFormat() {
|
||||
LocalDateToISO localDateToISO = new LocalDateToISO();
|
||||
org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(2023, 11, 6);
|
||||
|
||||
String expected = "2023-11-06T00:00:00.000Z";
|
||||
String actual = localDateToISO.formatUsingJodaTime(localDate);
|
||||
assertEquals(expected, actual);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLocalDate_whenUsingApacheCommonsLang_thenISOFormat() {
|
||||
public void givenLocalDate_whenUsingApacheCommonsLang_thenISOFormat() {
|
||||
LocalDateToISO localDateToISO = new LocalDateToISO();
|
||||
LocalDate localDate = LocalDate.of(2023, 11, 6);
|
||||
|
@ -1,25 +1,25 @@
|
||||
package com.baeldung.zoneoffsetandzoneidof;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ZoneOffSetAndZoneIdOfUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenOffsetDateTimeWithUTCZoneOffset_thenOffsetShouldBeUTC() {
|
||||
OffsetDateTime dateTimeWithOffset = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
assertEquals(dateTimeWithOffset.getOffset(), ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZonedDateTimeWithUTCZoneId_thenZoneShouldBeUTC() {
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
|
||||
assertEquals(zonedDateTime.getZone(), ZoneId.of("UTC"));
|
||||
}
|
||||
}
|
||||
package com.baeldung.zoneoffsetandzoneidof;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ZoneOffSetAndZoneIdOfUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenOffsetDateTimeWithUTCZoneOffset_thenOffsetShouldBeUTC() {
|
||||
OffsetDateTime dateTimeWithOffset = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
assertEquals(dateTimeWithOffset.getOffset(), ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZonedDateTimeWithUTCZoneId_thenZoneShouldBeUTC() {
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
|
||||
assertEquals(zonedDateTime.getZone(), ZoneId.of("UTC"));
|
||||
}
|
||||
}
|
@ -12,3 +12,4 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Read Input Character-by-Character in Java](https://www.baeldung.com/java-read-input-character)
|
||||
- [Difference Between flush() and close() in Java FileWriter](https://www.baeldung.com/java-filewriter-flush-vs-close)
|
||||
- [Get a Path to a Resource in a Java JAR File](https://www.baeldung.com/java-get-path-resource-jar)
|
||||
- [Java InputStream vs. InputStreamReader](https://www.baeldung.com/java-inputstream-vs-inputstreamreader)
|
||||
|
@ -13,4 +13,3 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader)
|
||||
- [Read Multiple Inputs on the Same Line in Java](https://www.baeldung.com/java-read-multiple-inputs-same-line)
|
||||
- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file)
|
||||
- [Java InputStream vs. InputStreamReader](https://www.baeldung.com/java-inputstream-vs-inputstreamreader)
|
||||
|
@ -12,6 +12,13 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven</id>
|
||||
<url>https://maven.openimaj.org/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.imagej</groupId>
|
||||
|
@ -0,0 +1,5 @@
|
||||
package com.baeldung.gson.polymorphic;
|
||||
|
||||
public interface Shape {
|
||||
double getArea();
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.gson.polymorphic;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ShapeTypeAdapter implements JsonSerializer<Shape>, JsonDeserializer<Shape> {
|
||||
@Override
|
||||
public JsonElement serialize(Shape shape, Type type, JsonSerializationContext context) {
|
||||
JsonElement elem = new Gson().toJsonTree(shape);
|
||||
elem.getAsJsonObject().addProperty("type", shape.getClass().getName());
|
||||
return elem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Shape deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
String typeName = jsonObject.get("type").getAsString();
|
||||
|
||||
try {
|
||||
Class<? extends Shape> cls = (Class<? extends Shape>) Class.forName(typeName);
|
||||
return new Gson().fromJson(json, cls);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new JsonParseException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
package com.baeldung.gson.polymorphic;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TypeAdapterUnitTest {
|
||||
@Test
|
||||
void testSerialize() {
|
||||
List<Shape> shapes = Arrays.asList(
|
||||
new Circle(4d),
|
||||
new Square(5d)
|
||||
);
|
||||
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeHierarchyAdapter(Shape.class, new ShapeTypeAdapter());
|
||||
Gson gson = builder.create();
|
||||
|
||||
String json = gson.toJson(shapes);
|
||||
|
||||
assertEquals("[" +
|
||||
"{" +
|
||||
"\"radius\":4.0," +
|
||||
"\"area\":50.26548245743669," +
|
||||
"\"type\":\"com.baeldung.gson.polymorphic.TypeAdapterUnitTest$Circle\"" +
|
||||
"},{" +
|
||||
"\"side\":5.0," +
|
||||
"\"area\":25.0," +
|
||||
"\"type\":\"com.baeldung.gson.polymorphic.TypeAdapterUnitTest$Square\"" +
|
||||
"}]", json);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testDeserializeWrapper() {
|
||||
List<Shape> shapes = Arrays.asList(
|
||||
new Circle(4d),
|
||||
new Square(5d)
|
||||
);
|
||||
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeHierarchyAdapter(Shape.class, new ShapeTypeAdapter());
|
||||
Gson gson = builder.create();
|
||||
|
||||
String json = gson.toJson(shapes);
|
||||
|
||||
Type collectionType = new TypeToken<List<Shape>>(){}.getType();
|
||||
List<Shape> result = gson.fromJson(json, collectionType);
|
||||
|
||||
assertEquals(shapes, result);
|
||||
}
|
||||
|
||||
private static class Square implements Shape {
|
||||
private final double side;
|
||||
private final double area;
|
||||
|
||||
public Square(double side) {
|
||||
this.side = side;
|
||||
this.area = side * side;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Square square = (Square) o;
|
||||
return Double.compare(square.side, side) == 0 && Double.compare(square.area, area) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(side, area);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Circle implements Shape {
|
||||
private final double radius;
|
||||
|
||||
private final double area;
|
||||
|
||||
public Circle(double radius) {
|
||||
this.radius = radius;
|
||||
this.area = Math.PI * radius * radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Circle circle = (Circle) o;
|
||||
return Double.compare(circle.radius, radius) == 0 && Double.compare(circle.area, area) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(radius, area);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.baeldung.gson.polymorphic;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TypeFieldUnitTest {
|
||||
@Test
|
||||
void testSerialize() {
|
||||
List<Shape> shapes = Arrays.asList(
|
||||
new Circle(4d),
|
||||
new Square(5d)
|
||||
);
|
||||
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(shapes);
|
||||
|
||||
assertEquals("[" +
|
||||
"{" +
|
||||
"\"type\":\"circle\"," +
|
||||
"\"radius\":4.0," +
|
||||
"\"area\":50.26548245743669" +
|
||||
"},{" +
|
||||
"\"type\":\"square\"," +
|
||||
"\"side\":5.0," +
|
||||
"\"area\":25.0" +
|
||||
"}]", json);
|
||||
}
|
||||
|
||||
private static class Square implements Shape {
|
||||
private final String type = "square";
|
||||
private final double side;
|
||||
private final double area;
|
||||
|
||||
public Square(double side) {
|
||||
this.side = side;
|
||||
this.area = side * side;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Circle implements Shape {
|
||||
private final String type = "circle";
|
||||
private final double radius;
|
||||
|
||||
private final double area;
|
||||
|
||||
public Circle(double radius) {
|
||||
this.radius = radius;
|
||||
this.area = Math.PI * radius * radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
package com.baeldung.gson.polymorphic;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class WrapperUnitTest {
|
||||
@Test
|
||||
void testSerializeWrapper() {
|
||||
List<Wrapper> shapes = Arrays.asList(
|
||||
new Wrapper(new Circle(4d)),
|
||||
new Wrapper(new Square(5d))
|
||||
);
|
||||
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(shapes);
|
||||
|
||||
assertEquals("[" +
|
||||
"{" +
|
||||
"\"circle\":{" +
|
||||
"\"radius\":4.0," +
|
||||
"\"area\":50.26548245743669" +
|
||||
"}" +
|
||||
"},{" +
|
||||
"\"square\":{" +
|
||||
"\"side\":5.0," +
|
||||
"\"area\":25.0" +
|
||||
"}" +
|
||||
"}]", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeserializeWrapper() {
|
||||
List<Wrapper> shapes = Arrays.asList(
|
||||
new Wrapper(new Circle(4d)),
|
||||
new Wrapper(new Square(5d))
|
||||
);
|
||||
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(shapes);
|
||||
|
||||
Type collectionType = new TypeToken<List<Wrapper>>(){}.getType();
|
||||
List<Wrapper> result = gson.fromJson(json, collectionType);
|
||||
|
||||
assertEquals(shapes, result);
|
||||
}
|
||||
|
||||
private static class Square implements Shape {
|
||||
private final double side;
|
||||
private final double area;
|
||||
|
||||
public Square(double side) {
|
||||
this.side = side;
|
||||
this.area = side * side;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Square square = (Square) o;
|
||||
return Double.compare(square.side, side) == 0 && Double.compare(square.area, area) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(side, area);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Circle implements Shape {
|
||||
private final double radius;
|
||||
|
||||
private final double area;
|
||||
|
||||
public Circle(double radius) {
|
||||
this.radius = radius;
|
||||
this.area = Math.PI * radius * radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Circle circle = (Circle) o;
|
||||
return Double.compare(circle.radius, radius) == 0 && Double.compare(circle.area, area) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(radius, area);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Wrapper {
|
||||
private final Circle circle;
|
||||
private final Square square;
|
||||
|
||||
public Wrapper(Circle circle) {
|
||||
this.circle = circle;
|
||||
this.square = null;
|
||||
}
|
||||
|
||||
public Wrapper(Square square) {
|
||||
this.square = square;
|
||||
this.circle = null;
|
||||
}
|
||||
|
||||
public Circle getCircle() {
|
||||
return circle;
|
||||
}
|
||||
|
||||
public Square getSquare() {
|
||||
return square;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Wrapper wrapper = (Wrapper) o;
|
||||
return Objects.equals(circle, wrapper.circle) && Objects.equals(square, wrapper.square);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(circle, square);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -36,7 +36,7 @@
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.ws</groupId>
|
||||
<artifactId>jaxws-ri</artifactId>
|
||||
<version>2.3.0</version>
|
||||
<version>${jaxws.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@ -46,7 +46,7 @@
|
||||
<plugin>
|
||||
<groupId>com.sun.xml.ws</groupId>
|
||||
<artifactId>jaxws-maven-plugin</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<version>${jaxws.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@ -73,4 +73,8 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jaxws.version>4.0.2</jaxws.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -14,6 +14,13 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>wso2-https</id>
|
||||
<url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@ -40,7 +47,7 @@
|
||||
|
||||
<properties>
|
||||
<microservice.mainClass>com.baeldung.msf4j.msf4jintro.Application</microservice.mainClass>
|
||||
<msf4j.version>2.6.3</msf4j.version>
|
||||
<msf4j.version>2.8.11</msf4j.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -9,10 +9,9 @@
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
|
@ -9,10 +9,9 @@
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -11,10 +11,9 @@
|
||||
|
||||
<!-- "sam build" needs all the dependencies including its parent project. Hence, we cannot use parent-boot in this case -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.1.5</version>
|
||||
<relativePath/>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -9,10 +9,9 @@
|
||||
<description>Module For Spring Boot Basic Customization 3</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -10,10 +10,9 @@
|
||||
<description>spring-boot-data-3</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -10,10 +10,9 @@
|
||||
<description>Spring Boot Todo Application with Groovy</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -6,10 +6,9 @@
|
||||
<artifactId>spring-boot-libraries-3</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -10,10 +10,9 @@
|
||||
<description>Module For Spring Boot Swagger</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -16,6 +16,13 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jboss-https</id>
|
||||
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
Loading…
x
Reference in New Issue
Block a user