First commit

This commit is contained in:
Andrea Cavalli 2021-03-01 21:04:06 +01:00
parent b75c29af23
commit 7830146b77
44 changed files with 4438 additions and 0 deletions

50
.github/workflows/maven-publish.yml vendored Normal file
View File

@ -0,0 +1,50 @@
# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
# For more information see: https://github.com/actions/setup-java#apache-maven-with-a-settings-path
name: Maven Package
on:
push:
pull_request:
schedule:
- cron: '0 0 * * 0' # weekly
jobs:
build:
strategy:
matrix:
include:
- { os: ubuntu-20.04, arch: "linux/amd64" }
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
with:
submodules: "recursive"
- name: Setup variables
shell: bash
run: |
# ====== Variables
export REVISION=${{ github.run_number }}
echo "REVISION=$REVISION" >> $GITHUB_ENV
- name: Set up JDK 15
if: github.ref == 'refs/heads/master'
uses: actions/setup-java@v1
with:
java-version: 15
server-id: mchv-release-distribution
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
env:
MAVEN_USERNAME: ${{ secrets.MCHV_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MCHV_TOKEN }}
- name: Deploy to Maven (Release)
if: github.ref == 'refs/heads/master'
shell: bash
run: |
echo "REVISION: $REVISION"
mvn -B -Drevision=${REVISION} clean deploy
env:
MAVEN_USERNAME: ${{ secrets.MCHV_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MCHV_TOKEN }}

13
README.md Normal file
View File

@ -0,0 +1,13 @@
Data generator
==============
Maven plugin to generate data classes from a .yaml definition file.
It can be also executed standalone.
The data is serializable and upgradable from any version to the latest.
The transformations between each version are defined in the .yaml file itself.
Supports custom (external) data types, custom data arrays, custom data optionals, interfaces with common getters / setters.
The serialized data is very lightweight: it serializes only the data, without any metadata or type specification, because it's all deducted on compile-time from the definitions file.

104
pom.xml Normal file
View File

@ -0,0 +1,104 @@
<?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>
<name>Data generator</name>
<groupId>it.cavallium</groupId>
<artifactId>data-generator</artifactId>
<version>0.9.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.26</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.12.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,50 @@
package it.cavallium.data.generator;
import java.lang.reflect.Method;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
public class CachedReflection {
private static ConcurrentHashMap<String, Class<?>> classes = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, Method> methods = new ConcurrentHashMap<>();
public static Class<?> classForName(String str) throws ClassNotFoundException {
try {
return classes.computeIfAbsent(str, (x) -> {
try {
return Class.forName(str);
} catch (ClassNotFoundException e) {
throw new CompletionException(e);
}
});
} catch (CompletionException ex) {
var cause = ex.getCause();
if (cause instanceof ClassNotFoundException) {
throw (ClassNotFoundException) cause;
}
throw ex;
}
}
public static Method getDeclaredMethod(Class<?> type, String name) throws NoSuchElementException, SecurityException {
try {
return methods.computeIfAbsent(type + "$$$" + name, (x) -> {
try {
return Stream.of(type.getDeclaredMethods()).filter(method -> method.getName().equals(name)).findAny().get();
} catch (NoSuchElementException | SecurityException e) {
throw new CompletionException(e);
}
});
} catch (CompletionException ex) {
var cause = ex.getCause();
if (cause instanceof NoSuchElementException) {
throw (NoSuchElementException) cause;
} else if (cause instanceof SecurityException) {
throw (SecurityException) cause;
}
throw ex;
}
}
}

View File

@ -0,0 +1,13 @@
package it.cavallium.data.generator;
public class CommonField {
public final String fieldName;
public final String fieldType;
public final boolean hasSetter;
public CommonField(String fieldName, String fieldType, boolean hasSetter) {
this.fieldName = fieldName;
this.fieldType = fieldType;
this.hasSetter = hasSetter;
}
}

View File

@ -0,0 +1,9 @@
package it.cavallium.data.generator;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public interface DataInitializer<T> {
@NotNull T initialize() throws IOException;
}

View File

@ -0,0 +1,23 @@
package it.cavallium.data.generator;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
public class DataInput3 extends DataInputStream {
private final byte[] buffer;
/**
* Creates a DataInputStream that uses the specified underlying InputStream.
*
* @param in the specified input stream
*/
public DataInput3(byte[] in) {
super(new ByteArrayInputStream(in));
this.buffer = in;
}
public byte[] asBytes() {
return buffer;
}
}

View File

@ -0,0 +1,13 @@
package it.cavallium.data.generator;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public interface DataSerializer<T> {
void serialize(DataOutput dataOutput, @NotNull T data) throws IOException;
@NotNull T deserialize(DataInput dataInput) throws IOException;
}

View File

@ -0,0 +1,9 @@
package it.cavallium.data.generator;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public interface DataUpgrader<T, U> {
@NotNull U upgrade(@NotNull T data) throws IOException;
}

View File

@ -0,0 +1,47 @@
package it.cavallium.data.generator;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
@Mojo(name = "run", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class MavenPlugin extends AbstractMojo {
@Parameter( required = true)
private File configPath;
@Parameter( required = true)
private String basePackageName;
/**
* @parameter default-value="${project}"
* @required
* @readonly
*/
@Parameter(defaultValue = "${project}", required = true, readonly = false)
MavenProject project;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
SourcesGenerator sourcesGenerator = SourcesGenerator.load(configPath.toPath());
Path genRecordsPath = project.getBasedir().getAbsoluteFile().toPath().resolve("target").resolve("generated-sources").resolve("database-classes");
FileUtils.deleteDirectory(genRecordsPath.resolve(Path.of(basePackageName.replace('.', File.separatorChar))).toFile());
Path outPath = genRecordsPath.resolve("java");
this.project.addCompileSourceRoot(outPath.toString());
sourcesGenerator.generateSources(basePackageName, outPath);
} catch (IOException e) {
throw new MojoExecutionException("Exception while generating classes", e);
}
getLog().info("Classes generated.");
}
}

View File

@ -0,0 +1,50 @@
package it.cavallium.data.generator;
public class Nullable<T> {
private static final long serialVersionUID = 1L;
private final T value;
public Nullable(T value) {
this.value = value;
}
public static <T> Nullable<T> of(T value) {
if (value == null) {
throw new NullPointerException();
} else {
return new Nullable<>(value);
}
}
public static <T> Nullable<T> ofNullable(T value) {
return new Nullable<>(value);
}
public static <T> Nullable<T> empty() {
return new Nullable<>(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
@org.jetbrains.annotations.NotNull
public T get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
@org.jetbrains.annotations.Nullable
public T getNullable() {
return value;
}
}

View File

@ -0,0 +1,47 @@
package it.cavallium.data.generator;
import com.squareup.javapoet.CodeBlock;
import java.util.List;
import java.util.Objects;
public class SerializeCodeBlockGenerator {
private final CodeBlock before;
private final CodeBlock after;
public SerializeCodeBlockGenerator(CodeBlock before, CodeBlock after) {
this.before = before;
this.after = after;
}
public CodeBlock generate(String method) {
return generate(CodeBlock.builder().add(method).build());
}
public CodeBlock generate(CodeBlock method) {
return CodeBlock.join(List.of(before, method, after), "");
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SerializeCodeBlockGenerator that = (SerializeCodeBlockGenerator) o;
return Objects.equals(before, that.before) && Objects.equals(after, that.after);
}
@Override
public int hashCode() {
return Objects.hash(before, after);
}
@Override
public String toString() {
return CodeBlock.builder().add("$[").add(generate(CodeBlock.builder().add("test.get()").build())).add(";\n$]").build().toString();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,178 @@
package it.cavallium.data.generator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SourcesGeneratorConfiguration {
public String currentVersion;
public Map<String, InterfaceDataConfiguration> interfacesData;
public Map<String, VersionConfiguration> versions;
public static class InterfaceDataConfiguration {
public Set<String> extendInterfaces = new HashSet<>();
public Map<String, String> commonData = new HashMap<>();
public Map<String, String> commonGetters = new HashMap<>();
}
public static class VersionConfiguration {
public DetailsConfiguration details;
public Map<String, Set<String>> superTypes;
public Map<String, CustomTypesConfiguration> customTypes;
public Map<String, ClassConfiguration> classes;
public List<VersionTransformation> transformations;
}
public static class DetailsConfiguration {
public String changelog;
}
public static class ClassConfiguration {
public String stringRepresenter;
public LinkedHashMap<String, String> data;
public String getStringRepresenter() {
return stringRepresenter;
}
public LinkedHashMap<String, String> getData() {
return data;
}
}
public static class VersionTransformation {
public MoveDataConfiguration moveData = null;
public RemoveDataConfiguration removeData = null;
public UpgradeDataConfiguration upgradeData = null;
public NewDataConfiguration newData = null;
void checkConsistency() {
int nonNullValues = 0;
if (moveData != null) nonNullValues++;
if (removeData != null) nonNullValues++;
if (upgradeData != null) nonNullValues++;
if (newData != null) nonNullValues++;
if (nonNullValues != 1) {
throw new IllegalArgumentException("Please fill only one transformation!");
}
}
public boolean isForClass(String type) {
checkConsistency();
if (moveData != null) {
return moveData.transformClass.equals(type);
}
if (removeData != null) {
return removeData.transformClass.equals(type);
}
if (upgradeData != null) {
return upgradeData.transformClass.equals(type);
}
if (newData != null) {
return newData.transformClass.equals(type);
}
throw new IllegalStateException();
}
public TransformationConfiguration getTransformation() {
checkConsistency();
if (moveData != null) {
return moveData;
}
if (removeData != null) {
return removeData;
}
if (upgradeData != null) {
return upgradeData;
}
if (newData != null) {
return newData;
}
throw new IllegalStateException();
}
}
public interface TransformationConfiguration {
String getTransformClass();
String getTransformName();
}
public static class MoveDataConfiguration implements TransformationConfiguration {
public String transformClass;
public String from;
public String to;
@Override
public String getTransformClass() {
return transformClass;
}
@Override
public String getTransformName() {
return "move-data";
}
}
public static class RemoveDataConfiguration implements TransformationConfiguration {
public String transformClass;
public String from;
@Override
public String getTransformClass() {
return transformClass;
}
@Override
public String getTransformName() {
return "remove-data";
}
}
public static class UpgradeDataConfiguration implements TransformationConfiguration {
public String transformClass;
public String from;
public String upgrader;
@Override
public String getTransformClass() {
return transformClass;
}
@Override
public String getTransformName() {
return "upgrade-data";
}
}
public static class NewDataConfiguration implements TransformationConfiguration {
public String transformClass;
public String to;
public String initializer;
@Override
public String getTransformClass() {
return transformClass;
}
@Override
public String getTransformName() {
return "new-data";
}
}
public static class CustomTypesConfiguration {
public String javaClass;
public String serializer;
}
}

View File

@ -0,0 +1,12 @@
package it.cavallium.data.generator;
import java.io.IOException;
import java.nio.file.Paths;
public class Standalone {
public static void main(String[] args) throws IOException {
SourcesGenerator sourcesGenerator = SourcesGenerator.load(Paths.get(args[0]));
sourcesGenerator.generateSources(args[1], Paths.get(args[2]));
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArrayStringSerializer implements DataSerializer<String[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull String[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeUTF(data[i]);
}
}
@NotNull
@Override
public String[] deserialize(DataInput dataInput) throws IOException {
var data = new String[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readUTF();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArraybooleanSerializer implements DataSerializer<boolean[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull boolean[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeBoolean(data[i]);
}
}
@NotNull
@Override
public boolean[] deserialize(DataInput dataInput) throws IOException {
var data = new boolean[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readBoolean();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArraybyteSerializer implements DataSerializer<byte[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull byte[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeByte(data[i]);
}
}
@NotNull
@Override
public byte[] deserialize(DataInput dataInput) throws IOException {
var data = new byte[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readByte();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArraycharSerializer implements DataSerializer<char[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull char[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeChar(data[i]);
}
}
@NotNull
@Override
public char[] deserialize(DataInput dataInput) throws IOException {
var data = new char[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readChar();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArraydoubleSerializer implements DataSerializer<double[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull double[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeDouble(data[i]);
}
}
@NotNull
@Override
public double[] deserialize(DataInput dataInput) throws IOException {
var data = new double[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readDouble();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArrayfloatSerializer implements DataSerializer<float[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull float[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeFloat(data[i]);
}
}
@NotNull
@Override
public float[] deserialize(DataInput dataInput) throws IOException {
var data = new float[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readFloat();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArrayintSerializer implements DataSerializer<int[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull int[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeInt(data[i]);
}
}
@NotNull
@Override
public int[] deserialize(DataInput dataInput) throws IOException {
var data = new int[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readInt();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArraylongSerializer implements DataSerializer<long[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull long[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeLong(data[i]);
}
}
@NotNull
@Override
public long[] deserialize(DataInput dataInput) throws IOException {
var data = new long[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readLong();
}
return data;
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class ArrayshortSerializer implements DataSerializer<short[]> {
@Override
public void serialize(DataOutput dataOutput, @NotNull short[] data) throws IOException {
dataOutput.writeInt(data.length);
for (int i = 0; i < data.length; i++) {
dataOutput.writeShort(data[i]);
}
}
@NotNull
@Override
public short[] deserialize(DataInput dataInput) throws IOException {
var data = new short[dataInput.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = dataInput.readShort();
}
return data;
}
}

View File

@ -0,0 +1,5 @@
package it.cavallium.data.generator.nativedata;
public interface IGenericNullable {
Object $getNullable();
}

View File

@ -0,0 +1,117 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class NullableString implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final String value;
public NullableString(String value) {
this.value = value;
}
public static NullableString of(@NotNull String value) {
if (value == null) {
throw new NullPointerException();
} else {
return new NullableString(value);
}
}
public static NullableString ofNullable(@Nullable String value) {
return new NullableString(value);
}
public static NullableString ofNullableBlank(@Nullable String value) {
if (value == null || value.isBlank()) {
return empty();
}
return new NullableString(value);
}
public static <T> NullableString empty() {
return new NullableString(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public boolean isContentful() {
return value != null && !value.isBlank();
}
@NotNull
public String get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public String orElse(String defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public String getNullable() {
return value;
}
@NotNull
public String getNullable(String defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public NullableString clone() {
if (value != null) {
return NullableString.of(value);
} else {
return NullableString.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (NullableString) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullableStringSerializer implements DataSerializer<NullableString> {
public static final NullableStringSerializer INSTANCE = new NullableStringSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull NullableString data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
String dataContent = data.get();
dataOutput.writeUTF(dataContent);
}
}
@NotNull
@Override
public NullableString deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return NullableString.empty();
} else {
return NullableString.of(dataInput.readUTF());
}
}
}

View File

@ -0,0 +1,101 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullableboolean implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final Boolean value;
public Nullableboolean(Boolean value) {
this.value = value;
}
public static Nullableboolean of(boolean value) {
return new Nullableboolean(value);
}
public static Nullableboolean ofNullable(@Nullable Boolean value) {
return new Nullableboolean(value);
}
public static <T> Nullableboolean empty() {
return new Nullableboolean(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public boolean get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public boolean orElse(boolean defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public Boolean getNullable() {
return value;
}
public boolean getNullable(boolean defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullableboolean clone() {
if (value != null) {
return Nullableboolean.of(value);
} else {
return Nullableboolean.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Nullableboolean that = (Nullableboolean) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullablebooleanSerializer implements DataSerializer<Nullableboolean> {
public static final NullablebooleanSerializer INSTANCE = new NullablebooleanSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullableboolean data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
boolean dataContent = data.get();
dataOutput.writeBoolean(dataContent);
}
}
@NotNull
@Override
public Nullableboolean deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullableboolean.empty();
} else {
return Nullableboolean.of(dataInput.readBoolean());
}
}
}

View File

@ -0,0 +1,104 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@EqualsAndHashCode
@ToString
public class Nullablebyte implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final Byte value;
public Nullablebyte(Byte value) {
this.value = value;
}
public static Nullablebyte of(byte value) {
return new Nullablebyte(value);
}
public static Nullablebyte ofNullable(@Nullable Byte value) {
return new Nullablebyte(value);
}
public static <T> Nullablebyte empty() {
return new Nullablebyte(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public byte get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public byte orElse(byte defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public Byte getNullable() {
return value;
}
public byte getNullable(byte defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullablebyte clone() {
if (value != null) {
return Nullablebyte.of(value);
} else {
return Nullablebyte.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Nullablebyte that = (Nullablebyte) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullablebyteSerializer implements DataSerializer<Nullablebyte> {
public static final NullablebyteSerializer INSTANCE = new NullablebyteSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullablebyte data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
byte dataContent = data.get();
dataOutput.writeByte(dataContent);
}
}
@NotNull
@Override
public Nullablebyte deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullablebyte.empty();
} else {
return Nullablebyte.of(dataInput.readByte());
}
}
}

View File

@ -0,0 +1,104 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@EqualsAndHashCode
@ToString
public class Nullablechar implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final Character value;
public Nullablechar(Character value) {
this.value = value;
}
public static Nullablechar of(char value) {
return new Nullablechar(value);
}
public static Nullablechar ofNullable(@Nullable Character value) {
return new Nullablechar(value);
}
public static <T> Nullablechar empty() {
return new Nullablechar(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public char get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public char orElse(char defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public Character getNullable() {
return value;
}
public char getNullable(char defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullablechar clone() {
if (value != null) {
return Nullablechar.of(value);
} else {
return Nullablechar.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (Nullablechar) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullablecharSerializer implements DataSerializer<Nullablechar> {
public static final NullablecharSerializer INSTANCE = new NullablecharSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullablechar data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
char dataContent = data.get();
dataOutput.writeChar(dataContent);
}
}
@NotNull
@Override
public Nullablechar deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullablechar.empty();
} else {
return Nullablechar.of(dataInput.readChar());
}
}
}

View File

@ -0,0 +1,95 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullabledouble implements Serializable {
private static final long serialVersionUID = 1L;
private final Double value;
public Nullabledouble(Double value) {
this.value = value;
}
public static Nullabledouble of(double value) {
return new Nullabledouble(value);
}
public static Nullabledouble ofNullable(@Nullable Double value) {
return new Nullabledouble(value);
}
public static <T> Nullabledouble empty() {
return new Nullabledouble(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public double get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public double orElse(double defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Nullable
public Double getNullable() {
return value;
}
public double getNullable(double defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullabledouble clone() {
if (value != null) {
return Nullabledouble.of(value);
} else {
return Nullabledouble.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (Nullabledouble) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullabledoubleSerializer implements DataSerializer<Nullabledouble> {
public static final NullabledoubleSerializer INSTANCE = new NullabledoubleSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullabledouble data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
double dataContent = data.get();
dataOutput.writeDouble(dataContent);
}
}
@NotNull
@Override
public Nullabledouble deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullabledouble.empty();
} else {
return Nullabledouble.of(dataInput.readDouble());
}
}
}

View File

@ -0,0 +1,100 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullablefloat implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final Float value;
public Nullablefloat(Float value) {
this.value = value;
}
public static Nullablefloat of(float value) {
return new Nullablefloat(value);
}
public static Nullablefloat ofNullable(@Nullable Float value) {
return new Nullablefloat(value);
}
public static <T> Nullablefloat empty() {
return new Nullablefloat(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public float get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public float orElse(float defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public Float getNullable() {
return value;
}
public float getNullable(float defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullablefloat clone() {
if (value != null) {
return Nullablefloat.of(value);
} else {
return Nullablefloat.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (Nullablefloat) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullablefloatSerializer implements DataSerializer<Nullablefloat> {
public static final NullablefloatSerializer INSTANCE = new NullablefloatSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullablefloat data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
float dataContent = data.get();
dataOutput.writeFloat(dataContent);
}
}
@NotNull
@Override
public Nullablefloat deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullablefloat.empty();
} else {
return Nullablefloat.of(dataInput.readFloat());
}
}
}

View File

@ -0,0 +1,100 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullableint implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final Integer value;
public Nullableint(Integer value) {
this.value = value;
}
public static Nullableint of(int value) {
return new Nullableint(value);
}
public static Nullableint ofNullable(@Nullable Integer value) {
return new Nullableint(value);
}
public static <T> Nullableint empty() {
return new Nullableint(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public int get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public int orElse(int defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public Integer getNullable() {
return value;
}
public int getNullable(int defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullableint clone() {
if (value != null) {
return Nullableint.of(value);
} else {
return Nullableint.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (Nullableint) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullableintSerializer implements DataSerializer<Nullableint> {
public static final NullableintSerializer INSTANCE = new NullableintSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullableint data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
int dataContent = data.get();
dataOutput.writeInt(dataContent);
}
}
@NotNull
@Override
public Nullableint deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullableint.empty();
} else {
return Nullableint.of(dataInput.readInt());
}
}
}

View File

@ -0,0 +1,100 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullablelong implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final Long value;
public Nullablelong(Long value) {
this.value = value;
}
public static Nullablelong of(long value) {
return new Nullablelong(value);
}
public static Nullablelong ofNullable(@Nullable Long value) {
return new Nullablelong(value);
}
public static <T> Nullablelong empty() {
return new Nullablelong(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public long get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public long orElse(long defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public Long getNullable() {
return value;
}
public long getNullable(long defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullablelong clone() {
if (value != null) {
return Nullablelong.of(value);
} else {
return Nullablelong.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (Nullablelong) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullablelongSerializer implements DataSerializer<Nullablelong> {
public static final NullablelongSerializer INSTANCE = new NullablelongSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullablelong data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
long dataContent = data.get();
dataOutput.writeLong(dataContent);
}
}
@NotNull
@Override
public Nullablelong deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullablelong.empty();
} else {
return Nullablelong.of(dataInput.readLong());
}
}
}

View File

@ -0,0 +1,100 @@
package it.cavallium.data.generator.nativedata;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullableshort implements Serializable, IGenericNullable {
private static final long serialVersionUID = 1L;
private final Short value;
public Nullableshort(Short value) {
this.value = value;
}
public static Nullableshort of(short value) {
return new Nullableshort(value);
}
public static Nullableshort ofNullable(@Nullable Short value) {
return new Nullableshort(value);
}
public static <T> Nullableshort empty() {
return new Nullableshort(null);
}
public boolean isEmpty() {
return value == null;
}
public boolean isPresent() {
return value != null;
}
public short get() {
if (value == null) {
throw new NullPointerException();
} else {
return value;
}
}
public short orElse(short defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
}
@Nullable
public Short getNullable() {
return value;
}
public short getNullable(short defaultValue) {
return value == null ? defaultValue : value;
}
@NotNull
@Override
public Nullableshort clone() {
if (value != null) {
return Nullableshort.of(value);
} else {
return Nullableshort.empty();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var that = (Nullableshort) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
if (value == null) return "null";
return "" + value;
}
}

View File

@ -0,0 +1,34 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class NullableshortSerializer implements DataSerializer<Nullableshort> {
public static final NullableshortSerializer INSTANCE = new NullableshortSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Nullableshort data) throws IOException {
if (data.isEmpty()) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
short dataContent = data.get();
dataOutput.writeShort(dataContent);
}
}
@NotNull
@Override
public Nullableshort deserialize(DataInput dataInput) throws IOException {
var isPresent = dataInput.readBoolean();
if (!isPresent) {
return Nullableshort.empty();
} else {
return Nullableshort.of(dataInput.readShort());
}
}
}

View File

@ -0,0 +1,28 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull;
public class StringSerializer implements DataSerializer<String> {
public static final StringSerializer INSTANCE = new StringSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull String data) throws IOException {
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
dataOutput.writeInt(bytes.length);
dataOutput.write(bytes);
}
@NotNull
@Override
public String deserialize(DataInput dataInput) throws IOException {
byte[] bytes = new byte[dataInput.readInt()];
dataInput.readFully(bytes);
return new String(bytes, StandardCharsets.UTF_8);
}
}