Deep copying
This commit is contained in:
@@ -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<T> {
|
||||
Class<T> valueClass();
|
||||
@@ -13,4 +14,7 @@ public interface ConfigEntry<T> {
|
||||
|
||||
void visitInOrder(ConfigEntryVisitor visitor);
|
||||
void visitInOrder(ConfigEntryValueVisitor visitor, T value);
|
||||
|
||||
@NotNull
|
||||
T deepCopy(@NotNull T value);
|
||||
}
|
||||
|
||||
@@ -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<E, T extends Collection<E>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> extends BaseConfigEntryImpl<T>
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
@@ -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<T> extends BaseConfigEntryImpl<T> implements SimpleConfigEntry<T> {
|
||||
public SimpleConfigEntryImpl(Class<T> valueClass) {
|
||||
@@ -18,4 +19,10 @@ public class SimpleConfigEntryImpl<T> extends BaseConfigEntryImpl<T> implements
|
||||
public void visitInOrder(ConfigEntryValueVisitor visitor, T value) {
|
||||
visitor.visitEntry(this, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public T deepCopy(@NotNull T value) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,4 +81,17 @@ public class StaticMapCompoundConfigEntryImpl<T extends Map<String, Object>> 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<Object> elementEntry = (ConfigEntry<Object>) compoundEntries.get(key);
|
||||
if (elementEntry != null) {
|
||||
copy.put(key, elementEntry.deepCopy(element));
|
||||
}
|
||||
});
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user