Skip to content

Commit 987df9d

Browse files
author
flexca
committed
Add first serialization test
1 parent 76e5be3 commit 987df9d

26 files changed

Lines changed: 348 additions & 107 deletions
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
package com.github.flexca.enot.core.element.value;
22

3+
import com.github.flexca.enot.core.element.value.converter.BinaryToBinaryConverter;
4+
import com.github.flexca.enot.core.element.value.converter.BooleanToBinaryConverter;
5+
import com.github.flexca.enot.core.element.value.converter.EmptyToBinaryConverter;
6+
import com.github.flexca.enot.core.element.value.converter.IntegerToBinaryConverter;
7+
import com.github.flexca.enot.core.element.value.converter.TextToBinaryConverter;
8+
import com.github.flexca.enot.core.element.value.converter.UnsupportedToBinaryConverter;
9+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
10+
311
import java.util.Collections;
412
import java.util.Set;
513

614
public enum CommonEnotValueType implements EnotValueType {
715

8-
BOOLEAN("boolean", Collections.emptySet(), true),
9-
BINARY("binary", Collections.emptySet(), false),
10-
INTEGER("integer", Collections.emptySet(), true),
11-
TEXT("text", Collections.emptySet(), true),
12-
PLACEHOLDER("placeholder", Collections.emptySet(), false),
13-
OBJECT_IDENTIFIER("object_identifier", Collections.emptySet(), false),
14-
DATE_TIME("date_time", Collections.emptySet(), false),
15-
ELEMENT("element", Collections.emptySet(), false),
16-
EMPTY("empty", Collections.emptySet(), false);
16+
BOOLEAN("boolean", Collections.emptySet(), true, new BooleanToBinaryConverter()),
17+
BINARY("binary", Collections.emptySet(), false, new BinaryToBinaryConverter()),
18+
INTEGER("integer", Collections.emptySet(), true, new IntegerToBinaryConverter()),
19+
TEXT("text", Collections.emptySet(), true, new TextToBinaryConverter()),
20+
PLACEHOLDER("placeholder", Collections.emptySet(), false, new UnsupportedToBinaryConverter()),
21+
OBJECT_IDENTIFIER("object_identifier", Collections.emptySet(), false, new UnsupportedToBinaryConverter()),
22+
DATE_TIME("date_time", Collections.emptySet(), false, new UnsupportedToBinaryConverter()),
23+
ELEMENT("element", Collections.emptySet(), false, new UnsupportedToBinaryConverter()),
24+
EMPTY("empty", Collections.emptySet(), false, new EmptyToBinaryConverter());
1725

1826
private final String name;
1927
private final Set<EnotValueType> superTypes;
2028
private final boolean allowedForAttributes;
29+
private final EnotBinaryConverter binaryConverter;
2130

22-
private CommonEnotValueType(String name, Set<EnotValueType> superTypes, boolean allowedForAttributes) {
31+
private CommonEnotValueType(String name, Set<EnotValueType> superTypes, boolean allowedForAttributes,
32+
EnotBinaryConverter binaryConverter) {
2333
this.name = name;
2434
this.superTypes = Collections.unmodifiableSet(superTypes);
2535
this.allowedForAttributes = allowedForAttributes;
36+
this.binaryConverter = binaryConverter;
2637
}
2738

2839
@Override
@@ -39,4 +50,9 @@ public Set<EnotValueType> getSuperTypes() {
3950
public boolean isAllowedForAttributes() {
4051
return allowedForAttributes;
4152
}
53+
54+
@Override
55+
public EnotBinaryConverter getBinaryConverter() {
56+
return binaryConverter;
57+
}
4258
}

core/src/main/java/com/github/flexca/enot/core/element/value/EnotValueType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.flexca.enot.core.element.value;
22

3+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
34
import com.github.flexca.enot.core.util.TypeUtils;
45

56
import java.util.Set;
@@ -12,6 +13,8 @@ public interface EnotValueType {
1213

1314
boolean isAllowedForAttributes();
1415

16+
EnotBinaryConverter getBinaryConverter();
17+
1518
default boolean canConsume(EnotValueType candidate) {
1619
return TypeUtils.canConsume(this, candidate);
1720
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.flexca.enot.core.element.value.converter;
2+
3+
import com.github.flexca.enot.core.element.value.CommonEnotValueType;
4+
import com.github.flexca.enot.core.exception.EnotInvalidArgumentException;
5+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
6+
7+
public class BinaryToBinaryConverter implements EnotBinaryConverter {
8+
9+
@Override
10+
public byte[] toBinary(Object input) {
11+
12+
if (input == null) {
13+
return null;
14+
}
15+
if (input instanceof byte[] binaryInput) {
16+
return binaryInput;
17+
} else {
18+
throw new EnotInvalidArgumentException("expecting input of type " + CommonEnotValueType.BINARY.getName());
19+
}
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.flexca.enot.core.element.value.converter;
2+
3+
import com.github.flexca.enot.core.element.value.CommonEnotValueType;
4+
import com.github.flexca.enot.core.exception.EnotInvalidArgumentException;
5+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
6+
7+
public class BooleanToBinaryConverter implements EnotBinaryConverter {
8+
9+
@Override
10+
public byte[] toBinary(Object input) {
11+
12+
if (input == null) {
13+
return null;
14+
}
15+
if (input instanceof Boolean booleanInput) {
16+
byte[] result = new byte[1];
17+
result[0] = booleanInput ? (byte) 0x1 : 0x0;
18+
return result;
19+
} else {
20+
throw new EnotInvalidArgumentException("expecting input of type " + CommonEnotValueType.BOOLEAN.getName());
21+
}
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.flexca.enot.core.element.value.converter;
2+
3+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
4+
5+
public class EmptyToBinaryConverter implements EnotBinaryConverter {
6+
7+
@Override
8+
public byte[] toBinary(Object input) {
9+
return null;
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.flexca.enot.core.element.value.converter;
2+
3+
import com.github.flexca.enot.core.element.value.CommonEnotValueType;
4+
import com.github.flexca.enot.core.exception.EnotInvalidArgumentException;
5+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
6+
7+
import java.math.BigInteger;
8+
9+
public class IntegerToBinaryConverter implements EnotBinaryConverter {
10+
11+
@Override
12+
public byte[] toBinary(Object input) {
13+
14+
if (input == null) {
15+
return null;
16+
}
17+
if (input instanceof Integer integerInput) {
18+
return BigInteger.valueOf(integerInput).toByteArray();
19+
} else if (input instanceof Long longInput) {
20+
return BigInteger.valueOf(longInput).toByteArray();
21+
} else if (input instanceof BigInteger bigIntegerInput) {
22+
return bigIntegerInput.toByteArray();
23+
} else {
24+
throw new EnotInvalidArgumentException("expecting input of type " + CommonEnotValueType.INTEGER.getName());
25+
}
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.flexca.enot.core.element.value.converter;
2+
3+
import com.github.flexca.enot.core.element.value.CommonEnotValueType;
4+
import com.github.flexca.enot.core.exception.EnotInvalidArgumentException;
5+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
6+
7+
import java.nio.charset.StandardCharsets;
8+
9+
public class TextToBinaryConverter implements EnotBinaryConverter {
10+
11+
@Override
12+
public byte[] toBinary(Object input) {
13+
14+
if (input == null) {
15+
return null;
16+
}
17+
if (input instanceof String stringInput) {
18+
return stringInput.getBytes(StandardCharsets.UTF_8);
19+
} else {
20+
throw new EnotInvalidArgumentException("expecting input of type " + CommonEnotValueType.INTEGER.getName());
21+
}
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.flexca.enot.core.element.value.converter;
2+
3+
import com.github.flexca.enot.core.exception.EnotInvalidArgumentException;
4+
import com.github.flexca.enot.core.registry.EnotBinaryConverter;
5+
6+
public class UnsupportedToBinaryConverter implements EnotBinaryConverter {
7+
8+
@Override
9+
public byte[] toBinary(Object input) {
10+
throw new EnotInvalidArgumentException("unsupported conversion to binary");
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.flexca.enot.core.exception;
2+
3+
public class EnotDataConvertingException extends EnotRuntimeException {
4+
5+
public EnotDataConvertingException(String message) {
6+
super(message);
7+
}
8+
9+
public EnotDataConvertingException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.flexca.enot.core.registry;
2+
3+
public interface EnotBinaryConverter {
4+
5+
byte[] toBinary(Object input);
6+
}

0 commit comments

Comments
 (0)