Skip to content

Commit

Permalink
Rename OmniValue internal amount to willets
Browse files Browse the repository at this point in the history
Other, related code cleanup.
  • Loading branch information
msgilligan committed Nov 9, 2016
1 parent d067b26 commit 03e447e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 69 deletions.
21 changes: 10 additions & 11 deletions omnij-core/src/main/java/foundation/omni/OmniDivisibleValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public final class OmniDivisibleValue extends OmniValue {
public static final BigDecimal MAX_VALUE = new BigDecimal("92233720368.54775807");
public static final MathContext DEFAULT_CONTEXT = new MathContext(0, RoundingMode.UNNECESSARY);
public static final int DEFAULT_SCALE = Coin.SMALLEST_UNIT_EXPONENT;
public static final long willetsPerCoin = Coin.COIN.value; // 10^8 (Omni equivalent of satoshi unit
private static final BigDecimal bdWilletsPerCoin = new BigDecimal(willetsPerCoin);
private static final BigDecimal willetsPerDivisibleBigDecimal = new BigDecimal(willetsPerDivisible);

/**
* Create OmniDivisibleValue of the specified amount
Expand All @@ -35,7 +34,7 @@ public static OmniDivisibleValue of(long amount) {
* @return OmniDivisibleValue representing amount
*/
public static OmniDivisibleValue of(BigDecimal amount) {
return new OmniDivisibleValue(amount.multiply(bdWilletsPerCoin).longValueExact());
return new OmniDivisibleValue(amount.multiply(willetsPerDivisibleBigDecimal).longValueExact());
}

/**
Expand Down Expand Up @@ -65,8 +64,8 @@ public Class<BigDecimal> getNumberType() {

@Override
public BigDecimal numberValue() {
BigDecimal willets = new BigDecimal(value);
return willets.divide(bdWilletsPerCoin, DEFAULT_SCALE, RoundingMode.UNNECESSARY);
BigDecimal willets = new BigDecimal(this.willets);
return willets.divide(willetsPerDivisibleBigDecimal, DEFAULT_SCALE, RoundingMode.UNNECESSARY);
}

@Override
Expand Down Expand Up @@ -145,27 +144,27 @@ public BigDecimal bigDecimalValue() {
}

public OmniDivisibleValue plus(OmniDivisibleValue right) {
return OmniDivisibleValue.ofWillets(this.value + right.value);
return OmniDivisibleValue.ofWillets(this.willets + right.willets);
}

public OmniDivisibleValue minus(OmniDivisibleValue right) {
return OmniDivisibleValue.ofWillets(this.value - right.value);
return OmniDivisibleValue.ofWillets(this.willets - right.willets);
}

OmniDivisibleValue multiply(Integer right) {
return OmniDivisibleValue.ofWillets(this.value * right);
return OmniDivisibleValue.ofWillets(this.willets * right);
}

OmniDivisibleValue multiply(Long right) {
return OmniDivisibleValue.ofWillets(this.value * right);
return OmniDivisibleValue.ofWillets(this.willets * right);
}

OmniDivisibleValue div(Integer right) {
return OmniDivisibleValue.ofWillets(this.value / right);
return OmniDivisibleValue.ofWillets(this.willets / right);
}

OmniDivisibleValue div(Long right) {
return OmniDivisibleValue.ofWillets(this.value / right);
return OmniDivisibleValue.ofWillets(this.willets / right);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package foundation.omni;

import javax.money.NumberValue;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

/**
Expand Down Expand Up @@ -33,8 +31,8 @@ public static OmniIndivisibleValue ofWillets(long willets) {
return OmniIndivisibleValue.of(willets);
}

private OmniIndivisibleValue(long value) {
super(value);
private OmniIndivisibleValue(long willets) {
super(willets);
}

@Override
Expand All @@ -49,7 +47,7 @@ public OmniIndivisibleValue round(MathContext mathContext) {

@Override
public Long numberValue() {
return value;
return willets;
}

@Deprecated
Expand All @@ -58,7 +56,7 @@ public BigDecimal bigDecimalValue() {
}

private BigDecimal asBigDecimal() {
return new BigDecimal(value);
return new BigDecimal(willets);
}

@Override
Expand All @@ -67,11 +65,11 @@ public PropertyType getPropertyType() {
}

public OmniIndivisibleValue plus(OmniIndivisibleValue right) {
return OmniIndivisibleValue.of(this.value + right.value);
return OmniIndivisibleValue.of(this.willets + right.willets);
}

public OmniIndivisibleValue minus(OmniIndivisibleValue right) {
return OmniIndivisibleValue.of(this.value - right.value);
return OmniIndivisibleValue.of(this.willets - right.willets);
}

}
79 changes: 32 additions & 47 deletions omnij-core/src/main/java/foundation/omni/OmniValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import javax.money.NumberValue;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.PropertyPermission;

/**
* <p>Numeric value for a quantity of Omni tokens - base class for OmniDivisible and OmniIndivisible subclasses.
Expand All @@ -30,14 +27,8 @@
* <p>TODO: Should we allow negative values?</p>
*/
public abstract class OmniValue extends NumberValue {
// TODO: Make public and rename to 'willets'?
// (unless, of course, we want to OmniIndivisibleValue to also represent Bitcoins
// in which case we might want to give it a name other than 'willets'. I don't like
// using 'value' because of the tension between the divisible/indivisible distinction
// and the fact that we're extending NumberValue and need to interoperate with standard
// Number types where OmniDivisible needs to behave more like BigDecimal.)
// Is there a word that could mean either willets or satoshi? e.g. 'bits', 'internal', 'nanocoins', etc?
protected final long value; // internal value format, in willets
public static final long willetsPerDivisible = Coin.COIN.value; // 10^8 (Omni equivalent of satoshi unit
protected final long willets; // internal value format, in willets

// Willet max/min values, same as max/min for indivisible, but different than for divisible
public static final long MIN_VALUE = 0; // Minimum value of 1 in transactions?
Expand All @@ -47,17 +38,11 @@ public abstract class OmniValue extends NumberValue {
* Default Constructor
* Used only by subclasses using internal (willets) format
*
* @param value Willets (internal/wire format)
* @param willets Willets (internal/wire format)
*/
protected OmniValue(long value) {
checkValue(value);
this.value = value;
}

@Deprecated
protected OmniValue(BigInteger value) {
checkValue(value);
this.value = value.longValue();
protected OmniValue(long willets) {
checkValue(willets);
this.willets = willets;
}

public static OmniValue of(BigDecimal amount, PropertyType type) {
Expand All @@ -76,22 +61,22 @@ public static OmniValue ofWillets(long amount, PropertyType type) {
}

public long getWillets() {
return value;
return willets;
}

abstract public PropertyType getPropertyType();

/**
* <p>Make sure a BigInteger value is a valid Omni "number of coins" value</p>
* <p>Make sure a BigInteger value is a valid Omni "number of coins" (willets) value</p>
*
* @param value
* @param willets
* @throws ArithmeticException
*/
public static void checkValue(BigInteger value) throws ArithmeticException {
if (value.compareTo(BigInteger.valueOf(MIN_VALUE)) == -1) {
public static void checkValue(BigInteger willets) throws ArithmeticException {
if (willets.compareTo(BigInteger.valueOf(MIN_VALUE)) == -1) {
throw new ArithmeticException();
}
if (value.compareTo(BigInteger.valueOf(MAX_VALUE)) == 1) {
if (willets.compareTo(BigInteger.valueOf(MAX_VALUE)) == 1) {
throw new ArithmeticException();
}
}
Expand All @@ -102,11 +87,11 @@ public static void checkValue(BigInteger value) throws ArithmeticException {
* <p>Note: Since any positive long is valid, we just need to check that
* it's not less than MIN_VALUE</p>
*
* @param value value to check.
* @param willets value to check.
* @throws ArithmeticException
*/
public static void checkValue(long value) throws ArithmeticException {
if (value < MIN_VALUE) {
public static void checkValue(long willets) throws ArithmeticException {
if (willets < MIN_VALUE) {
throw new ArithmeticException();
}
}
Expand All @@ -132,15 +117,15 @@ public int getScale() {

@Override
public int intValueExact() {
if (value > Integer.MAX_VALUE) {
if (willets > Integer.MAX_VALUE) {
throw new ArithmeticException("Value too big to be converted to integer");
}
return (int) value;
return (int) willets;
}

@Override
public long longValueExact() {
return value;
return willets;
}

@Override
Expand Down Expand Up @@ -170,18 +155,18 @@ public long getAmountFractionDenominator() {

@Override
public byte byteValue() {
if (value > Byte.MAX_VALUE) {
if (willets > Byte.MAX_VALUE) {
throw new ArithmeticException("Value too big to be converted to byte");
}
return (byte) value;
return (byte) willets;
}

@Override
public short shortValue() {
if (value > Short.MAX_VALUE) {
if (willets > Short.MAX_VALUE) {
throw new ArithmeticException("Value too big to be converted to short");
}
return (short) value;
return (short) willets;
}

@Override
Expand All @@ -206,7 +191,7 @@ public double doubleValue() {

@Override
public int hashCode() {
return Long.valueOf(value).hashCode();
return Long.valueOf(willets).hashCode();
}

@Override
Expand All @@ -217,15 +202,15 @@ public boolean equals(Object obj) {
if (obj == this) {
return true;
}
return this.value == ((OmniValue)obj).value;
return this.willets == ((OmniValue)obj).willets;
}

@Override
public int compareTo(NumberValue o) {
if (o instanceof OmniValue) {
return Long.compare(this.value, ((OmniValue) o).value);
return Long.compare(this.willets, ((OmniValue) o).willets);
} else {
return Long.compare(this.value, o.longValueExact());
return Long.compare(this.willets, o.longValueExact());
}
}

Expand All @@ -239,29 +224,29 @@ public String toString() {

public OmniValue plus(OmniValue right) {
if (this instanceof OmniDivisibleValue && right instanceof OmniDivisibleValue) {
return OmniDivisibleValue.ofWillets(this.value + right.value);
return OmniDivisibleValue.ofWillets(this.willets + right.willets);
} else if (this instanceof OmniIndivisibleValue && right instanceof OmniIndivisibleValue) {
return OmniIndivisibleValue.ofWillets(this.value + right.value);
return OmniIndivisibleValue.ofWillets(this.willets + right.willets);
} else {
throw new ArithmeticException("Can't use plus with mixed OmniDivisible and OmniIndivisible operands");
}
}

public OmniValue minus(OmniValue right) {
if (this instanceof OmniDivisibleValue && right instanceof OmniDivisibleValue) {
return OmniDivisibleValue.of(this.value - right.value);
return OmniDivisibleValue.of(this.willets - right.willets);
} else if (this instanceof OmniIndivisibleValue && right instanceof OmniIndivisibleValue) {
return OmniIndivisibleValue.of(this.value - right.value);
return OmniIndivisibleValue.of(this.willets - right.willets);
} else {
throw new ArithmeticException("Can't use minus with mixed OmniDivisible and OmniIndivisible operands");
}
}

public OmniValue multiply(long right) {
if (this instanceof OmniDivisibleValue) {
return OmniDivisibleValue.of(this.value * right);
return OmniDivisibleValue.of(this.willets * right);
} else if (this instanceof OmniIndivisibleValue) {
return OmniIndivisibleValue.of(this.value * right);
return OmniIndivisibleValue.of(this.willets * right);
} else {
throw new ArithmeticException("Can't use multiply with class other than OmniDivisible or OmniIndivisible");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import foundation.omni.OmniIndivisibleValue
@CompileStatic
@Category(Number)
class NumberCategory {
private static final BigDecimal willetsPerBTCDecimal = new BigDecimal(OmniDivisibleValue.willetsPerCoin)
private static final BigInteger willetsPerBTCBigInt = BigInteger.valueOf(OmniDivisibleValue.willetsPerCoin)

/**
* Treat number as a decimal, divisible amount of an Omni currency
*
Expand Down

0 comments on commit 03e447e

Please sign in to comment.