Skip to content

Commit

Permalink
lib-common: Improve coding API
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Dec 5, 2023
1 parent db2d6f9 commit 079fd40
Show file tree
Hide file tree
Showing 27 changed files with 140 additions and 121 deletions.
4 changes: 2 additions & 2 deletions jacline-lib-common/src/main/jacline/key-decoder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
goog.module("kohlschutter.coding.KeyDecoder");

const DecodingException = goog.require("com.kohlschutter.jacline.lib.coding.DecodingException");
const CodingException = goog.require("com.kohlschutter.jacline.lib.coding.CodingException");
const JsCloseable = goog.require('com.kohlschutter.jacline.lib.io.JsCloseable');

// FIXME type checking
Expand Down Expand Up @@ -52,7 +52,7 @@ class KeyDecoder {
static load(expectedType, o) {
if (expectedType) {
if (o["javaClass"] != expectedType) {
throw DecodingException.withUnexpectedType(expectedType, o["javaClass"]);
throw CodingException.withUnexpectedType(expectedType, o["javaClass"]);
}
}
return new KeyDecoder(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface ArrayDecoder<T> {
*
* @param serialized The serialized representation.
* @return The array.
* @throws DecodingException on error.
* @throws CodingException on error.
*/
T[] decode(Object serialized) throws DecodingException;
T[] decode(Object serialized) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface ArrayEncoder {
*
* @param array The array to encode.
* @return The encoded object.
* @throws CodingException on error.
*/
Object encode(Object[] array);
Object encode(Object[] array) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ public interface Codable {
*
* @return The encoded representation.
*/
Object encode(KeyEncoderProvider provider);
Object encode(KeyEncoderProvider provider) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@

import jsinterop.annotations.JsMethod;

public class DecodingException extends Exception {
public class CodingException extends Exception {
private static final long serialVersionUID = 1L;

public DecodingException() {
public CodingException() {
super();
}

public DecodingException(String message, Throwable cause) {
public CodingException(String message, Throwable cause) {
super(message, cause);
}

public DecodingException(String message) {
public CodingException(String message) {
super(message);
}

public DecodingException(Throwable cause) {
public CodingException(Throwable cause) {
super(cause);
}

@JsMethod
public static DecodingException withMessage(String message) {
return new DecodingException(message);
public static CodingException withMessage(String message) {
return new CodingException(message);
}

@JsMethod
public static DecodingException withUnexpectedType(String expected, String found) {
return new DecodingException("Unexpected type: " + found + "; expected: " + expected);
public static CodingException withUnexpectedType(String expected, String found) {
return new CodingException("Unexpected type: " + found + "; expected: " + expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ private CodingProviders() {
*
* @param provider The original {@link KeyEncoderProvider}, or {@code null} (for default).
* @return Some {@link KeyEncoderProvider}.
* @throws CodingException on error.
*/
public static KeyEncoderProvider decorateEncoderProvider(KeyEncoderProvider provider) {
public static KeyEncoderProvider decorateEncoderProvider(KeyEncoderProvider provider)
throws CodingException {
if (provider == null) {
return KeyEncoder::begin;
} else {
Expand All @@ -45,17 +47,14 @@ public static KeyEncoderProvider decorateEncoderProvider(KeyEncoderProvider prov
*
* @param provider The original {@link KeyDecoderProvider}, or {@code null} (for default).
* @return Some {@link KeyDecoderProvider}.
* @throws CodingException on error.
*/
public static KeyDecoderProvider decorateDecoderProvider(KeyDecoderProvider provider) {
public static KeyDecoderProvider decorateDecoderProvider(KeyDecoderProvider provider)
throws CodingException {
if (provider == null) {
return KeyDecoder::load;
} else {
return provider;
}
}

public static String getTypeFromEncoded(Object obj) {
// TODO Auto-generated method stub
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ private static CodingServiceProvider findDefault() {
return first.isPresent() ? Objects.requireNonNull(first.get()) : null;
}

KeyDecoder keyDecoder(String expectedCodedType, Object encoded) throws DecodingException;
KeyDecoder keyDecoder(String expectedCodedType, Object encoded) throws CodingException;

KeyEncoder keyEncoder(String type);
KeyEncoder keyEncoder(String type) throws CodingException;

SequenceDecoder sequenceDecoder(Object encoded) throws DecodingException;
SequenceDecoder sequenceDecoder(Object encoded) throws CodingException;

SequenceEncoder sequenceEncoder();
SequenceEncoder sequenceEncoder() throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
@JsFunction
@FunctionalInterface
public interface Decoder<T> {
T decode(KeyDecoderProvider decoderProvider, Object serialized) throws DecodingException;
T decode(KeyDecoderProvider decoderProvider, Object serialized) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@

@JsType(isNative = true, namespace = "kohlschutter.coding", name = "KeyDecoder")
public interface KeyDecoder extends JsCloseable {
String stringForKey(String key);
String stringForKey(String key) throws CodingException;

Boolean booleanForKey(String key);
Boolean booleanForKey(String key) throws CodingException;

Number numberForKey(String key);
Number numberForKey(String key) throws CodingException;

<T> T[] arrayForKey(String key, ArrayDecoder<T> decoder) throws DecodingException;
<T> T[] arrayForKey(String key, ArrayDecoder<T> decoder) throws CodingException;

<T> T objectForKey(String key, ObjectDecoder<T> decoder) throws DecodingException;
<T> T objectForKey(String key, ObjectDecoder<T> decoder) throws CodingException;

boolean hasKey(String key);
boolean hasKey(String key) throws CodingException;

@JsImplementationProvidedSeparately
static KeyDecoder load(String expectedCodedType, Object encoded) throws DecodingException {
static KeyDecoder load(String expectedCodedType, Object encoded) throws CodingException {
return CodingServiceProvider.getDefault().keyDecoder(expectedCodedType, encoded);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
@JsFunction
@FunctionalInterface
public interface KeyDecoderProvider {
KeyDecoder load(String expectedCodedType, Object encoded) throws DecodingException;
KeyDecoder load(String expectedCodedType, Object encoded) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,29 @@ public interface KeyEncoder {
* @param key The key, or {@code null}
* @param value The value.
* @return This encoder.
* @throws CodingException on error.
*/
KeyEncoder encodeString(String key, String value);
KeyEncoder encodeString(String key, String value) throws CodingException;

/**
* Encodes a boolean (or {@code null}) for the given key.
*
* @param key The key, or {@code null}
* @param value The value.
* @return This encoder.
* @throws CodingException on error.
*/
KeyEncoder encodeBoolean(String key, Boolean value);
KeyEncoder encodeBoolean(String key, Boolean value) throws CodingException;

/**
* Encodes a number (or {@code null}) for the given key.
*
* @param key The key, or {@code null}
* @param value The value.
* @return This encoder.
* @throws CodingException on error.
*/
KeyEncoder encodeNumber(String key, Number value);
KeyEncoder encodeNumber(String key, Number value) throws CodingException;

/**
* Encodes an array (or {@code null}) for the given key.
Expand All @@ -58,35 +61,39 @@ public interface KeyEncoder {
* @param encoder The encoder
* @param array The array.
* @return This encoder.
* @throws CodingException on error.
*/
KeyEncoder encodeArray(String key, ArrayEncoder encoder, Object[] array);
KeyEncoder encodeArray(String key, ArrayEncoder encoder, Object[] array) throws CodingException;

/**
* Returns a new encoder that can encode the object stored under the given key.
*
* @param key The key to store the object under.
* @param type The encoded type.
* @return The sub-encoder.
* @throws CodingException on error.
*/
KeyEncoder beginEncodeObject(String key, String type);
KeyEncoder beginEncodeObject(String key, String type) throws CodingException;

/**
* Ends any {@link #beginEncodeObject(String, String)} block, returning the parent encoder, or the
* same encoder if it's the root encoder.
*
* @return The parent or this encoder.
* @throws CodingException on error.
*/
KeyEncoder end();
KeyEncoder end() throws CodingException;

/**
* Returns the encoded representation for the data encoded by this encoder.
*
* @return The object.
* @throws CodingException on error.
*/
Object getEncoded();
Object getEncoded() throws CodingException;

@JsImplementationProvidedSeparately
static KeyEncoder begin(String type) {
static KeyEncoder begin(String type) throws CodingException {
return CodingServiceProvider.getDefault().keyEncoder(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
@JsFunction
@FunctionalInterface
public interface KeyEncoderProvider {
KeyEncoder begin(String type);
KeyEncoder begin(String type) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
@JsFunction
@FunctionalInterface
public interface ObjectDecoder<T> {
T decode(Object serialized) throws DecodingException;
T decode(Object serialized) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
@JsFunction
@FunctionalInterface
public interface ObjectEncoder {
Object encode(Object obj);
Object encode(Object obj) throws CodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ public interface SequenceDecoder extends JsCloseable {
@FunctionalInterface
@JsFunction
interface SequenceConsumer<T> {
void consume(T t) throws DecodingException;
void consume(T t) throws CodingException;
}

int size();

int position();

SequenceDecoder strings(int count, SequenceConsumer<String> forEach) throws DecodingException;
SequenceDecoder strings(int count, SequenceConsumer<String> forEach) throws CodingException;

SequenceDecoder booleans(int count, SequenceConsumer<Boolean> forEach) throws DecodingException;
SequenceDecoder booleans(int count, SequenceConsumer<Boolean> forEach) throws CodingException;

SequenceDecoder numbers(int count, SequenceConsumer<Number> forEach) throws DecodingException;
SequenceDecoder numbers(int count, SequenceConsumer<Number> forEach) throws CodingException;

<T> SequenceDecoder arrays(int count, ArrayDecoder<T> decoder, SequenceConsumer<T[]> forEach)
throws DecodingException;
throws CodingException;

<T> SequenceDecoder objects(int count, ObjectDecoder<T> decoder, SequenceConsumer<T> forEach)
throws DecodingException;
throws CodingException;

@JsImplementationProvidedSeparately
static SequenceDecoder load(Object encoded) throws DecodingException {
static SequenceDecoder load(Object encoded) throws CodingException {
return CodingServiceProvider.getDefault().sequenceDecoder(encoded);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,65 @@ public interface SequenceEncoder {
*
* @param values The values.
* @return This encoder.
* @throws CodingException on error.
*/
SequenceEncoder encodeStrings(String... values);
SequenceEncoder encodeStrings(String... values) throws CodingException;

/**
* Encodes a boolean (or {@code null}).
*
* @param values The values.
* @return This encoder.
* @throws CodingException on error.
*/
SequenceEncoder encodeBooleans(Boolean... values);
SequenceEncoder encodeBooleans(Boolean... values) throws CodingException;

/**
* Encodes a number (or {@code null}).
*
* @param values The values.
* @return This encoder.
* @throws CodingException on error.
*/
SequenceEncoder encodeNumbers(Number... values);
SequenceEncoder encodeNumbers(Number... values) throws CodingException;

/**
* Encodes an array.
*
* @return A new sub-encoder.
* @throws CodingException on error.
*/
SequenceEncoder beginEncodeArray();
SequenceEncoder beginEncodeArray() throws CodingException;

/**
* Returns a new encoder that can encode the object stored under the given key.
*
* @param encoder The object encoder
* @param objects The objects
* @return This encoder.
* @throws CodingException on error.
*/
SequenceEncoder encodeObjects(ObjectEncoder encoder, Object... objects);
SequenceEncoder encodeObjects(ObjectEncoder encoder, Object... objects) throws CodingException;

/**
* Ends any {@link #beginEncodeArray()} block, returning the parent encoder, or the same encoder
* if it's the root encoder.
*
* @return The parent or this encoder.
* @throws CodingException on error.
*/
SequenceEncoder end();
SequenceEncoder end() throws CodingException;

/**
* Returns the encoded representation for the data encoded by this encoder.
*
* @return The object.
* @throws CodingException on error.
*/
Object getEncoded();
Object getEncoded() throws CodingException;

@JsImplementationProvidedSeparately
static SequenceEncoder begin() {
static SequenceEncoder begin() throws CodingException {
return CodingServiceProvider.getDefault().sequenceEncoder();
}
}
Loading

0 comments on commit 079fd40

Please sign in to comment.