From 713cf2209b1c92aeca37d2f7796fbc08bcf5ea3e Mon Sep 17 00:00:00 2001 From: jpe7s Date: Thu, 15 Aug 2024 21:38:15 -0500 Subject: [PATCH] consistent enum construction. --- build.gradle | 2 +- .../anchor/types/MarginCalculationMode.java | 7 +++++-- .../drift/anchor/types/ModifyOrderId.java | 4 ++-- .../glam/anchor/types/EngineFieldValue.java | 21 +++++++++++-------- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index b7303fb..5089d50 100644 --- a/build.gradle +++ b/build.gradle @@ -41,7 +41,7 @@ ext { sava = '1.2.3' sava_solana_programs = '1.1.0' - sava_anchor_src_gen = '1.1.4' + sava_anchor_src_gen = '1.1.7' } subprojects { diff --git a/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/MarginCalculationMode.java b/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/MarginCalculationMode.java index 9ca80b8..67ab455 100644 --- a/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/MarginCalculationMode.java +++ b/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/MarginCalculationMode.java @@ -10,7 +10,7 @@ static MarginCalculationMode read(final byte[] _data, final int offset) { final int ordinal = _data[offset] & 0xFF; final int i = offset + 1; return switch (ordinal) { - case 0 -> new Standard(_data[i] == 1); + case 0 -> Standard.read(_data, i); case 1 -> Liquidation.read(_data, i); default -> throw new IllegalStateException(java.lang.String.format( "Unexpected ordinal [%d] for enum [MarginCalculationMode]", ordinal @@ -20,8 +20,11 @@ static MarginCalculationMode read(final byte[] _data, final int offset) { record Standard(boolean val) implements EnumBool, MarginCalculationMode { + public static final Standard TRUE = new Standard(true); + public static final Standard FALSE = new Standard(false); + public static Standard read(final byte[] _data, int i) { - return new Standard(_data[i] == 1); + return _data[i] == 1 ? Standard.TRUE : Standard.FALSE; } @Override diff --git a/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/ModifyOrderId.java b/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/ModifyOrderId.java index c40bef9..4de595f 100644 --- a/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/ModifyOrderId.java +++ b/programs/src/main/java/software/sava/anchor/programs/drift/anchor/types/ModifyOrderId.java @@ -12,8 +12,8 @@ static ModifyOrderId read(final byte[] _data, final int offset) { final int ordinal = _data[offset] & 0xFF; final int i = offset + 1; return switch (ordinal) { - case 0 -> new UserOrderId(_data[i] & 0xFF); - case 1 -> new OrderId(getInt32LE(_data, i)); + case 0 -> UserOrderId.read(_data, i); + case 1 -> OrderId.read(_data, i); default -> throw new IllegalStateException(java.lang.String.format( "Unexpected ordinal [%d] for enum [ModifyOrderId]", ordinal )); diff --git a/programs/src/main/java/software/sava/anchor/programs/glam/anchor/types/EngineFieldValue.java b/programs/src/main/java/software/sava/anchor/programs/glam/anchor/types/EngineFieldValue.java index e25ebe3..5ce30e1 100644 --- a/programs/src/main/java/software/sava/anchor/programs/glam/anchor/types/EngineFieldValue.java +++ b/programs/src/main/java/software/sava/anchor/programs/glam/anchor/types/EngineFieldValue.java @@ -31,18 +31,18 @@ static EngineFieldValue read(final byte[] _data, final int offset) { final int ordinal = _data[offset] & 0xFF; final int i = offset + 1; return switch (ordinal) { - case 0 -> new Boolean(_data[i] == 1); + case 0 -> Boolean.read(_data, i); case 1 -> Date.read(_data, i); - case 2 -> new Double(getInt64LE(_data, i)); - case 3 -> new Integer(getInt32LE(_data, i)); + case 2 -> Double.read(_data, i); + case 3 -> Integer.read(_data, i); case 4 -> String.read(_data, i); case 5 -> Time.read(_data, i); - case 6 -> new U8(_data[i] & 0xFF); - case 7 -> new U64(getInt64LE(_data, i)); - case 8 -> new Pubkey(readPubKey(_data, i)); - case 9 -> new Percentage(getInt32LE(_data, i)); + case 6 -> U8.read(_data, i); + case 7 -> U64.read(_data, i); + case 8 -> Pubkey.read(_data, i); + case 9 -> Percentage.read(_data, i); case 10 -> URI.read(_data, i); - case 11 -> new Timestamp(getInt64LE(_data, i)); + case 11 -> Timestamp.read(_data, i); case 12 -> VecPubkey.read(_data, i); case 13 -> VecU32.read(_data, i); case 14 -> VecAcl.read(_data, i); @@ -54,8 +54,11 @@ static EngineFieldValue read(final byte[] _data, final int offset) { record Boolean(boolean val) implements EnumBool, EngineFieldValue { + public static final Boolean TRUE = new Boolean(true); + public static final Boolean FALSE = new Boolean(false); + public static Boolean read(final byte[] _data, int i) { - return new Boolean(_data[i] == 1); + return _data[i] == 1 ? Boolean.TRUE : Boolean.FALSE; } @Override