chore: Some minor changes

This commit is contained in:
Jakub Žitník 2025-03-08 17:53:47 +01:00
parent 32fba49587
commit 5e45851e04
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
3 changed files with 2 additions and 21 deletions

View File

@ -128,12 +128,6 @@
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
<version>2.0.17</version> <version>2.0.17</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.15.0</version> <!-- Use the latest version -->
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,6 +1,5 @@
package cz.jzitnik.game; package cz.jzitnik.game;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cz.jzitnik.game.entities.Block; import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.GameStates; import cz.jzitnik.game.entities.GameStates;
import cz.jzitnik.game.entities.Player; import cz.jzitnik.game.entities.Player;

View File

@ -5,11 +5,9 @@ import java.util.NoSuchElementException;
import java.util.function.*; import java.util.function.*;
public class MyOptional<T> implements Serializable { public class MyOptional<T> implements Serializable {
private static final long serialVersionUID = 1L;
private T value; private T value;
private boolean isPresent; private boolean isPresent;
// Constructor
public MyOptional() { public MyOptional() {
this.isPresent = false; this.isPresent = false;
} }
@ -19,12 +17,10 @@ public class MyOptional<T> implements Serializable {
this.isPresent = true; this.isPresent = true;
} }
// Check if value is present
public boolean isPresent() { public boolean isPresent() {
return isPresent; return isPresent;
} }
// Get the value (or throw exception if not present)
public T get() { public T get() {
if (!isPresent) { if (!isPresent) {
throw new NoSuchElementException("No value present"); throw new NoSuchElementException("No value present");
@ -32,12 +28,10 @@ public class MyOptional<T> implements Serializable {
return value; return value;
} }
// Get the value or return default
public T orElse(T other) { public T orElse(T other) {
return isPresent ? value : other; return isPresent ? value : other;
} }
// If value is present, apply function, else return another Optional
public <U> MyOptional<U> map(Function<? super T, ? extends U> mapper) { public <U> MyOptional<U> map(Function<? super T, ? extends U> mapper) {
if (!isPresent) { if (!isPresent) {
return new MyOptional<>(); return new MyOptional<>();
@ -45,7 +39,6 @@ public class MyOptional<T> implements Serializable {
return new MyOptional<>(mapper.apply(value)); return new MyOptional<>(mapper.apply(value));
} }
// If value is present, apply function and return a new Optional
public <U> MyOptional<U> flatMap(Function<? super T, ? extends MyOptional<U>> mapper) { public <U> MyOptional<U> flatMap(Function<? super T, ? extends MyOptional<U>> mapper) {
if (!isPresent) { if (!isPresent) {
return new MyOptional<>(); return new MyOptional<>();
@ -53,14 +46,12 @@ public class MyOptional<T> implements Serializable {
return mapper.apply(value); return mapper.apply(value);
} }
// Perform an action if value is present
public void ifPresent(Consumer<? super T> action) { public void ifPresent(Consumer<? super T> action) {
if (isPresent) { if (isPresent) {
action.accept(value); action.accept(value);
} }
} }
// Return the value inside the Optional or default if not present
public T orElseGet(Supplier<? extends T> other) { public T orElseGet(Supplier<? extends T> other) {
return isPresent ? value : other.get(); return isPresent ? value : other.get();
} }
@ -73,7 +64,7 @@ public class MyOptional<T> implements Serializable {
return this; return this;
} }
// Serialize the value @Serial
private void writeObject(ObjectOutputStream out) throws IOException { private void writeObject(ObjectOutputStream out) throws IOException {
out.writeBoolean(isPresent); out.writeBoolean(isPresent);
if (isPresent) { if (isPresent) {
@ -81,7 +72,7 @@ public class MyOptional<T> implements Serializable {
} }
} }
// Deserialize the value @Serial
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
isPresent = in.readBoolean(); isPresent = in.readBoolean();
if (isPresent) { if (isPresent) {
@ -94,17 +85,14 @@ public class MyOptional<T> implements Serializable {
return isPresent ? "SerializableOptional[" + value + "]" : "SerializableOptional.empty"; return isPresent ? "SerializableOptional[" + value + "]" : "SerializableOptional.empty";
} }
// Static factory method for an empty SerializableOptional
public static <T> MyOptional<T> empty() { public static <T> MyOptional<T> empty() {
return new MyOptional<>(); return new MyOptional<>();
} }
// Static factory method for a present value
public static <T> MyOptional<T> of(T value) { public static <T> MyOptional<T> of(T value) {
return new MyOptional<>(value); return new MyOptional<>(value);
} }
// Static factory method for a present value or null
public static <T> MyOptional<T> ofNullable(T value) { public static <T> MyOptional<T> ofNullable(T value) {
return value == null ? empty() : new MyOptional<>(value); return value == null ? empty() : new MyOptional<>(value);
} }