Skip to content

Commit

Permalink
Add TransactionInfo.transactionType(), JavaDoc comments and tests
Browse files Browse the repository at this point in the history
TransactionInfo.transactionType() returns Optional<TransactionType>

Optional<TransactionType> is the recommended type for handling a
TransactionType enum. With the forthcoming "Pattern Matching for Switch" it will be possible to use it in switch statements very elegantly.

Tests have been added as well as extensive JavaDoc with code examples
for how to use this variable type in earlier (and admittedly more verbose) versions of Java.
  • Loading branch information
msgilligan committed Sep 12, 2022
1 parent 31f1e22 commit 4d3f432
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
44 changes: 43 additions & 1 deletion omnij-core/src/main/java/foundation/omni/tx/Transactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,49 @@ public interface OmniRefTx extends OmniTx {
* <p>
* Transaction type is an unsigned 16-bit value, and stored as a Java {@code short} that is treated as unsigned.
* The {@link #value()} accessor performs the proper conversion and returns an unsigned {@code int}.
* This is a partial list, see {@code omnicore.h} for the complete list.
* <p>
* With <a href="https://openjdk.org/jeps/427">JEP 427: Pattern Matching for Switch</a> it is possible
* to handle {@code null} as a {@code case}. So if you're using a recent version of Java (with preview enabled) you
* can handle undefined transaction types in a single {@code switch} statement/expression. If you have an integer with a transaction type code named {@code typeInt}, you can do something like:
* <pre> {@code
* Optional<TransactionType> optionalType = TransactionType.find(typeInt);
* boolean isSend = switch(optionalType.orElse(null)) {
* case SIMPLE_SEND, SEND_TO_OWNERS, SEND_ALL -> true;
* default -> false;
* case null -> false;
* }
* }</pre>
* The {@code default} case represents defined enum constants not handled with explicit cases and the {@code null} case
* provides a way to handle numeric codes not (yet) defined in the enum.
* <p>
* For versions of Java with switch expressions but no pattern matching, this above code can be written as:
* <pre> {@code
* Optional<TransactionType> optionalType = TransactionType.find(typeInt);
* boolean isSend = optionalType.map(t -> switch(t) {
* case SIMPLE_SEND, SEND_TO_OWNERS, SEND_ALL -> true;
* default -> false;
* }).orElse(false);
* }</pre>
* <p>
* For even earlier versions of Java (back to Java 9), it can be written as:
* <pre> {@code
* Optional<TransactionType> optionalType = TransactionType.find(typeInt);
* boolean isSend;
* optionalType.ifPresentOrElse(t -> switch(t) {
* case SIMPLE_SEND:
* case SEND_TO_OWNERS:
* case SEND_ALL:
* isSend = true;
* break;
* default:
* isSend = false;
* }, {
* isSend = false;
* }
* }</pre>
* For a Java 8 example see the Java unit test {@code TransactionTypeTest.java}.
* <p>
* This is a partial list of transaction types, see {@code omnicore.h} for the complete list.
* @see <a href="https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/omnicore.h">enum TransactionType in omnicore.h</a>
*/
public enum TransactionType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package foundation.omni.tx

import spock.lang.Specification

import foundation.omni.tx.Transactions.TransactionType

/**
* Tests for {@link TransactionType}
*/
class TransactionTypeSpec extends Specification {

void "switch test"(int code, boolean expectedResult) {
given: "An Optional-wrapped Transaction Type enum"
Optional<TransactionType> optionalType = TransactionType.find(code)

when: "We use a switch expression to calculate a boolean value "
boolean isSend = switch(optionalType.orElse(null)) {
case TransactionType.SIMPLE_SEND,
TransactionType.SEND_TO_OWNERS,
TransactionType.SEND_ALL -> true
case null -> false
default -> false
}

then: "We get the correct result"
isSend == expectedResult

where:
code | expectedResult
// Transaction codes that are "sends"
0 | true
3 | true
4 | true
// Defined Transactions that are not "sends"
20 | false
25 | false
// Undefined transaction codes
300 | false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package foundation.omni.tx;

import org.junit.Test;

import java.util.Optional;

import foundation.omni.tx.Transactions.TransactionType;
import static org.junit.Assert.assertTrue;

/**
* Tests for {@link Transactions.TransactionType}
*/
public class TransactionTypeTest {

// TODO: Upgrade to JUnit 5 and make this a parameterized test, like its Spock equivalent
@Test
public void switchTest() {
Optional<TransactionType> optionalType = TransactionType.find(0);

boolean isSend;
if (optionalType.isPresent()) {
switch (optionalType.get()) {
case SIMPLE_SEND:
case SEND_TO_OWNERS:
case SEND_ALL:
isSend = true;
break;
default:
isSend = false;
break;
}
} else {
isSend = false;
}

assertTrue(isSend);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import foundation.omni.CurrencyID;
import foundation.omni.OmniValue;
import foundation.omni.tx.Transactions;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Sha256Hash;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* (Mostly) Immutable representation of OmniTransaction info JSON
Expand Down Expand Up @@ -147,6 +150,18 @@ public String getType() {
return type;
}

/**
* Get the transaction type as an {@code Optional} {@link Transactions.TransactionType} or
* as an {@link Optional#empty()} if it's a transaction type not (yet) included in
* {@link Transactions.TransactionType}.
*
* @return The type or {@link Optional#empty()} if it's an unknown type
*/
@JsonIgnore
public Optional<Transactions.TransactionType> transactionType() {
return Transactions.TransactionType.find(typeInt);
}

public OmniValue getAmount() {
return amount;
}
Expand Down

0 comments on commit 4d3f432

Please sign in to comment.