Add data inizializer/upgrader declarative context parameters
This commit is contained in:
parent
73d24bf13f
commit
5afe301750
@ -82,7 +82,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.10.1</version>
|
<version>3.11.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<release>21</release>
|
<release>21</release>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
|
@ -159,7 +159,7 @@ public class DataModel {
|
|||||||
.stream()
|
.stream()
|
||||||
.filter(x -> x.size() > 1)
|
.filter(x -> x.size() > 1)
|
||||||
.forEach(x -> {
|
.forEach(x -> {
|
||||||
var type = x.get(0);
|
var type = x.getFirst();
|
||||||
throw new IllegalArgumentException("Type " + type + " has been defined more than once (check base, super, and custom types)!");
|
throw new IllegalArgumentException("Type " + type + " has been defined more than once (check base, super, and custom types)!");
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -209,28 +209,19 @@ public class DataModel {
|
|||||||
+ t.transformClass);
|
+ t.transformClass);
|
||||||
}
|
}
|
||||||
transformClass.addDifferentThanPrev(transformation);
|
transformClass.addDifferentThanPrev(transformation);
|
||||||
var definition = removeAndGetIndex(transformClass.data, t.from);
|
var definition = transformClass.remove(t.from);
|
||||||
if (definition.isEmpty()) {
|
if (definition.isEmpty()) {
|
||||||
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown field: " + t.from);
|
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown field: " + t.from);
|
||||||
}
|
}
|
||||||
String prevDef;
|
ParsedClass.FieldInfo prevDef = transformClass.insert(
|
||||||
if (t.index != null) {
|
Objects.requireNonNullElse(t.index, definition.get().getKey()),
|
||||||
prevDef = tryInsertAtIndex(transformClass.data,
|
t.to,
|
||||||
t.to,
|
new ParsedClass.InputFieldInfo(definition.get().getValue().typeName(), List.of())
|
||||||
definition.get().getValue(),
|
);
|
||||||
t.index
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
prevDef = tryInsertAtIndex(transformClass.data,
|
|
||||||
t.to,
|
|
||||||
definition.get().getValue(),
|
|
||||||
definition.get().getKey()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (prevDef != null) {
|
if (prevDef != null) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
transformCoordinate + " tries to overwrite the existing field \"" + t.to + "\" of value \""
|
transformCoordinate + " tries to overwrite the existing field \"" + t.to + "\" of value \""
|
||||||
+ prevDef + "\" with the field \"" + t.from + "\" of type \"" + definition.orElse(null) + "\"");
|
+ prevDef.typeName() + "\" with the field \"" + t.from + "\" of type \"" + definition.orElse(null) + "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "new-data" -> {
|
case "new-data" -> {
|
||||||
@ -244,15 +235,12 @@ public class DataModel {
|
|||||||
if (!allTypes.contains(extractTypeName(t.type))) {
|
if (!allTypes.contains(extractTypeName(t.type))) {
|
||||||
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown type: " + t.type);
|
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown type: " + t.type);
|
||||||
}
|
}
|
||||||
String prevDef;
|
var type = fixType(t.type);
|
||||||
if (t.index != null) {
|
var fieldInfo = new ParsedClass.InputFieldInfo(type, t.getContextParameters());
|
||||||
prevDef = tryInsertAtIndex(transformClass.data, t.to, fixType(t.type), t.index);
|
ParsedClass.FieldInfo prevDef = transformClass.insert(t.index, t.to, fieldInfo);
|
||||||
} else {
|
|
||||||
prevDef = transformClass.data.putIfAbsent(t.to, fixType(t.type));
|
|
||||||
}
|
|
||||||
if (prevDef != null) {
|
if (prevDef != null) {
|
||||||
throw new IllegalArgumentException(transformCoordinate + " tries to overwrite the existing field \""
|
throw new IllegalArgumentException(transformCoordinate + " tries to overwrite the existing field \""
|
||||||
+ t.to + "\" of value \"" + prevDef
|
+ t.to + "\" of value \"" + prevDef.typeName()
|
||||||
+ "\" with the new type \"" + t.type + "\"");
|
+ "\" with the new type \"" + t.type + "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,7 +269,7 @@ public class DataModel {
|
|||||||
if (!allTypes.contains(extractTypeName(t.type))) {
|
if (!allTypes.contains(extractTypeName(t.type))) {
|
||||||
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown type: " + t.type);
|
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown type: " + t.type);
|
||||||
}
|
}
|
||||||
String prevDefinition = transformClass.data.replace(t.from, fixType(t.type));
|
var prevDefinition = transformClass.replace(t.from, new ParsedClass.InputFieldInfo(fixType(t.type), t.getContextParameters()));
|
||||||
if (prevDefinition == null) {
|
if (prevDefinition == null) {
|
||||||
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown field: " + t.from);
|
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown field: " + t.from);
|
||||||
}
|
}
|
||||||
@ -340,7 +328,7 @@ public class DataModel {
|
|||||||
List<ComputedType> versionBaseTypes = computedClassConfig.get(versionIndexF).entrySet().stream()
|
List<ComputedType> versionBaseTypes = computedClassConfig.get(versionIndexF).entrySet().stream()
|
||||||
.map(e -> {
|
.map(e -> {
|
||||||
var data = new LinkedHashMap<String, VersionedType>();
|
var data = new LinkedHashMap<String, VersionedType>();
|
||||||
e.getValue().getData().forEach((key, value) -> data.put(key, new VersionedType(value, version)));
|
e.getValue().getData().forEach((key, value) -> data.put(key, new VersionedType(value.typeName(), version)));
|
||||||
return new ComputedTypeBase(new VersionedType(e.getKey(), version),
|
return new ComputedTypeBase(new VersionedType(e.getKey(), version),
|
||||||
e.getValue().stringRepresenter, data, computedTypeSupplier);
|
e.getValue().stringRepresenter, data, computedTypeSupplier);
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
@ -357,8 +345,8 @@ public class DataModel {
|
|||||||
var nullableRawTypes = computedClassConfig.values().stream()
|
var nullableRawTypes = computedClassConfig.values().stream()
|
||||||
.flatMap(x -> x.values().stream())
|
.flatMap(x -> x.values().stream())
|
||||||
.flatMap(x -> x.getData().values().stream())
|
.flatMap(x -> x.getData().values().stream())
|
||||||
.filter(x -> x.startsWith("-"))
|
.filter(x -> x.typeName().startsWith("-"))
|
||||||
.map(nullableName -> nullableName.substring(1))
|
.map(nullableName -> nullableName.typeName().substring(1))
|
||||||
.toList();
|
.toList();
|
||||||
// Compute nullable versioned types
|
// Compute nullable versioned types
|
||||||
nullableRawTypes.stream()
|
nullableRawTypes.stream()
|
||||||
@ -383,8 +371,8 @@ public class DataModel {
|
|||||||
var arrayRawTypes = computedClassConfig.values().stream()
|
var arrayRawTypes = computedClassConfig.values().stream()
|
||||||
.flatMap(x -> x.values().stream())
|
.flatMap(x -> x.values().stream())
|
||||||
.flatMap(x -> x.getData().values().stream())
|
.flatMap(x -> x.getData().values().stream())
|
||||||
.filter(x -> x.startsWith("§"))
|
.filter(x -> x.typeName().startsWith("§"))
|
||||||
.map(nullableName -> nullableName.substring(1))
|
.map(nullableName -> nullableName.typeName().substring(1))
|
||||||
.toList();
|
.toList();
|
||||||
// Compute array versioned types
|
// Compute array versioned types
|
||||||
arrayRawTypes.stream()
|
arrayRawTypes.stream()
|
||||||
@ -450,7 +438,7 @@ public class DataModel {
|
|||||||
.entrySet()
|
.entrySet()
|
||||||
.stream()
|
.stream()
|
||||||
.collect(Collectors.toMap(Entry::getKey, e -> {
|
.collect(Collectors.toMap(Entry::getKey, e -> {
|
||||||
var fieldTypeName = e.getValue();
|
var fieldTypeName = e.getValue().typeName();
|
||||||
return new VersionedType(fieldTypeName, computedVersions.get(currentOrNewerTypeVersion.getInt(fieldTypeName)));
|
return new VersionedType(fieldTypeName, computedVersions.get(currentOrNewerTypeVersion.getInt(fieldTypeName)));
|
||||||
}, (a, b) -> {
|
}, (a, b) -> {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
@ -591,40 +579,6 @@ public class DataModel {
|
|||||||
this.baseTypeDataChanges = baseTypeDataChanges;
|
this.baseTypeDataChanges = baseTypeDataChanges;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String tryInsertAtIndex(LinkedHashMap<String, String> data, String key, String value, int index) {
|
|
||||||
var before = new LinkedHashMap<String, String>();
|
|
||||||
var after = new LinkedHashMap<String, String>();
|
|
||||||
int i = 0;
|
|
||||||
for (Entry<String, String> entry : data.entrySet()) {
|
|
||||||
if (i < index) {
|
|
||||||
before.put(entry.getKey(), entry.getValue());
|
|
||||||
} else {
|
|
||||||
after.put(entry.getKey(), entry.getValue());
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
data.clear();
|
|
||||||
data.putAll(before);
|
|
||||||
var prev = data.putIfAbsent(key, value);
|
|
||||||
data.putAll(after);
|
|
||||||
return prev;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Optional<Entry<Integer, String>> removeAndGetIndex(LinkedHashMap<String, String> data, String find) {
|
|
||||||
int foundIndex = -1;
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
for (Entry<String, String> entry : data.entrySet()) {
|
|
||||||
if (entry.getKey().equals(find)) {
|
|
||||||
foundIndex = i;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (foundIndex == -1) return Optional.empty();
|
|
||||||
return Optional.of(Map.entry(foundIndex, requireNonNull(data.remove(find))));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static String getNextVersion(Map<String, String> versionsSequence, String version) {
|
public static String getNextVersion(Map<String, String> versionsSequence, String version) {
|
||||||
return versionsSequence.get(version);
|
return versionsSequence.get(version);
|
||||||
@ -650,7 +604,7 @@ public class DataModel {
|
|||||||
if (list.size() != 1) {
|
if (list.size() != 1) {
|
||||||
throw exceptionGenerator.apply(list);
|
throw exceptionGenerator.apply(list);
|
||||||
}
|
}
|
||||||
return list.get(0);
|
return list.getFirst();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -669,7 +623,7 @@ public class DataModel {
|
|||||||
if (list.isEmpty()) {
|
if (list.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
return Optional.of(list.get(0));
|
return Optional.of(list.getFirst());
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package it.cavallium.datagen.plugin;
|
package it.cavallium.datagen.plugin;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public final class MoveDataConfiguration implements TransformationConfiguration {
|
public final class MoveDataConfiguration implements TransformationConfiguration {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package it.cavallium.datagen.plugin;
|
package it.cavallium.datagen.plugin;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public class NewDataConfiguration implements TransformationConfiguration {
|
public class NewDataConfiguration implements TransformationConfiguration {
|
||||||
@ -12,6 +15,8 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
public String initializerInstance;
|
public String initializerInstance;
|
||||||
@Nullable
|
@Nullable
|
||||||
public Integer index;
|
public Integer index;
|
||||||
|
@Nullable
|
||||||
|
public List<String> contextParameters;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTransformClass() {
|
public String getTransformClass() {
|
||||||
@ -27,6 +32,11 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
return JInterfaceLocation.parse(initializer, initializerInstance);
|
return JInterfaceLocation.parse(initializer, initializerInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public List<String> getContextParameters() {
|
||||||
|
return Objects.requireNonNullElse(contextParameters, List.of());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
@ -39,7 +49,8 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
return Objects.equals(transformClass, that.transformClass) && Objects.equals(to, that.to)
|
return Objects.equals(transformClass, that.transformClass) && Objects.equals(to, that.to)
|
||||||
&& Objects.equals(type, that.type) && Objects.equals(initializer, that.initializer)
|
&& Objects.equals(type, that.type) && Objects.equals(initializer, that.initializer)
|
||||||
&& Objects.equals(initializerInstance, that.initializerInstance)
|
&& Objects.equals(initializerInstance, that.initializerInstance)
|
||||||
&& Objects.equals(index, that.index);
|
&& Objects.equals(index, that.index)
|
||||||
|
&& Objects.equals(contextParameters, that.contextParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -51,6 +62,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
hash += ConfigUtils.hashCode(initializer);
|
hash += ConfigUtils.hashCode(initializer);
|
||||||
hash += ConfigUtils.hashCode(initializerInstance);
|
hash += ConfigUtils.hashCode(initializerInstance);
|
||||||
hash += ConfigUtils.hashCode(index);
|
hash += ConfigUtils.hashCode(index);
|
||||||
|
hash += ConfigUtils.hashCode(contextParameters);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +74,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
|
|||||||
if (this.to != null) c.to = this.to;
|
if (this.to != null) c.to = this.to;
|
||||||
if (this.type != null) c.type = this.type;
|
if (this.type != null) c.type = this.type;
|
||||||
if (this.index != null) c.index = this.index;
|
if (this.index != null) c.index = this.index;
|
||||||
|
if (this.contextParameters != null) c.contextParameters = this.contextParameters;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,26 @@
|
|||||||
package it.cavallium.datagen.plugin;
|
package it.cavallium.datagen.plugin;
|
||||||
|
|
||||||
import static it.cavallium.datagen.plugin.DataModel.fixType;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import static it.cavallium.datagen.plugin.DataModel.fixType;
|
||||||
import java.util.LinkedHashMap;
|
import static it.cavallium.datagen.plugin.DataModel.joinPackage;
|
||||||
import java.util.List;
|
import static java.util.Objects.requireNonNull;
|
||||||
import java.util.Map;
|
|
||||||
|
import java.util.*;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Objects;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public final class ParsedClass {
|
public final class ParsedClass {
|
||||||
|
|
||||||
|
public record InputFieldInfo(@NotNull String typeName, @NotNull List<String> contextParams) {}
|
||||||
|
|
||||||
|
public record FieldInfo(@NotNull String typeName, @NotNull LinkedHashMap<String, String> contextFieldsData) {}
|
||||||
|
|
||||||
public String stringRepresenter;
|
public String stringRepresenter;
|
||||||
|
|
||||||
public LinkedHashMap<String, String> data;
|
public LinkedHashMap<String, FieldInfo> data;
|
||||||
public List<TransformationConfiguration> differentThanPrev;
|
public List<TransformationConfiguration> differentThanPrev;
|
||||||
public boolean differentThanNext;
|
public boolean differentThanNext;
|
||||||
|
|
||||||
@ -23,9 +29,11 @@ public final class ParsedClass {
|
|||||||
if (baseTypesData.data != null) {
|
if (baseTypesData.data != null) {
|
||||||
this.data = baseTypesData.data.entrySet().stream()
|
this.data = baseTypesData.data.entrySet().stream()
|
||||||
.map(e -> Map.entry(e.getKey(), fixType(e.getValue())))
|
.map(e -> Map.entry(e.getKey(), fixType(e.getValue())))
|
||||||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> {
|
.collect(Collectors.toMap(Entry::getKey,
|
||||||
throw new IllegalStateException();
|
v -> new FieldInfo(v.getValue(),
|
||||||
}, LinkedHashMap::new));
|
new LinkedHashMap<>(0)),
|
||||||
|
(a, b) -> {throw new IllegalStateException();},
|
||||||
|
LinkedHashMap::new));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +45,7 @@ public final class ParsedClass {
|
|||||||
return stringRepresenter;
|
return stringRepresenter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkedHashMap<String, String> getData() {
|
public LinkedHashMap<String, FieldInfo> getData() {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,4 +84,82 @@ public final class ParsedClass {
|
|||||||
}
|
}
|
||||||
differentThanPrev.add(transformation);
|
differentThanPrev.add(transformation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class NoContextParameterException extends RuntimeException {
|
||||||
|
private final String contextParameter;
|
||||||
|
|
||||||
|
public NoContextParameterException(String contextParameter) {
|
||||||
|
this.contextParameter = contextParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContextParameter() {
|
||||||
|
return contextParameter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static LinkedHashMap<String, String> computeContextParametersTypes(
|
||||||
|
LinkedHashMap<String, ParsedClass.FieldInfo> data, @NotNull List<String> contextParameters)
|
||||||
|
throws NoContextParameterException {
|
||||||
|
return contextParameters.stream().collect(Collectors.toMap(Function.identity(), param -> {
|
||||||
|
var type = data.get(param);
|
||||||
|
if (type == null) {
|
||||||
|
throw new NoContextParameterException(param);
|
||||||
|
}
|
||||||
|
return type.typeName();
|
||||||
|
}, (c, a) -> {
|
||||||
|
throw new IllegalStateException("Unreachable");
|
||||||
|
}, LinkedHashMap::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
public FieldInfo insert(@Nullable Integer index, String to, InputFieldInfo fieldInfo) throws NoContextParameterException {
|
||||||
|
var value = convertFieldInfo(fieldInfo);
|
||||||
|
if (index == null) {
|
||||||
|
return data.putIfAbsent(to, value);
|
||||||
|
} else {
|
||||||
|
return tryInsertAtIndex(data, to, value, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Entry<Integer, FieldInfo>> remove(String find) {
|
||||||
|
int foundIndex = -1;
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
for (var entry : data.entrySet()) {
|
||||||
|
if (entry.getKey().equals(find)) {
|
||||||
|
foundIndex = i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (foundIndex == -1) return Optional.empty();
|
||||||
|
return Optional.of(Map.entry(foundIndex, requireNonNull(data.remove(find))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public FieldInfo replace(String from, InputFieldInfo fieldInfo) {
|
||||||
|
return data.replace(from, convertFieldInfo(fieldInfo));
|
||||||
|
}
|
||||||
|
|
||||||
|
private FieldInfo convertFieldInfo(InputFieldInfo fieldInfo) {
|
||||||
|
return new FieldInfo(fieldInfo.typeName, computeContextParametersTypes(data, fieldInfo.contextParams));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ParsedClass.FieldInfo tryInsertAtIndex(LinkedHashMap<String, ParsedClass.FieldInfo> data, String key,
|
||||||
|
FieldInfo value, int index) {
|
||||||
|
var before = new LinkedHashMap<String, ParsedClass.FieldInfo>(index);
|
||||||
|
var after = new LinkedHashMap<String, ParsedClass.FieldInfo>(data.size() - index);
|
||||||
|
int i = 0;
|
||||||
|
for (Entry<String, ParsedClass.FieldInfo> entry : data.entrySet()) {
|
||||||
|
if (i < index) {
|
||||||
|
before.put(entry.getKey(), entry.getValue());
|
||||||
|
} else {
|
||||||
|
after.put(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
data.clear();
|
||||||
|
data.putAll(before);
|
||||||
|
var prev = data.putIfAbsent(key, value);
|
||||||
|
data.putAll(after);
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -405,7 +405,7 @@ public class SourcesGenerator {
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String capitalize(String field) {
|
public static String capitalize(String field) {
|
||||||
return Character.toUpperCase(field.charAt(0)) + field.substring(1);
|
return Character.toUpperCase(field.charAt(0)) + field.substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package it.cavallium.datagen.plugin;
|
package it.cavallium.datagen.plugin;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class UpgradeDataConfiguration implements TransformationConfiguration {
|
public class UpgradeDataConfiguration implements TransformationConfiguration {
|
||||||
@ -9,6 +13,8 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
public String type;
|
public String type;
|
||||||
public String upgrader;
|
public String upgrader;
|
||||||
public String upgraderInstance;
|
public String upgraderInstance;
|
||||||
|
@Nullable
|
||||||
|
public List<String> contextParameters;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTransformClass() {
|
public String getTransformClass() {
|
||||||
@ -24,6 +30,11 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
return JInterfaceLocation.parse(upgrader, upgraderInstance);
|
return JInterfaceLocation.parse(upgrader, upgraderInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public List<String> getContextParameters() {
|
||||||
|
return Objects.requireNonNullElse(contextParameters, List.of());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
@ -35,7 +46,8 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
UpgradeDataConfiguration that = (UpgradeDataConfiguration) o;
|
UpgradeDataConfiguration that = (UpgradeDataConfiguration) o;
|
||||||
return Objects.equals(transformClass, that.transformClass) && Objects.equals(from, that.from)
|
return Objects.equals(transformClass, that.transformClass) && Objects.equals(from, that.from)
|
||||||
&& Objects.equals(type, that.type) && Objects.equals(upgrader, that.upgrader)
|
&& Objects.equals(type, that.type) && Objects.equals(upgrader, that.upgrader)
|
||||||
&& Objects.equals(upgraderInstance, that.upgraderInstance);
|
&& Objects.equals(upgraderInstance, that.upgraderInstance)
|
||||||
|
&& Objects.equals(contextParameters, that.contextParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -46,6 +58,7 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
hash += ConfigUtils.hashCode(type);
|
hash += ConfigUtils.hashCode(type);
|
||||||
hash += ConfigUtils.hashCode(upgrader);
|
hash += ConfigUtils.hashCode(upgrader);
|
||||||
hash += ConfigUtils.hashCode(upgraderInstance);
|
hash += ConfigUtils.hashCode(upgraderInstance);
|
||||||
|
hash += ConfigUtils.hashCode(contextParameters);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,6 +69,7 @@ public class UpgradeDataConfiguration implements TransformationConfiguration {
|
|||||||
if (this.type != null) c.type = this.type;
|
if (this.type != null) c.type = this.type;
|
||||||
if (this.upgrader != null) c.upgrader = this.upgrader;
|
if (this.upgrader != null) c.upgrader = this.upgrader;
|
||||||
if (this.upgraderInstance != null) c.upgraderInstance = this.upgraderInstance;
|
if (this.upgraderInstance != null) c.upgraderInstance = this.upgraderInstance;
|
||||||
|
if (this.contextParameters != null) c.contextParameters = this.contextParameters;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,22 +9,12 @@ import com.squareup.javapoet.ParameterizedTypeName;
|
|||||||
import com.squareup.javapoet.TypeName;
|
import com.squareup.javapoet.TypeName;
|
||||||
import com.squareup.javapoet.TypeSpec;
|
import com.squareup.javapoet.TypeSpec;
|
||||||
import com.squareup.javapoet.TypeSpec.Builder;
|
import com.squareup.javapoet.TypeSpec.Builder;
|
||||||
import it.cavallium.datagen.DataInitializer;
|
import it.cavallium.datagen.*;
|
||||||
import it.cavallium.datagen.DataUpgrader;
|
import it.cavallium.datagen.plugin.*;
|
||||||
import it.cavallium.datagen.plugin.ClassGenerator;
|
|
||||||
import it.cavallium.datagen.plugin.ComputedType;
|
|
||||||
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
||||||
import it.cavallium.datagen.plugin.ComputedTypeBase;
|
|
||||||
import it.cavallium.datagen.plugin.ComputedVersion;
|
|
||||||
import it.cavallium.datagen.plugin.JInterfaceLocation;
|
|
||||||
import it.cavallium.datagen.plugin.JInterfaceLocation.JInterfaceLocationClassName;
|
import it.cavallium.datagen.plugin.JInterfaceLocation.JInterfaceLocationClassName;
|
||||||
import it.cavallium.datagen.plugin.JInterfaceLocation.JInterfaceLocationInstanceField;
|
import it.cavallium.datagen.plugin.JInterfaceLocation.JInterfaceLocationInstanceField;
|
||||||
import it.cavallium.datagen.plugin.MoveDataConfiguration;
|
|
||||||
import it.cavallium.datagen.plugin.NewDataConfiguration;
|
|
||||||
import it.cavallium.datagen.plugin.RemoveDataConfiguration;
|
|
||||||
import it.cavallium.datagen.plugin.TransformationConfiguration;
|
|
||||||
import it.cavallium.datagen.plugin.UpgradeDataConfiguration;
|
|
||||||
import it.cavallium.datagen.plugin.DataModel;
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -36,6 +26,7 @@ import java.util.stream.IntStream;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import javax.lang.model.element.Modifier;
|
import javax.lang.model.element.Modifier;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public class GenUpgraderBaseX extends ClassGenerator {
|
public class GenUpgraderBaseX extends ClassGenerator {
|
||||||
|
|
||||||
@ -64,7 +55,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
|
|
||||||
classBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
|
classBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
|
||||||
|
|
||||||
classBuilder.addSuperinterface(ParameterizedTypeName.get(ClassName.get(DataUpgrader.class),
|
classBuilder.superclass(ParameterizedTypeName.get(ClassName.get(DataUpgraderSimple.class),
|
||||||
typeBaseClassName,
|
typeBaseClassName,
|
||||||
nextTypeBase.getJTypeName(basePackageName)
|
nextTypeBase.getJTypeName(basePackageName)
|
||||||
));
|
));
|
||||||
@ -92,6 +83,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
|
|
||||||
AtomicInteger nextInitializerStaticFieldId = new AtomicInteger();
|
AtomicInteger nextInitializerStaticFieldId = new AtomicInteger();
|
||||||
HashMap<String, String> initializerStaticFieldNames = new HashMap<>();
|
HashMap<String, String> initializerStaticFieldNames = new HashMap<>();
|
||||||
|
HashMap<String, ContextInfo> contextStaticFieldCodeBlocks = new HashMap<>();
|
||||||
AtomicInteger nextUpgraderStaticFieldId = new AtomicInteger();
|
AtomicInteger nextUpgraderStaticFieldId = new AtomicInteger();
|
||||||
HashMap<String, String> upgraderStaticFieldNames = new HashMap<>();
|
HashMap<String, String> upgraderStaticFieldNames = new HashMap<>();
|
||||||
List<TransformationConfiguration> transformations = dataModel.getChanges(nextTypeBase);
|
List<TransformationConfiguration> transformations = dataModel.getChanges(nextTypeBase);
|
||||||
@ -120,7 +112,16 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
var computedTypes = dataModel.getComputedTypes(nextTypeBase.getVersion());
|
var computedTypes = dataModel.getComputedTypes(nextTypeBase.getVersion());
|
||||||
var newFieldType = Objects.requireNonNull(computedTypes.get(DataModel.fixType(newDataConfiguration.type)));
|
var newFieldType = Objects.requireNonNull(computedTypes.get(DataModel.fixType(newDataConfiguration.type)));
|
||||||
var initializerLocation = newDataConfiguration.getInitializerLocation();
|
var initializerLocation = newDataConfiguration.getInitializerLocation();
|
||||||
|
|
||||||
|
var contextInfo = createContextStaticClass(typeBase, e.getValue().to,
|
||||||
|
contextStaticFieldCodeBlocks,
|
||||||
|
classBuilder,
|
||||||
|
initializerLocation,
|
||||||
|
newDataConfiguration.getContextParameters()
|
||||||
|
);
|
||||||
|
|
||||||
var genericInitializerClass = ParameterizedTypeName.get(ClassName.get(DataInitializer.class),
|
var genericInitializerClass = ParameterizedTypeName.get(ClassName.get(DataInitializer.class),
|
||||||
|
contextInfo.typeName(),
|
||||||
newFieldType.getJTypeName(basePackageName).box()
|
newFieldType.getJTypeName(basePackageName).box()
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -131,7 +132,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
genericInitializerClass
|
genericInitializerClass
|
||||||
);
|
);
|
||||||
|
|
||||||
return new Field(newDataConfiguration.to, newFieldType, CodeBlock.of("$N.initialize()", initializerName), i + 1);
|
return new Field(newDataConfiguration.to, newFieldType, CodeBlock.of("$N.initialize($L)", initializerName, contextInfo.contextApply), i + 1);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
resultFields = fields.<ResultField>mapMulti((field, consumer) -> {
|
resultFields = fields.<ResultField>mapMulti((field, consumer) -> {
|
||||||
@ -170,7 +171,16 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
var cb = CodeBlock.builder();
|
var cb = CodeBlock.builder();
|
||||||
var newFieldType = Objects
|
var newFieldType = Objects
|
||||||
.requireNonNull(dataModel.getComputedTypes(nextTypeBase.getVersion()).get(DataModel.fixType(upgradeDataConfiguration.type)));
|
.requireNonNull(dataModel.getComputedTypes(nextTypeBase.getVersion()).get(DataModel.fixType(upgradeDataConfiguration.type)));
|
||||||
|
|
||||||
|
var contextInfo = createContextStaticClass(typeBase, upgradeDataConfiguration.from,
|
||||||
|
contextStaticFieldCodeBlocks,
|
||||||
|
classBuilder,
|
||||||
|
upgraderImplementationLocation,
|
||||||
|
upgradeDataConfiguration.getContextParameters()
|
||||||
|
);
|
||||||
|
|
||||||
var genericUpgraderClass = ParameterizedTypeName.get(ClassName.get(DataUpgrader.class),
|
var genericUpgraderClass = ParameterizedTypeName.get(ClassName.get(DataUpgrader.class),
|
||||||
|
contextInfo.typeName(),
|
||||||
fieldType.getJTypeName(basePackageName).box(),
|
fieldType.getJTypeName(basePackageName).box(),
|
||||||
newFieldType.getJTypeName(basePackageName).box()
|
newFieldType.getJTypeName(basePackageName).box()
|
||||||
);
|
);
|
||||||
@ -182,9 +192,10 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
genericUpgraderClass
|
genericUpgraderClass
|
||||||
);
|
);
|
||||||
|
|
||||||
cb.add("($T) $N.upgrade(($T) ",
|
cb.add("($T) $N.upgrade($L, ($T) ",
|
||||||
newFieldType.getJTypeName(basePackageName),
|
newFieldType.getJTypeName(basePackageName),
|
||||||
upgraderName,
|
upgraderName,
|
||||||
|
contextInfo.contextApply,
|
||||||
fieldType.getJTypeName(basePackageName)
|
fieldType.getJTypeName(basePackageName)
|
||||||
);
|
);
|
||||||
cb.add(codeBlock);
|
cb.add(codeBlock);
|
||||||
@ -223,10 +234,10 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String createInitializerStaticField(AtomicInteger nextInitializerStaticFieldId,
|
private String createInitializerStaticField(AtomicInteger nextInitializerStaticFieldId,
|
||||||
HashMap<String, String> initializerStaticFieldNames,
|
HashMap<String, String> initializerStaticFieldNames,
|
||||||
Builder classBuilder,
|
Builder classBuilder,
|
||||||
JInterfaceLocation initializerLocation,
|
JInterfaceLocation initializerLocation,
|
||||||
TypeName genericInitializerClass) {
|
TypeName genericInitializerClass) {
|
||||||
var identifier = initializerLocation.getIdentifier();
|
var identifier = initializerLocation.getIdentifier();
|
||||||
var initializerName = initializerStaticFieldNames.get(identifier);
|
var initializerName = initializerStaticFieldNames.get(identifier);
|
||||||
if (initializerName == null) {
|
if (initializerName == null) {
|
||||||
@ -247,6 +258,55 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
|||||||
return initializerName;
|
return initializerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
record ContextInfo(TypeName typeName, CodeBlock contextApply) {}
|
||||||
|
|
||||||
|
private ContextInfo createContextStaticClass(ComputedTypeBase typeBase,
|
||||||
|
String fieldName,
|
||||||
|
HashMap<String, ContextInfo> contextStaticFieldCodeBlocks,
|
||||||
|
Builder classBuilder,
|
||||||
|
JInterfaceLocation initializerLocation,
|
||||||
|
@NotNull List<String> contextParameters) {
|
||||||
|
var identifier = initializerLocation.getIdentifier();
|
||||||
|
var contextStaticFieldCodeBlock = contextStaticFieldCodeBlocks.get(identifier);
|
||||||
|
if (contextStaticFieldCodeBlock == null) {
|
||||||
|
var codeBlockBuilder = CodeBlock.builder();
|
||||||
|
TypeName typeName;
|
||||||
|
|
||||||
|
if (contextParameters.isEmpty()) {
|
||||||
|
typeName = ClassName.get(DataContextNone.class);
|
||||||
|
codeBlockBuilder.add("$T.INSTANCE", typeName);
|
||||||
|
} else {
|
||||||
|
var name = "Context" + SourcesGenerator.capitalize(fieldName);
|
||||||
|
var contextTypeClassBuilder = TypeSpec.recordBuilder(name)
|
||||||
|
.addSuperinterface(ClassName.get(DataContext.class))
|
||||||
|
.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
||||||
|
typeName = typeBase.getJUpgraderName(basePackageName).nestedClass(name);
|
||||||
|
|
||||||
|
codeBlockBuilder.add("new $T(", typeName);
|
||||||
|
boolean first = true;
|
||||||
|
for (String contextParameter : contextParameters) {
|
||||||
|
var fieldType = typeBase.getData().get(contextParameter);
|
||||||
|
contextTypeClassBuilder.addRecordComponent(ParameterSpec.builder(fieldType.getJTypeName(basePackageName), contextParameter).build());
|
||||||
|
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
codeBlockBuilder.add(", ");
|
||||||
|
}
|
||||||
|
codeBlockBuilder.add("data.$N()", contextParameter);
|
||||||
|
}
|
||||||
|
codeBlockBuilder.add(")");
|
||||||
|
|
||||||
|
var clazz = contextTypeClassBuilder.build();
|
||||||
|
classBuilder.addType(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
contextStaticFieldCodeBlock = new ContextInfo(typeName, codeBlockBuilder.build());
|
||||||
|
contextStaticFieldCodeBlocks.put(identifier, contextStaticFieldCodeBlock);
|
||||||
|
}
|
||||||
|
return contextStaticFieldCodeBlock;
|
||||||
|
}
|
||||||
|
|
||||||
private String createUpgraderStaticField(AtomicInteger nextUpgraderStaticFieldId,
|
private String createUpgraderStaticField(AtomicInteger nextUpgraderStaticFieldId,
|
||||||
HashMap<String, String> upgraderStaticFieldNames,
|
HashMap<String, String> upgraderStaticFieldNames,
|
||||||
Builder classBuilder,
|
Builder classBuilder,
|
||||||
|
@ -8,6 +8,7 @@ import com.squareup.javapoet.ParameterizedTypeName;
|
|||||||
import com.squareup.javapoet.TypeSpec;
|
import com.squareup.javapoet.TypeSpec;
|
||||||
import com.squareup.javapoet.TypeSpec.Builder;
|
import com.squareup.javapoet.TypeSpec.Builder;
|
||||||
import it.cavallium.datagen.DataUpgrader;
|
import it.cavallium.datagen.DataUpgrader;
|
||||||
|
import it.cavallium.datagen.DataUpgraderSimple;
|
||||||
import it.cavallium.datagen.plugin.ClassGenerator;
|
import it.cavallium.datagen.plugin.ClassGenerator;
|
||||||
import it.cavallium.datagen.plugin.ComputedType;
|
import it.cavallium.datagen.plugin.ComputedType;
|
||||||
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
||||||
@ -44,7 +45,7 @@ public class GenUpgraderSuperX extends ClassGenerator {
|
|||||||
|
|
||||||
classBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
|
classBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
|
||||||
|
|
||||||
classBuilder.addSuperinterface(ParameterizedTypeName.get(ClassName.get(DataUpgrader.class),
|
classBuilder.superclass(ParameterizedTypeName.get(ClassName.get(DataUpgraderSimple.class),
|
||||||
typeBaseClassName,
|
typeBaseClassName,
|
||||||
nextTypeSuper.getJTypeName(basePackageName)
|
nextTypeSuper.getJTypeName(basePackageName)
|
||||||
));
|
));
|
||||||
|
@ -11,6 +11,7 @@ import com.squareup.javapoet.TypeSpec.Builder;
|
|||||||
import com.squareup.javapoet.TypeVariableName;
|
import com.squareup.javapoet.TypeVariableName;
|
||||||
import it.cavallium.datagen.DataSerializer;
|
import it.cavallium.datagen.DataSerializer;
|
||||||
import it.cavallium.datagen.DataUpgrader;
|
import it.cavallium.datagen.DataUpgrader;
|
||||||
|
import it.cavallium.datagen.DataUpgraderSimple;
|
||||||
import it.cavallium.datagen.plugin.ClassGenerator;
|
import it.cavallium.datagen.plugin.ClassGenerator;
|
||||||
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
||||||
import it.cavallium.datagen.plugin.ComputedTypeArrayFixed;
|
import it.cavallium.datagen.plugin.ComputedTypeArrayFixed;
|
||||||
@ -188,7 +189,7 @@ public class GenVersion extends ClassGenerator {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var genericClassName = ParameterizedTypeName.get(ClassName.get(DataUpgrader.class),
|
var genericClassName = ParameterizedTypeName.get(ClassName.get(DataUpgraderSimple.class),
|
||||||
type.getJTypeName(basePackageName), nextVersion.getJTypeName(basePackageName)
|
type.getJTypeName(basePackageName), nextVersion.getJTypeName(basePackageName)
|
||||||
);
|
);
|
||||||
var upgraderClassName = type.getJUpgraderName(basePackageName);
|
var upgraderClassName = type.getJUpgraderName(basePackageName);
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
package it.cavallium.datagen;
|
||||||
|
|
||||||
|
public interface DataContext {
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package it.cavallium.datagen;
|
||||||
|
|
||||||
|
public final class DataContextNone implements DataContext {
|
||||||
|
|
||||||
|
public static final DataContextNone INSTANCE = new DataContextNone();
|
||||||
|
|
||||||
|
private DataContextNone() {}
|
||||||
|
|
||||||
|
}
|
@ -2,7 +2,7 @@ package it.cavallium.datagen;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public interface DataInitializer<T> {
|
public interface DataInitializer<C extends DataContext, T> {
|
||||||
|
|
||||||
@NotNull T initialize();
|
@NotNull T initialize(C context);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
package it.cavallium.datagen;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class DataInitializerSimple<T> implements DataInitializer<DataContextNone, T> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final @NotNull T initialize(DataContextNone context) {
|
||||||
|
return initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract @NotNull T initialize();
|
||||||
|
}
|
@ -2,7 +2,7 @@ package it.cavallium.datagen;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public interface DataUpgrader<T, U> {
|
public interface DataUpgrader<C extends DataContext, T, U> {
|
||||||
|
|
||||||
@NotNull U upgrade(@NotNull T data);
|
@NotNull U upgrade(@NotNull C context, @NotNull T data);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
package it.cavallium.datagen;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class DataUpgraderSimple<T, U> implements DataUpgrader<DataContextNone, T, U> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final @NotNull U upgrade(@NotNull DataContextNone context, @NotNull T data) {
|
||||||
|
return this.upgrade(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract @NotNull U upgrade(@NotNull T data);
|
||||||
|
}
|
@ -1,11 +1,19 @@
|
|||||||
package it.cavallium.datagen.nativedata;
|
package it.cavallium.datagen.nativedata;
|
||||||
|
|
||||||
|
import it.cavallium.datagen.DataContext;
|
||||||
|
import it.cavallium.datagen.DataContextNone;
|
||||||
import it.cavallium.datagen.DataUpgrader;
|
import it.cavallium.datagen.DataUpgrader;
|
||||||
|
import it.cavallium.datagen.DataUpgraderSimple;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class UpgradeUtil {
|
public class UpgradeUtil {
|
||||||
|
public static <A, B> List<B> upgradeArray(List<A> from, DataUpgraderSimple<A, B> upgrader) {
|
||||||
|
return upgradeArray(DataContextNone.INSTANCE, from, upgrader);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <A, B> List<B> upgradeArray(List<A> from, DataUpgrader<A, B> upgrader) {
|
public static <C extends DataContext, A, B> List<B> upgradeArray(C context, List<A> from, DataUpgrader<C, A, B> upgrader) {
|
||||||
Object[] array;
|
Object[] array;
|
||||||
if (from.getClass() == ImmutableWrappedArrayList.class
|
if (from.getClass() == ImmutableWrappedArrayList.class
|
||||||
&& ((ImmutableWrappedArrayList<?>) from).a.getClass() == Object[].class) {
|
&& ((ImmutableWrappedArrayList<?>) from).a.getClass() == Object[].class) {
|
||||||
@ -14,16 +22,20 @@ public class UpgradeUtil {
|
|||||||
array = from.toArray();
|
array = from.toArray();
|
||||||
}
|
}
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
array[i] = (B) upgrader.upgrade((A) array[i]);
|
array[i] = (B) upgrader.upgrade(context, (A) array[i]);
|
||||||
}
|
}
|
||||||
return (ImmutableWrappedArrayList<B>) ImmutableWrappedArrayList.of(array);
|
return (ImmutableWrappedArrayList<B>) ImmutableWrappedArrayList.of(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <A, B> B upgradeNullable(A nullableValue, DataUpgrader<A, B> upgrader) {
|
public static <A, B> B upgradeNullable(A nullableValue, DataUpgraderSimple<A, B> upgrader) {
|
||||||
|
return upgradeNullable(DataContextNone.INSTANCE, nullableValue, upgrader);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <C extends DataContext, A, B> B upgradeNullable(C context, A nullableValue, DataUpgrader<C, A, B> upgrader) {
|
||||||
if (nullableValue == null) {
|
if (nullableValue == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return upgrader.upgrade(nullableValue);
|
return upgrader.upgrade(context, nullableValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user