-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #209 from Dragon-Seeker/1.20.3-EdmMap
[1.20.3/4] Implement EdmMap
- Loading branch information
Showing
3 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/io/wispforest/owo/serialization/format/edm/EdmMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.wispforest.owo.serialization.format.edm; | ||
|
||
import io.wispforest.owo.serialization.endec.KeyedEndec; | ||
import io.wispforest.owo.serialization.util.MapCarrier; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Map; | ||
|
||
public final class EdmMap extends EdmElement<Map<String, EdmElement<?>>> implements MapCarrier { | ||
|
||
EdmMap(EdmElement<Map<String, EdmElement<?>>> mapElement) { | ||
super(mapElement.value(), mapElement.type()); | ||
} | ||
|
||
@Override | ||
public <T> T getWithErrors(@NotNull KeyedEndec<T> key) { | ||
if (!this.has(key)) return key.defaultValue(); | ||
return key.endec().decodeFully(EdmDeserializer::of, this.value().get(key.key())); | ||
} | ||
|
||
@Override | ||
public <T> void put(@NotNull KeyedEndec<T> key, @NotNull T value) { | ||
this.value().put(key.key(), key.endec().encodeFully(EdmSerializer::of, value)); | ||
} | ||
|
||
@Override | ||
public <T> void delete(@NotNull KeyedEndec<T> key) { | ||
this.value().remove(key.key()); | ||
} | ||
|
||
@Override | ||
public <T> boolean has(@NotNull KeyedEndec<T> key) { | ||
return this.value().containsKey(key.key()); | ||
} | ||
} |