Add the "or" method

This commit is contained in:
Andrea Cavalli 2022-05-05 15:33:11 +02:00
parent d428e7acb5
commit c3564e049b
13 changed files with 424 additions and 70 deletions

View File

@ -0,0 +1,22 @@
package it.cavallium.data.generator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface NativeNullable<T> {
boolean isEmpty();
boolean isPresent();
@NotNull
T orElse(@NotNull T defaultValue);
@NotNull NativeNullable<T> or(@NotNull NativeNullable<? extends T> fallback);
@Nullable
T getNullable();
@Nullable
T getNullable(@Nullable T defaultValue);
}

View File

@ -1,57 +0,0 @@
package it.cavallium.data.generator;
public class Nullable<T> {
private static final long serialVersionUID = 1L;
private static final Nullable<?> NULL = new Nullable<>(null);
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);
}
}
@SuppressWarnings("unchecked")
public static <T> Nullable<T> ofNullable(T value) {
if (value == null) {
return (Nullable<T>) NULL;
} else {
return new Nullable<>(value);
}
}
@SuppressWarnings("unchecked")
public static <T> Nullable<T> empty() {
return (Nullable<T>) 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,9 @@
package it.cavallium.data.generator;
import org.jetbrains.annotations.NotNull;
public interface TypedNullable<T> extends NativeNullable<T> {
@NotNull
T get() throws NullPointerException;
}

View File

@ -1,12 +1,14 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import it.cavallium.data.generator.TypedNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class NullableInt52 implements Serializable, IGenericNullable {
public class NullableInt52 implements Serializable, IGenericNullable, TypedNullable<Int52> {
@Serial
private static final long serialVersionUID = 1L;
@ -42,14 +44,17 @@ public class NullableInt52 implements Serializable, IGenericNullable {
return NULL;
}
@Override
public boolean isEmpty() {
return value == null;
}
@Override
public boolean isPresent() {
return value != null;
}
@Override
@NotNull
public Int52 get() {
if (value == null) {
@ -59,7 +64,8 @@ public class NullableInt52 implements Serializable, IGenericNullable {
}
}
public Int52 orElse(Int52 defaultValue) {
@Override
public @NotNull Int52 orElse(@NotNull Int52 defaultValue) {
if (value == null) {
return defaultValue;
} else {
@ -67,6 +73,28 @@ public class NullableInt52 implements Serializable, IGenericNullable {
}
}
@Override
public @NotNull NullableInt52 or(@NotNull NativeNullable<? extends Int52> fallback) {
if (value == null) {
if (fallback.getClass() == NullableInt52.class) {
return (NullableInt52) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public NullableInt52 or(NullableInt52 fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
@Override
public Object $getNullable() {
return this.getNullable();
@ -77,6 +105,7 @@ public class NullableInt52 implements Serializable, IGenericNullable {
return value;
}
@Override
@Nullable
public Int52 getNullable(Int52 defaultValue) {
return value == null ? defaultValue : value;

View File

@ -1,12 +1,14 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import it.cavallium.data.generator.TypedNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class NullableString implements Serializable, IGenericNullable {
public class NullableString implements Serializable, IGenericNullable, TypedNullable<String> {
@Serial
private static final long serialVersionUID = 1L;
@ -46,10 +48,12 @@ public class NullableString implements Serializable, IGenericNullable {
return NULL;
}
@Override
public boolean isEmpty() {
return value == null;
}
@Override
public boolean isPresent() {
return value != null;
}
@ -58,6 +62,11 @@ public class NullableString implements Serializable, IGenericNullable {
return value != null && !value.isBlank();
}
public boolean isBlank() {
return value == null || value.isBlank();
}
@Override
@NotNull
public String get() {
if (value == null) {
@ -67,7 +76,9 @@ public class NullableString implements Serializable, IGenericNullable {
}
}
public String orElse(String defaultValue) {
@Override
@NotNull
public String orElse(@NotNull String defaultValue) {
if (value == null) {
return defaultValue;
} else {
@ -75,6 +86,49 @@ public class NullableString implements Serializable, IGenericNullable {
}
}
@Override
public @NotNull NullableString or(@NotNull NativeNullable<? extends String> fallback) {
if (value == null) {
if (fallback.getClass() == NullableString.class) {
return (NullableString) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public NullableString or(NullableString fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public @NotNull NullableString orIfBlank(@NotNull NativeNullable<? extends String> fallback) {
if (isBlank()) {
if (fallback.getClass() == NullableString.class) {
return (NullableString) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public NullableString orIfBlank(NullableString fallback) {
if (isBlank()) {
return fallback;
} else {
return this;
}
}
@Override
public String $getNullable() {
return this.getNullable();
@ -85,7 +139,8 @@ public class NullableString implements Serializable, IGenericNullable {
return value;
}
@NotNull
@Override
@Nullable
public String getNullable(String defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullableboolean implements Serializable, IGenericNullable {
public class Nullableboolean implements Serializable, IGenericNullable, NativeNullable<Boolean> {
@Serial
private static final long serialVersionUID = 1L;
@ -42,6 +43,37 @@ public class Nullableboolean implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Boolean orElse(@NotNull Boolean defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullableboolean or(@NotNull NativeNullable<? extends Boolean> fallback) {
if (value == null) {
if (fallback.getClass() == Nullableboolean.class) {
return (Nullableboolean) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullableboolean or(Nullableboolean fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public boolean get() {
if (value == null) {
throw new NullPointerException();
@ -68,6 +100,11 @@ public class Nullableboolean implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Boolean getNullable(@Nullable Boolean defaultValue) {
return value != null ? value : defaultValue;
}
public boolean getNullable(boolean defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullablebyte implements Serializable, IGenericNullable {
public class Nullablebyte implements Serializable, IGenericNullable, NativeNullable<Byte> {
@Serial
private static final long serialVersionUID = 1L;
@ -50,6 +51,37 @@ public class Nullablebyte implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Byte orElse(@NotNull Byte defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullablebyte or(@NotNull NativeNullable<? extends Byte> fallback) {
if (value == null) {
if (fallback.getClass() == Nullablebyte.class) {
return (Nullablebyte) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullablebyte or(Nullablebyte fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public byte get() {
if (value == null) {
throw new NullPointerException();
@ -76,6 +108,11 @@ public class Nullablebyte implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Byte getNullable(@Nullable Byte defaultValue) {
return value != null ? value : defaultValue;
}
public byte getNullable(byte defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullablechar implements Serializable, IGenericNullable {
public class Nullablechar implements Serializable, IGenericNullable, NativeNullable<Character> {
@Serial
private static final long serialVersionUID = 1L;
@ -42,6 +43,37 @@ public class Nullablechar implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Character orElse(@NotNull Character defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullablechar or(@NotNull NativeNullable<? extends Character> fallback) {
if (value == null) {
if (fallback.getClass() == Nullablechar.class) {
return (Nullablechar) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullablechar or(Nullablechar fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public char get() {
if (value == null) {
throw new NullPointerException();
@ -68,6 +100,11 @@ public class Nullablechar implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Character getNullable(@Nullable Character defaultValue) {
return value != null ? value : defaultValue;
}
public char getNullable(char defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullabledouble implements Serializable, IGenericNullable {
public class Nullabledouble implements Serializable, IGenericNullable, NativeNullable<Double> {
@Serial
private static final long serialVersionUID = 1L;
@ -50,6 +51,37 @@ public class Nullabledouble implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Double orElse(@NotNull Double defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullabledouble or(@NotNull NativeNullable<? extends Double> fallback) {
if (value == null) {
if (fallback.getClass() == Nullabledouble.class) {
return (Nullabledouble) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullabledouble or(Nullabledouble fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public double get() {
if (value == null) {
throw new NullPointerException();
@ -76,6 +108,11 @@ public class Nullabledouble implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Double getNullable(@Nullable Double defaultValue) {
return value == null ? defaultValue : value;
}
public double getNullable(double defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullablefloat implements Serializable, IGenericNullable {
public class Nullablefloat implements Serializable, IGenericNullable, NativeNullable<Float> {
@Serial
private static final long serialVersionUID = 1L;
@ -50,6 +51,37 @@ public class Nullablefloat implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Float orElse(@NotNull Float defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullablefloat or(@NotNull NativeNullable<? extends Float> fallback) {
if (value == null) {
if (fallback.getClass() == Nullablefloat.class) {
return (Nullablefloat) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullablefloat or(Nullablefloat fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public float get() {
if (value == null) {
throw new NullPointerException();
@ -76,6 +108,11 @@ public class Nullablefloat implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Float getNullable(@Nullable Float defaultValue) {
return value == null ? defaultValue : value;
}
public float getNullable(float defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullableint implements Serializable, IGenericNullable {
public class Nullableint implements Serializable, IGenericNullable, NativeNullable<Integer> {
@Serial
private static final long serialVersionUID = 1L;
@ -50,6 +51,37 @@ public class Nullableint implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Integer orElse(@NotNull Integer defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullableint or(@NotNull NativeNullable<? extends Integer> fallback) {
if (value == null) {
if (fallback.getClass() == Nullableint.class) {
return (Nullableint) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullableint or(Nullableint fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public int get() {
if (value == null) {
throw new NullPointerException();
@ -76,6 +108,11 @@ public class Nullableint implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Integer getNullable(@Nullable Integer defaultValue) {
return value == null ? defaultValue : value;
}
public int getNullable(int defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullablelong implements Serializable, IGenericNullable {
public class Nullablelong implements Serializable, IGenericNullable, NativeNullable<Long> {
@Serial
private static final long serialVersionUID = 1L;
@ -50,6 +51,37 @@ public class Nullablelong implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Long orElse(@NotNull Long defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullablelong or(@NotNull NativeNullable<? extends Long> fallback) {
if (value == null) {
if (fallback.getClass() == Nullablelong.class) {
return (Nullablelong) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullablelong or(Nullablelong fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public long get() {
if (value == null) {
throw new NullPointerException();
@ -76,6 +108,11 @@ public class Nullablelong implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Long getNullable(@Nullable Long defaultValue) {
return value == null ? defaultValue : value;
}
public long getNullable(long defaultValue) {
return value == null ? defaultValue : value;
}

View File

@ -1,12 +1,13 @@
package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.NativeNullable;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Nullableshort implements Serializable, IGenericNullable {
public class Nullableshort implements Serializable, IGenericNullable, NativeNullable<Short> {
@Serial
private static final long serialVersionUID = 1L;
@ -50,6 +51,37 @@ public class Nullableshort implements Serializable, IGenericNullable {
return value != null;
}
@Override
public @NotNull Short orElse(@NotNull Short defaultValue) {
if (value == null) {
return defaultValue;
} else {
return value;
}
}
@Override
public @NotNull Nullableshort or(@NotNull NativeNullable<? extends Short> fallback) {
if (value == null) {
if (fallback.getClass() == Nullableshort.class) {
return (Nullableshort) fallback;
} else {
return ofNullable(fallback.getNullable());
}
} else {
return this;
}
}
@NotNull
public Nullableshort or(Nullableshort fallback) {
if (value == null) {
return fallback;
} else {
return this;
}
}
public short get() {
if (value == null) {
throw new NullPointerException();
@ -76,6 +108,11 @@ public class Nullableshort implements Serializable, IGenericNullable {
return value;
}
@Override
public @Nullable Short getNullable(@Nullable Short defaultValue) {
return value == null ? defaultValue : value;
}
public short getNullable(short defaultValue) {
return value == null ? defaultValue : value;
}