From 4e7f0611c4bd15ae983bd06feeb80ad69eedb853 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 9 Jul 2020 10:25:28 +0200 Subject: [PATCH] move bitcoinunit to drongo --- .../com/sparrowwallet/drongo/BitcoinUnit.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java diff --git a/src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java b/src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java new file mode 100644 index 0000000..2e50d9c --- /dev/null +++ b/src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java @@ -0,0 +1,50 @@ +package com.sparrowwallet.drongo; + +import com.sparrowwallet.drongo.protocol.Transaction; + +public enum BitcoinUnit { + BTC("BTC") { + @Override + public long getSatsValue(double unitValue) { + return (long)(unitValue * Transaction.SATOSHIS_PER_BITCOIN); + } + + public double getValue(long satsValue) { + return (double)satsValue / Transaction.SATOSHIS_PER_BITCOIN; + } + }, + SATOSHIS("sats") { + @Override + public long getSatsValue(double unitValue) { + return (long)unitValue; + } + + public double getValue(long satsValue) { + return (double)satsValue; + } + }; + + private final String label; + + BitcoinUnit(String label) { + this.label = label; + } + + public String getLabel() { + return label; + } + + public abstract long getSatsValue(double unitValue); + + public abstract double getValue(long satsValue); + + public double convertFrom(double fromValue, BitcoinUnit fromUnit) { + long satsValue = fromUnit.getSatsValue(fromValue); + return getValue(satsValue); + } + + @Override + public String toString() { + return label; + } +}