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>
<version>2.0.17</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.15.0</version> <!-- Use the latest version -->
</dependency>
</dependencies>
</project>

View File

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

View File

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