forked from splix/polkaj
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Boris Velkovski
authored and
Boris Velkovski
committed
Feb 27, 2023
1 parent
226ae14
commit 30d4e9a
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/Result.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.emeraldpay.polkaj.scaletypes; | ||
|
||
|
||
public class Result<T, E> { | ||
|
||
public enum ResultMode { | ||
OK((byte) 0), ERR((byte) 1); | ||
private byte value; | ||
|
||
ResultMode(byte value) { | ||
this.value = value; | ||
} | ||
|
||
public byte getValue() { | ||
return this.value; | ||
} | ||
} | ||
|
||
T ok; | ||
E error; | ||
ResultMode mode; | ||
|
||
public Result(ResultMode mode, T ok, E error) { | ||
this.mode = mode; | ||
this.ok = ok; | ||
this.error = error; | ||
} | ||
|
||
public boolean isOk() { | ||
return mode == ResultMode.OK; | ||
} | ||
|
||
public boolean isError() { | ||
return mode == ResultMode.ERR; | ||
} | ||
|
||
public T getOkValue() { | ||
return ok; | ||
} | ||
|
||
public E getErrorValue() { | ||
return error; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/ResultReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.emeraldpay.polkaj.scaletypes; | ||
|
||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import io.emeraldpay.polkaj.scale.ScaleReader; | ||
|
||
public class ResultReader<T, E> { | ||
static final String unsupportedValueMessage = "Reading unsupported result mode value"; | ||
|
||
public Result<T, E> readResult(ScaleCodecReader reader, ScaleReader<T> okScaleReader, ScaleReader<E> errorScaleReader) { | ||
byte mode = reader.readByte(); | ||
if (mode == Result.ResultMode.OK.getValue()) { | ||
T ok = reader.read(okScaleReader); | ||
return new Result(Result.ResultMode.OK, ok, null); | ||
} | ||
if (mode == Result.ResultMode.ERR.getValue()) { | ||
E error = reader.read(errorScaleReader); | ||
return new Result(Result.ResultMode.ERR, null, error); | ||
} | ||
throw new IllegalStateException(unsupportedValueMessage); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/ResultWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.emeraldpay.polkaj.scaletypes; | ||
|
||
import io.emeraldpay.polkaj.scale.ScaleCodecWriter; | ||
import io.emeraldpay.polkaj.scale.ScaleWriter; | ||
|
||
import java.io.IOException; | ||
|
||
public class ResultWriter<T, E> { | ||
public void writeResult(ScaleCodecWriter writer, ScaleWriter<T> okScaleWriter, | ||
ScaleWriter<E> errorScaleWriter, Result<T, E> result) throws IOException { | ||
writer.writeByte(result.mode.getValue()); | ||
if (result.mode == Result.ResultMode.OK) { | ||
writer.write(okScaleWriter, result.ok); | ||
return; | ||
} | ||
writer.write(errorScaleWriter, result.error); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
polkaj-scale-types/src/test/java/io/emeraldpay/polkaj/scaletypes/EncodeDecodeResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.emeraldpay.polkaj.scaletypes; | ||
|
||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecWriter; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class EncodeDecodeResult { | ||
|
||
@Test | ||
public void EncodeDecodeResult() { | ||
Result<Integer, Integer> dataToEncode = new Result<>(Result.ResultMode.OK, 10, 5); | ||
ByteArrayOutputStream buf = new ByteArrayOutputStream(); | ||
try (ScaleCodecWriter writer = new ScaleCodecWriter(buf)) { | ||
ResultWriter<Integer, Integer> resultWriter = new ResultWriter(); | ||
resultWriter.writeResult(writer, ScaleCodecWriter::writeCompact, ScaleCodecWriter::writeCompact, dataToEncode); | ||
|
||
byte[] decodeBuf = buf.toByteArray(); | ||
ScaleCodecReader reader = new ScaleCodecReader(decodeBuf); | ||
ResultReader<Integer, Integer> resultReader = new ResultReader<>(); | ||
Result<Integer, Integer> result = resultReader.readResult(reader, ScaleCodecReader::readCompactInt, ScaleCodecReader::readCompactInt); | ||
|
||
assertEquals(dataToEncode.mode, result.mode); | ||
assertEquals(dataToEncode.getOkValue(), result.getOkValue()); | ||
assertEquals(null, result.getErrorValue()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |