Initial commit

This commit is contained in:
Piotr Dec 2024-03-16 12:51:44 +01:00
commit 159ce68dec
Signed by: stawros
GPG key ID: F89F27AD8F881A91
21 changed files with 924 additions and 0 deletions

28
hazelcast/pom.xml Normal file
View file

@ -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>
<parent>
<groupId>eu.ztsh.lib</groupId>
<artifactId>time-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>eu.ztsh.lib.time</groupId>
<artifactId>hazelcast</artifactId>
<dependencies>
<dependency>
<groupId>eu.ztsh.lib.time</groupId>
<artifactId>time</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

13
hazelcast/readme.md Normal file
View file

@ -0,0 +1,13 @@
# Hazelcast serializer
Provides custom serializer for `TimeRange`.
## Usage
In `hazelcast.yaml` just add
```yaml
hazelcast:
serialization:
serializers:
- type-class: eu.ztsh.lib.time.TimeRange
class-name: eu.ztsh.lib.time.hazelcast.TimeRangeSerializer
```

View file

@ -0,0 +1,33 @@
package eu.ztsh.lib.time.hazelcast;
import com.hazelcast.nio.serialization.ByteArraySerializer;
import eu.ztsh.lib.time.TimeRange;
import java.io.IOException;
import java.time.LocalDate;
import static eu.ztsh.lib.time.Formatters.yearMonthDay;
public class TimeRangeSerializer implements ByteArraySerializer<TimeRange> {
@Override
public byte[] write(TimeRange timeRange) throws IOException {
return (yearMonthDay.format(timeRange.from()) + ":" + yearMonthDay.format(timeRange.to())).getBytes();
}
@Override
public TimeRange read(byte[] bytes) throws IOException {
var stringData = new String(bytes).split(":");
return new TimeRange(parse(stringData[0]), parse(stringData[1]));
}
private static LocalDate parse(String input) {
return LocalDate.parse(input, yearMonthDay);
}
@Override
public int getTypeId() {
return 150;
}
}

View file

@ -0,0 +1,35 @@
package eu.ztsh.lib.time.hazelcast;
import eu.ztsh.lib.time.TimeRange;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat;
class TimeRangeSerializerTest {
private final LocalDate date1 = LocalDate.of(2023, 10, 10);
private final LocalDate date2 = LocalDate.of(2023, 10, 11);
private final LocalDate date3 = LocalDate.of(2023, 10, 12);
private final TimeRangeSerializer serializer = new TimeRangeSerializer();
@Test
public void identityTest() throws IOException {
var range1 = new TimeRange(date1, date2);
var range2 = serializer.read(serializer.write(range1));
assertThat(range2).isEqualTo(range1);
}
@Test
public void nonIdentityTest() throws IOException {
var range1 = new TimeRange(date1, date2);
var range2 = new TimeRange(date1, date3);
var range3 = serializer.read(serializer.write(range1));
assertThat(range1).isNotEqualTo(range2);
assertThat(range3).isNotEqualTo(range2);
}
}