This commit is contained in:
Andrea Cavalli 2023-01-18 02:38:12 +01:00
parent 9c604a0158
commit f6fe7017c7
3 changed files with 58 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectCollection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -178,15 +179,19 @@ public class DataModel {
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown type: "
+ t.transformClass);
}
String definition = transformClass.data.remove(t.from);
if (definition == null) {
var definition = removeAndGetIndex(transformClass.data, t.from);
if (definition.isEmpty()) {
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown field: " + t.from);
}
var prevDef = transformClass.data.put(t.to, definition);
var prevDef = tryInsertAtIndex(transformClass.data,
t.to,
definition.get().getValue(),
definition.get().getKey()
);
if (prevDef != null) {
throw new IllegalArgumentException(
transformCoordinate + " tries to overwrite the existing field \"" + t.to + "\" of value \""
+ prevDef + "\" with the field \"" + t.from + "\" of type \"" + definition + "\"");
+ prevDef + "\" with the field \"" + t.from + "\" of type \"" + definition.orElse(null) + "\"");
}
}
case "new-data" -> {
@ -199,7 +204,12 @@ public class DataModel {
if (!allTypes.contains(extractTypeName(t.type))) {
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown type: " + t.type);
}
var prevDef = transformClass.data.put(t.to, fixType(t.type));
String prevDef;
if (t.index != null) {
prevDef = tryInsertAtIndex(transformClass.data, t.to, fixType(t.type), t.index);
} else {
prevDef = transformClass.data.putIfAbsent(t.to, fixType(t.type));
}
if (prevDef != null) {
throw new IllegalArgumentException(transformCoordinate + " tries to overwrite the existing field \""
+ t.to + "\" of value \"" + prevDef
@ -229,7 +239,7 @@ public class DataModel {
if (!allTypes.contains(extractTypeName(t.type))) {
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown type: " + t.type);
}
String prevDefinition = transformClass.data.put(t.from, fixType(t.type));
String prevDefinition = transformClass.data.replace(t.from, fixType(t.type));
if (prevDefinition == null) {
throw new IllegalArgumentException(transformCoordinate + " refers to an unknown field: " + t.from);
}
@ -266,6 +276,40 @@ public class DataModel {
this.customTypes = customTypesData;
}
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
public static String getNextVersion(Map<String, String> versionsSequence, String version) {
return versionsSequence.get(version);

View File

@ -1,6 +1,7 @@
package it.cavallium.data.generator.plugin;
import java.util.Objects;
import org.jetbrains.annotations.Nullable;
public class NewDataConfiguration implements TransformationConfiguration {
@ -8,6 +9,8 @@ public class NewDataConfiguration implements TransformationConfiguration {
public String to;
public String type;
public String initializer;
@Nullable
public Integer index;
@Override
public String getTransformClass() {
@ -29,7 +32,8 @@ public class NewDataConfiguration implements TransformationConfiguration {
}
NewDataConfiguration that = (NewDataConfiguration) o;
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(index, that.index);
}
@Override
@ -39,6 +43,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
hash += ConfigUtils.hashCode(to);
hash += ConfigUtils.hashCode(type);
hash += ConfigUtils.hashCode(initializer);
hash += ConfigUtils.hashCode(index);
return hash;
}
@ -50,6 +55,7 @@ public class NewDataConfiguration implements TransformationConfiguration {
if (this.initializer != null) c.initializer = this.initializer;
if (this.to != null) c.to = this.to;
if (this.type != null) c.type = this.type;
if (this.index != null) c.index = this.index;
return c;
}
}

View File

@ -1,4 +1,4 @@
package it.cavallium.data.generator;
package it.cavallium.data.generator.plugin;
import it.cavallium.data.generator.plugin.SourcesGenerator;
import java.io.IOException;