diff --git a/core/src/main/java/io/xpipe/core/data/DataStructureNodePointer.java b/core/src/main/java/io/xpipe/core/data/DataStructureNodePointer.java index 50636a664..e780a0bd1 100644 --- a/core/src/main/java/io/xpipe/core/data/DataStructureNodePointer.java +++ b/core/src/main/java/io/xpipe/core/data/DataStructureNodePointer.java @@ -61,7 +61,7 @@ public class DataStructureNodePointer { return path; } - public static interface Element { + public interface Element { DataStructureNode tryMatch(DataStructureNode n); @@ -70,7 +70,7 @@ public class DataStructureNodePointer { } } - public static final record NameElement(String name) implements Element { + public record NameElement(String name) implements Element { @Override public DataStructureNode tryMatch(DataStructureNode n) { @@ -88,7 +88,7 @@ public class DataStructureNodePointer { } } - public static final record IndexElement(int index) implements Element { + public record IndexElement(int index) implements Element { @Override public DataStructureNode tryMatch(DataStructureNode n) { @@ -104,7 +104,7 @@ public class DataStructureNodePointer { } } - public static final record SupplierElement(Supplier keySupplier) implements Element { + public record SupplierElement(Supplier keySupplier) implements Element { @Override public DataStructureNode tryMatch(DataStructureNode n) { @@ -126,7 +126,7 @@ public class DataStructureNodePointer { } } - public static final record FunctionElement(Function keyFunc) implements Element { + public record FunctionElement(Function keyFunc) implements Element { @Override public DataStructureNode tryMatch(DataStructureNode n) { @@ -148,7 +148,7 @@ public class DataStructureNodePointer { } } - public static final record SelectorElement(Predicate selector) implements Element { + public record SelectorElement(Predicate selector) implements Element { @Override public DataStructureNode tryMatch(DataStructureNode n) { diff --git a/core/src/main/java/io/xpipe/core/data/generic/GenericArrayReader.java b/core/src/main/java/io/xpipe/core/data/generic/GenericArrayReader.java index 472c8f6ee..ca79e1a43 100644 --- a/core/src/main/java/io/xpipe/core/data/generic/GenericArrayReader.java +++ b/core/src/main/java/io/xpipe/core/data/generic/GenericArrayReader.java @@ -9,22 +9,21 @@ import java.util.List; public class GenericArrayReader implements GenericAbstractReader { - public static GenericArrayReader newReader(int length) { - var ar = new GenericArrayReader(); - ar.onArrayStart(length); - return ar; - } - private boolean initialized; private List nodes; private int length; private int currentIndex = 0; private GenericAbstractReader currentReader; private DataStructureNode created; - public GenericArrayReader() { } + public static GenericArrayReader newReader(int length) { + var ar = new GenericArrayReader(); + ar.onArrayStart(length); + return ar; + } + private void init(int length) { this.length = length; this.nodes = new ArrayList<>(length); diff --git a/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamCallback.java b/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamCallback.java index 1ce49a29e..14ddfbe6b 100644 --- a/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamCallback.java +++ b/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamCallback.java @@ -2,7 +2,8 @@ package io.xpipe.core.data.generic; public interface GenericDataStreamCallback { - default void onName(String name) {} + default void onName(String name) { + } default void onArrayStart(int length) { } diff --git a/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamParser.java b/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamParser.java index 1976acf4c..e924bb58e 100644 --- a/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamParser.java +++ b/core/src/main/java/io/xpipe/core/data/generic/GenericDataStreamParser.java @@ -19,7 +19,7 @@ public class GenericDataStreamParser { public static List readN(InputStream in, int n) throws IOException { var list = new ArrayList(); var reader = new GenericDataStructureNodeReader(); - for (int i = 0; i < n ; i++) { + for (int i = 0; i < n; i++) { read(in, reader); list.add(reader.create()); } diff --git a/core/src/main/java/io/xpipe/core/data/generic/GenericTupleReader.java b/core/src/main/java/io/xpipe/core/data/generic/GenericTupleReader.java index 97e2291d2..91482d266 100644 --- a/core/src/main/java/io/xpipe/core/data/generic/GenericTupleReader.java +++ b/core/src/main/java/io/xpipe/core/data/generic/GenericTupleReader.java @@ -1,21 +1,15 @@ package io.xpipe.core.data.generic; import io.xpipe.core.data.DataStructureNode; +import io.xpipe.core.data.node.SimpleTupleNode; import io.xpipe.core.data.node.TupleNode; import io.xpipe.core.data.node.ValueNode; -import io.xpipe.core.data.node.SimpleTupleNode; import java.util.ArrayList; import java.util.List; public class GenericTupleReader implements GenericAbstractReader { - public static GenericTupleReader newReader(int length) { - var tr = new GenericTupleReader(); - tr.onTupleStart(length); - return tr; - } - private boolean initialized; private int length; private List names; @@ -23,10 +17,15 @@ public class GenericTupleReader implements GenericAbstractReader { private int currentIndex = 0; private GenericAbstractReader currentReader; private DataStructureNode created; - public GenericTupleReader() { } + public static GenericTupleReader newReader(int length) { + var tr = new GenericTupleReader(); + tr.onTupleStart(length); + return tr; + } + private boolean hasReader() { return currentReader != null; } diff --git a/core/src/main/java/io/xpipe/core/data/node/SimpleTupleNode.java b/core/src/main/java/io/xpipe/core/data/node/SimpleTupleNode.java index cb6937b35..a9ef1ee79 100644 --- a/core/src/main/java/io/xpipe/core/data/node/SimpleTupleNode.java +++ b/core/src/main/java/io/xpipe/core/data/node/SimpleTupleNode.java @@ -5,7 +5,10 @@ import io.xpipe.core.data.type.DataType; import io.xpipe.core.data.type.TupleType; import lombok.EqualsAndHashCode; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; @EqualsAndHashCode(callSuper = false) public class SimpleTupleNode extends TupleNode { diff --git a/core/src/main/java/io/xpipe/core/data/node/TupleNode.java b/core/src/main/java/io/xpipe/core/data/node/TupleNode.java index eb5b52091..e29675530 100644 --- a/core/src/main/java/io/xpipe/core/data/node/TupleNode.java +++ b/core/src/main/java/io/xpipe/core/data/node/TupleNode.java @@ -10,38 +10,6 @@ import java.util.stream.Collectors; public abstract class TupleNode extends DataStructureNode { - @Value - public static class KeyValue { - - String key; - DataStructureNode value; - } - - public static class Builder { - - private final List entries = new ArrayList<>(); - - public Builder add(String name, DataStructureNode node) { - Objects.requireNonNull(node); - entries.add(new KeyValue(name, node)); - return this; - } - - public Builder add(DataStructureNode node) { - Objects.requireNonNull(node); - entries.add(new KeyValue(null, node)); - return this; - } - - public TupleNode build() { - boolean hasKeys = entries.stream().anyMatch(kv -> kv.key != null); - return hasKeys ? TupleNode.wrap( - entries.stream().map(kv -> kv.key).toList(), - entries.stream().map(kv -> kv.value).toList()) : - TupleNode.wrap(entries.stream().map(kv -> kv.value).toList()); - } - } - public static Builder builder() { return new Builder(); } @@ -112,4 +80,36 @@ public abstract class TupleNode extends DataStructureNode { public abstract List getNames(); public abstract List getNodes(); + + @Value + public static class KeyValue { + + String key; + DataStructureNode value; + } + + public static class Builder { + + private final List entries = new ArrayList<>(); + + public Builder add(String name, DataStructureNode node) { + Objects.requireNonNull(node); + entries.add(new KeyValue(name, node)); + return this; + } + + public Builder add(DataStructureNode node) { + Objects.requireNonNull(node); + entries.add(new KeyValue(null, node)); + return this; + } + + public TupleNode build() { + boolean hasKeys = entries.stream().anyMatch(kv -> kv.key != null); + return hasKeys ? TupleNode.wrap( + entries.stream().map(kv -> kv.key).toList(), + entries.stream().map(kv -> kv.value).toList()) : + TupleNode.wrap(entries.stream().map(kv -> kv.value).toList()); + } + } } diff --git a/core/src/main/java/io/xpipe/core/data/node/ValueNode.java b/core/src/main/java/io/xpipe/core/data/node/ValueNode.java index 39117482d..e72e94fbb 100644 --- a/core/src/main/java/io/xpipe/core/data/node/ValueNode.java +++ b/core/src/main/java/io/xpipe/core/data/node/ValueNode.java @@ -10,7 +10,10 @@ import java.nio.charset.StandardCharsets; @EqualsAndHashCode(callSuper = false) public abstract class ValueNode extends DataStructureNode { - private static final byte[] NULL = new byte[] {0}; + private static final byte[] NULL = new byte[]{0}; + + protected ValueNode() { + } public static ValueNode immutable(byte[] data) { return new ImmutableValueNode(data); @@ -40,9 +43,6 @@ public abstract class ValueNode extends DataStructureNode { return mutable(o); } - protected ValueNode() { - } - @Override public abstract DataStructureNode setRawData(byte[] data); diff --git a/core/src/main/java/io/xpipe/core/data/type/ArrayType.java b/core/src/main/java/io/xpipe/core/data/type/ArrayType.java index 872f345e0..e9909b5d7 100644 --- a/core/src/main/java/io/xpipe/core/data/type/ArrayType.java +++ b/core/src/main/java/io/xpipe/core/data/type/ArrayType.java @@ -11,6 +11,12 @@ import java.util.List; @EqualsAndHashCode public class ArrayType implements DataType { + private final DataType sharedType; + + public ArrayType(DataType sharedType) { + this.sharedType = sharedType; + } + public static ArrayType ofWildcard() { return new ArrayType(WildcardType.of()); } @@ -25,12 +31,6 @@ public class ArrayType implements DataType { return new ArrayType(eq ? first : WildcardType.of()); } - private final DataType sharedType; - - public ArrayType(DataType sharedType) { - this.sharedType = sharedType; - } - public boolean isSimple() { return hasSharedType() && getSharedType().isValue(); } diff --git a/core/src/main/java/io/xpipe/core/data/type/TupleType.java b/core/src/main/java/io/xpipe/core/data/type/TupleType.java index 63805393d..7bb880221 100644 --- a/core/src/main/java/io/xpipe/core/data/type/TupleType.java +++ b/core/src/main/java/io/xpipe/core/data/type/TupleType.java @@ -13,8 +13,8 @@ import java.util.List; @EqualsAndHashCode public class TupleType implements DataType { - private List names; - private List types; + private final List names; + private final List types; @JsonCreator private TupleType(List names, List types) { diff --git a/core/src/main/java/io/xpipe/core/data/type/ValueType.java b/core/src/main/java/io/xpipe/core/data/type/ValueType.java index 697920268..40bd8a2af 100644 --- a/core/src/main/java/io/xpipe/core/data/type/ValueType.java +++ b/core/src/main/java/io/xpipe/core/data/type/ValueType.java @@ -9,14 +9,14 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode public class ValueType implements DataType { - public static ValueType of() { - return new ValueType(); - } - private ValueType() { } + public static ValueType of() { + return new ValueType(); + } + @Override public String getName() { return "value"; diff --git a/core/src/main/java/io/xpipe/core/data/type/WildcardType.java b/core/src/main/java/io/xpipe/core/data/type/WildcardType.java index e3b6aaa63..9f329ebd9 100644 --- a/core/src/main/java/io/xpipe/core/data/type/WildcardType.java +++ b/core/src/main/java/io/xpipe/core/data/type/WildcardType.java @@ -5,14 +5,14 @@ import io.xpipe.core.data.type.callback.DataTypeCallback; public class WildcardType implements DataType { - public static WildcardType of() { - return new WildcardType(); - } - private WildcardType() { } + public static WildcardType of() { + return new WildcardType(); + } + @Override public String getName() { return "wildcard"; diff --git a/core/src/main/java/io/xpipe/core/data/type/callback/DataTypeCallback.java b/core/src/main/java/io/xpipe/core/data/type/callback/DataTypeCallback.java index f9b3ed6c1..81144daf4 100644 --- a/core/src/main/java/io/xpipe/core/data/type/callback/DataTypeCallback.java +++ b/core/src/main/java/io/xpipe/core/data/type/callback/DataTypeCallback.java @@ -9,7 +9,7 @@ import java.util.function.Consumer; public interface DataTypeCallback { - public static DataTypeCallback flatten(Consumer typeConsumer) { + static DataTypeCallback flatten(Consumer typeConsumer) { return new DataTypeCallback() { @Override public void onValue() { diff --git a/core/src/main/java/io/xpipe/core/data/type/callback/FlatArrayTypeCallback.java b/core/src/main/java/io/xpipe/core/data/type/callback/FlatArrayTypeCallback.java index b8d962efb..7551a2efc 100644 --- a/core/src/main/java/io/xpipe/core/data/type/callback/FlatArrayTypeCallback.java +++ b/core/src/main/java/io/xpipe/core/data/type/callback/FlatArrayTypeCallback.java @@ -46,7 +46,7 @@ public class FlatArrayTypeCallback implements DataTypeCallback { arrayDepth++; } - public static interface FlatCallback { + public interface FlatCallback { default void onValue() { } diff --git a/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamParser.java b/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamParser.java index d62297309..22c5298de 100644 --- a/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamParser.java +++ b/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamParser.java @@ -14,6 +14,13 @@ import java.util.function.Consumer; public class TypedDataStreamParser { + private final DataType dataType; + private GenericDataStructureNodeReader genericReader; + + public TypedDataStreamParser(DataType dataType) { + this.dataType = dataType; + } + public boolean hasNext(InputStream in) throws IOException { var b = in.read(); if (b == -1) { @@ -98,13 +105,6 @@ public class TypedDataStreamParser { cb.onTupleEnd(); } - private DataType dataType; - private GenericDataStructureNodeReader genericReader; - - public TypedDataStreamParser(DataType dataType) { - this.dataType = dataType; - } - private GenericDataStructureNodeReader getGenericReader() { if (genericReader == null) { genericReader = new GenericDataStructureNodeReader(); diff --git a/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamWriter.java b/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamWriter.java index d0d0b1b2f..7ffa13c97 100644 --- a/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamWriter.java +++ b/core/src/main/java/io/xpipe/core/data/typed/TypedDataStreamWriter.java @@ -23,11 +23,9 @@ public class TypedDataStreamWriter { private static void write(OutputStream out, DataStructureNode node, DataType type) throws IOException { if (type.isTuple() && node.isTuple()) { writeTuple(out, (SimpleTupleNode) node, (TupleType) type); - } - else if (node.isArray() && type.isArray()) { + } else if (node.isArray() && type.isArray()) { writeArray(out, (ArrayNode) node, (ArrayType) type); - } - else if (node.isValue() && type.isValue()) { + } else if (node.isValue() && type.isValue()) { writeValue(out, (ValueNode) node); } else { throw new AssertionError(); diff --git a/core/src/main/java/io/xpipe/core/data/typed/TypedDataStructureNodeReader.java b/core/src/main/java/io/xpipe/core/data/typed/TypedDataStructureNodeReader.java index 77ef2ea17..36558b1c0 100644 --- a/core/src/main/java/io/xpipe/core/data/typed/TypedDataStructureNodeReader.java +++ b/core/src/main/java/io/xpipe/core/data/typed/TypedDataStructureNodeReader.java @@ -17,23 +17,14 @@ import java.util.Stack; public class TypedDataStructureNodeReader implements TypedAbstractReader { - public static TypedDataStructureNodeReader mutable(DataType type) { - return new TypedDataStructureNodeReader(type, false); - } - - public static TypedDataStructureNodeReader immutable(DataType type) { - return new TypedDataStructureNodeReader(type, true); - } - - private int currentDataTypeIndex; private final List flattened; private final Stack> children; private final Stack nodes; + private final boolean makeImmutable; + private int currentDataTypeIndex; private DataStructureNode readNode; private boolean initialized; private int arrayDepth; - private final boolean makeImmutable; - private TypedDataStructureNodeReader(DataType type, boolean makeImmutable) { flattened = new ArrayList<>(); children = new Stack<>(); @@ -42,6 +33,14 @@ public class TypedDataStructureNodeReader implements TypedAbstractReader { this.makeImmutable = makeImmutable; } + public static TypedDataStructureNodeReader mutable(DataType type) { + return new TypedDataStructureNodeReader(type, false); + } + + public static TypedDataStructureNodeReader immutable(DataType type) { + return new TypedDataStructureNodeReader(type, true); + } + @Override public void onNodeBegin() { if (nodes.size() != 0 || children.size() != 0) { diff --git a/core/src/main/java/io/xpipe/core/data/typed/TypedReusableDataStructureNodeReader.java b/core/src/main/java/io/xpipe/core/data/typed/TypedReusableDataStructureNodeReader.java index 0d1acf7e8..7350f0b43 100644 --- a/core/src/main/java/io/xpipe/core/data/typed/TypedReusableDataStructureNodeReader.java +++ b/core/src/main/java/io/xpipe/core/data/typed/TypedReusableDataStructureNodeReader.java @@ -13,10 +13,10 @@ import java.util.Stack; public class TypedReusableDataStructureNodeReader implements TypedAbstractReader { - private TypedDataStructureNodeReader initialReader; - private DataStructureNode node; private final List flattened; - private Stack indices; + private final TypedDataStructureNodeReader initialReader; + private DataStructureNode node; + private final Stack indices; private int arrayDepth; public TypedReusableDataStructureNodeReader(DataType type) { diff --git a/core/src/main/java/io/xpipe/core/source/DataSourceId.java b/core/src/main/java/io/xpipe/core/source/DataSourceId.java index c327c57ce..72f5b8e97 100644 --- a/core/src/main/java/io/xpipe/core/source/DataSourceId.java +++ b/core/src/main/java/io/xpipe/core/source/DataSourceId.java @@ -7,7 +7,7 @@ import lombok.Getter; /** * Represents a reference to an XPipe data source. * This reference consists out of a collection name and an entry name to allow for better organisation. - * + *

* To allow for a simple usage of data source ids, the collection and entry names are trimmed and * converted to lower case names when creating them. * The two names are separated by a colon and are therefore not allowed to contain colons themselves. @@ -20,12 +20,19 @@ import lombok.Getter; public class DataSourceId { public static final char SEPARATOR = ':'; + private final String collectionName; + private final String entryName; + @JsonCreator + private DataSourceId(String collectionName, String entryName) { + this.collectionName = collectionName; + this.entryName = entryName; + } /** * Creates a new data source id from a collection name and an entry name. * * @param collectionName the collection name, which must be not null and not empty - * @param entryName the entry name, which must be not null and not empty + * @param entryName the entry name, which must be not null and not empty * @throws IllegalArgumentException if any name is not valid */ public static DataSourceId create(String collectionName, String entryName) { @@ -52,15 +59,6 @@ public class DataSourceId { return new DataSourceId(collectionName, entryName); } - private final String collectionName; - private final String entryName; - - @JsonCreator - private DataSourceId(String collectionName, String entryName) { - this.collectionName = collectionName; - this.entryName = entryName; - } - /** * Creates a new data source id from a string representation. * The string must contain exactly one colon and non-empty names. diff --git a/core/src/main/java/io/xpipe/core/store/LocalFileDataStore.java b/core/src/main/java/io/xpipe/core/store/LocalFileDataStore.java index 85150b43a..65dad077f 100644 --- a/core/src/main/java/io/xpipe/core/store/LocalFileDataStore.java +++ b/core/src/main/java/io/xpipe/core/store/LocalFileDataStore.java @@ -1,6 +1,8 @@ package io.xpipe.core.store; -import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonTypeName; import org.apache.commons.io.FilenameUtils; import java.io.IOException; diff --git a/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java b/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java index 50860441b..a5b47e88a 100644 --- a/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java +++ b/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java @@ -20,40 +20,6 @@ import java.nio.file.Path; public class CoreJacksonModule extends SimpleModule { - public static class CharsetSerializer extends JsonSerializer { - - @Override - public void serialize(Charset value, JsonGenerator jgen, SerializerProvider provider) - throws IOException, JsonProcessingException { - jgen.writeString(value.name()); - } - } - - public static class CharsetDeserializer extends JsonDeserializer { - - @Override - public Charset deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { - return Charset.forName(p.getValueAsString()); - } - } - - public static class LocalPathSerializer extends JsonSerializer { - - @Override - public void serialize(Path value, JsonGenerator jgen, SerializerProvider provider) - throws IOException, JsonProcessingException { - jgen.writeString(value.toString()); - } - } - - public static class LocalPathDeserializer extends JsonDeserializer { - - @Override - public Path deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { - return Path.of(p.getValueAsString()); - } - } - @Override public void setupModule(SetupContext context) { context.registerSubtypes( @@ -69,4 +35,38 @@ public class CoreJacksonModule extends SimpleModule { addSerializer(Path.class, new LocalPathSerializer()); addDeserializer(Path.class, new LocalPathDeserializer()); } + + public static class CharsetSerializer extends JsonSerializer { + + @Override + public void serialize(Charset value, JsonGenerator jgen, SerializerProvider provider) + throws IOException { + jgen.writeString(value.name()); + } + } + + public static class CharsetDeserializer extends JsonDeserializer { + + @Override + public Charset deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + return Charset.forName(p.getValueAsString()); + } + } + + public static class LocalPathSerializer extends JsonSerializer { + + @Override + public void serialize(Path value, JsonGenerator jgen, SerializerProvider provider) + throws IOException { + jgen.writeString(value.toString()); + } + } + + public static class LocalPathDeserializer extends JsonDeserializer { + + @Override + public Path deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + return Path.of(p.getValueAsString()); + } + } } diff --git a/core/src/main/java/io/xpipe/core/util/JacksonHelper.java b/core/src/main/java/io/xpipe/core/util/JacksonHelper.java index cf6ae4be4..31af0bd17 100644 --- a/core/src/main/java/io/xpipe/core/util/JacksonHelper.java +++ b/core/src/main/java/io/xpipe/core/util/JacksonHelper.java @@ -28,8 +28,7 @@ public class JacksonHelper { init = true; } - private static List findModules(ModuleLayer layer) - { + private static List findModules(ModuleLayer layer) { ArrayList modules = new ArrayList(); ServiceLoader loader = ServiceLoader.load(layer, Module.class); for (Module module : loader) {