Rework beacon exchanges

This commit is contained in:
Christopher Schnick
2022-02-04 01:04:08 +01:00
parent b2af324993
commit 40f9de1dc3
13 changed files with 229 additions and 34 deletions
@@ -22,7 +22,6 @@ import lombok.Getter;
public class DataSourceId {
public static final char SEPARATOR = ':';
public static final DataSourceId ANONYMOUS = new DataSourceId(null, null);
private final String collectionName;
private final String entryName;
@@ -41,10 +40,6 @@ public class DataSourceId {
* @throws IllegalArgumentException if any name is not valid
*/
public static DataSourceId create(String collectionName, String entryName) {
if (collectionName == null && entryName == null) {
return ANONYMOUS;
}
if (collectionName != null && collectionName.trim().length() == 0) {
throw new IllegalArgumentException("Trimmed collection name is empty");
}
@@ -77,10 +72,6 @@ public class DataSourceId {
throw new IllegalArgumentException("String is null");
}
if (s.equals(String.valueOf(SEPARATOR))) {
return ANONYMOUS;
}
var split = s.split(String.valueOf(SEPARATOR));
if (split.length != 2) {
throw new IllegalArgumentException("Data source id must contain exactly one " + SEPARATOR);
@@ -98,6 +89,6 @@ public class DataSourceId {
@Override
public String toString() {
return (collectionName != null ? collectionName.toLowerCase() : "") + SEPARATOR + (entryName != null ? entryName.toLowerCase() : "");
return collectionName.toLowerCase() + SEPARATOR + (entryName != null ? entryName.toLowerCase() : "");
}
}
@@ -0,0 +1,167 @@
package io.xpipe.core.source;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.Value;
import java.util.Objects;
public interface DataSourceReference {
static DataSourceReference empty() {
return new Empty();
}
public static DataSourceReference parse(String s) {
if (s == null || s.trim().length() == 0) {
return new Empty();
}
if (s.contains(":")) {
return new Id(DataSourceId.fromString(s));
}
return new Name(s);
}
enum Type {
ID,
NAME,
EMPTY
}
Type getType();
DataSourceId getId();
String getName();
String toRefString();
String toString();
@Value
@AllArgsConstructor
static class Id implements DataSourceReference {
@NonNull
DataSourceId value;
@Override
public String toString() {
return toRefString();
}
@Override
public String toRefString() {
return value.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Id id = (Id) o;
return value.equals(id.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public Type getType() {
return Type.ID;
}
@Override
public DataSourceId getId() {
return value;
}
@Override
public String getName() {
throw new UnsupportedOperationException();
}
}
@Value
@AllArgsConstructor
static class Name implements DataSourceReference {
@NonNull
String value;
@Override
public String toString() {
return toRefString();
}
@Override
public String toRefString() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Name n = (Name) o;
return value.equals(n.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public Type getType() {
return Type.NAME;
}
@Override
public DataSourceId getId() {
throw new UnsupportedOperationException();
}
@Override
public String getName() {
return value;
}
}
static class Empty implements DataSourceReference {
@Override
public String toRefString() {
return "";
}
@Override
public String toString() {
return "none";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o != null && getClass() == o.getClass();
}
@Override
public int hashCode() {
return 1;
}
@Override
public Type getType() {
return Type.EMPTY;
}
@Override
public DataSourceId getId() {
throw new UnsupportedOperationException();
}
@Override
public String getName() {
throw new UnsupportedOperationException();
}
}
}
@@ -13,6 +13,7 @@ import io.xpipe.core.data.type.ArrayType;
import io.xpipe.core.data.type.TupleType;
import io.xpipe.core.data.type.ValueType;
import io.xpipe.core.source.DataSourceInfo;
import io.xpipe.core.source.DataSourceReference;
import io.xpipe.core.store.LocalFileDataStore;
import java.io.IOException;
@@ -37,7 +38,31 @@ public class CoreJacksonModule extends SimpleModule {
addSerializer(Path.class, new LocalPathSerializer());
addDeserializer(Path.class, new LocalPathDeserializer());
addSerializer(DataSourceReference.class, new DataSourceReferenceSerializer());
addDeserializer(DataSourceReference.class, new DataSourceReferenceDeserializer());
context.setMixInAnnotations(Throwable.class, ThrowableTypeMixIn.class);
context.setMixInAnnotations(DataSourceReference.class, DataSourceReferenceTypeMixIn.class);
context.addSerializers(_serializers);
context.addDeserializers(_deserializers);
}
public static class DataSourceReferenceSerializer extends JsonSerializer<DataSourceReference> {
@Override
public void serialize(DataSourceReference value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeString(value.toRefString());
}
}
public static class DataSourceReferenceDeserializer extends JsonDeserializer<DataSourceReference> {
@Override
public DataSourceReference deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return DataSourceReference.parse(p.getValueAsString());
}
}
public static class CharsetSerializer extends JsonSerializer<Charset> {
@@ -77,4 +102,8 @@ public class CoreJacksonModule extends SimpleModule {
@JsonSerialize(as = Throwable.class)
public abstract static class ThrowableTypeMixIn {
}
@JsonSerialize(as = DataSourceReference.class)
public abstract static class DataSourceReferenceTypeMixIn {
}
}