diff --git a/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/entry/ConfigEntry.java b/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/entry/ConfigEntry.java index bc15db6..d6908d2 100644 --- a/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/entry/ConfigEntry.java +++ b/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/entry/ConfigEntry.java @@ -2,6 +2,7 @@ package de.siphalor.tweed5.core.api.entry; import de.siphalor.tweed5.core.api.extension.EntryExtensionsData; import de.siphalor.tweed5.core.api.container.ConfigContainer; +import org.jetbrains.annotations.NotNull; public interface ConfigEntry { Class valueClass(); @@ -13,4 +14,7 @@ public interface ConfigEntry { void visitInOrder(ConfigEntryVisitor visitor); void visitInOrder(ConfigEntryValueVisitor visitor, T value); + + @NotNull + T deepCopy(@NotNull T value); } diff --git a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/CoherentCollectionConfigEntryImpl.java b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/CoherentCollectionConfigEntryImpl.java index e73193d..ff08501 100644 --- a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/CoherentCollectionConfigEntryImpl.java +++ b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/CoherentCollectionConfigEntryImpl.java @@ -4,6 +4,7 @@ import de.siphalor.tweed5.core.api.entry.CoherentCollectionConfigEntry; import de.siphalor.tweed5.core.api.entry.ConfigEntry; import de.siphalor.tweed5.core.api.entry.ConfigEntryValueVisitor; import de.siphalor.tweed5.core.api.entry.ConfigEntryVisitor; +import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.function.IntFunction; @@ -52,4 +53,13 @@ public class CoherentCollectionConfigEntryImpl> exten visitor.leaveCollectionEntry(this, value); } } + + @Override + public @NotNull T deepCopy(@NotNull T value) { + T copy = collectionConstructor.apply(value.size()); + for (E element : value) { + copy.add(elementEntry().deepCopy(element)); + } + return copy; + } } diff --git a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/ReflectiveCompoundConfigEntryImpl.java b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/ReflectiveCompoundConfigEntryImpl.java index 2e83f25..d323cd4 100644 --- a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/ReflectiveCompoundConfigEntryImpl.java +++ b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/ReflectiveCompoundConfigEntryImpl.java @@ -6,6 +6,7 @@ import de.siphalor.tweed5.core.api.entry.ConfigEntryValueVisitor; import de.siphalor.tweed5.core.api.entry.ConfigEntryVisitor; import lombok.Getter; import lombok.Value; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -113,6 +114,19 @@ public class ReflectiveCompoundConfigEntryImpl extends BaseConfigEntryImpl } } + @Override + public @NotNull T deepCopy(@NotNull T value) { + try { + T copy = instantiateCompoundValue(); + for (CompoundEntry compoundEntry : compoundEntries.values()) { + compoundEntry.field.set(copy, compoundEntry.field.get(value)); + } + return copy; + } catch (IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + @Value public static class CompoundEntry { String name; diff --git a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/SimpleConfigEntryImpl.java b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/SimpleConfigEntryImpl.java index aa0a35c..6262cc3 100644 --- a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/SimpleConfigEntryImpl.java +++ b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/SimpleConfigEntryImpl.java @@ -3,6 +3,7 @@ package de.siphalor.tweed5.core.impl.entry; import de.siphalor.tweed5.core.api.entry.ConfigEntryValueVisitor; import de.siphalor.tweed5.core.api.entry.ConfigEntryVisitor; import de.siphalor.tweed5.core.api.entry.SimpleConfigEntry; +import org.jetbrains.annotations.NotNull; public class SimpleConfigEntryImpl extends BaseConfigEntryImpl implements SimpleConfigEntry { public SimpleConfigEntryImpl(Class valueClass) { @@ -18,4 +19,10 @@ public class SimpleConfigEntryImpl extends BaseConfigEntryImpl implements public void visitInOrder(ConfigEntryValueVisitor visitor, T value) { visitor.visitEntry(this, value); } + + @Override + @NotNull + public T deepCopy(@NotNull T value) { + return value; + } } diff --git a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/StaticMapCompoundConfigEntryImpl.java b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/StaticMapCompoundConfigEntryImpl.java index 1378581..6484579 100644 --- a/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/StaticMapCompoundConfigEntryImpl.java +++ b/tweed5-core/src/main/java/de/siphalor/tweed5/core/impl/entry/StaticMapCompoundConfigEntryImpl.java @@ -81,4 +81,17 @@ public class StaticMapCompoundConfigEntryImpl> ext visitor.leaveCompoundEntry(this, value); } } + + @Override + public @NotNull T deepCopy(@NotNull T value) { + T copy = instantiateCompoundValue(); + value.forEach((String key, Object element) -> { + //noinspection unchecked + ConfigEntry elementEntry = (ConfigEntry) compoundEntries.get(key); + if (elementEntry != null) { + copy.put(key, elementEntry.deepCopy(element)); + } + }); + return copy; + } }