Class FluidAmount
- All Implemented Interfaces:
java.lang.Comparable<FluidAmount>
public final class FluidAmount
extends java.lang.Object
whole
+
(numerator
/ denominator
)". Negative values are indicated with both whole
and
numerator
being negative - it is never permissible for only one of them to be less than 0 and the other to
be greater than 0.
Note: This class is intended to become a value-based class, so it will (eventually) obey the rules set out in https://openjdk.java.net/jeps/390.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FluidAmount.FluidMergeResult
static class
FluidAmount.FluidMergeRounding
Specifies how mergedFluidAmount
's should handle rounding.static class
FluidAmount.SafeAddResult
-
Field Summary
Fields Modifier and Type Field Description static FluidAmount
A_MILLION
A very large amount of fluid - one million buckets.static FluidAmount
ABSOLUTE_MAXIMUM
The maximum possible value that a validFluidAmount
can hold.static FluidAmount
ABSOLUTE_MINIMUM
The minimum possible value that a validFluidAmount
can hold.static FluidAmount
BOTTLE
One bottle is equal to a third of a bucket.static FluidAmount
BUCKET
One bucket of fluid - which is alwaysONE
.long
denominator
Always greater than 0.static FluidAmount
MAX_BUCKETS
Long.MAX_VALUE
of buckets.static FluidAmount
MAX_VALUE
Deprecated.AsMAX_BUCKETS
should generally be used instead, however if you really need the absolute value then you can useABSOLUTE_MAXIMUM
.static FluidAmount
MIN_BUCKETS
Long.MIN_VALUE
of buckets.static FluidAmount
MIN_VALUE
Deprecated.AsMIN_BUCKETS
should generally be used instead, however if you really need the absolute minimum value then you can useABSOLUTE_MINIMUM
.static FluidAmount
NEGATIVE_ONE
long
numerator
static FluidAmount
ONE
long
whole
static FluidAmount
ZERO
-
Constructor Summary
Constructors Constructor Description FluidAmount(long whole)
Deprecated.AsofWhole(long)
should be used instead. -
Method Summary
Modifier and Type Method Description FluidAmount
add(long by)
Adds the given long value to thisFluidAmount
, without performing any checking or saturation.BigFluidAmount
add(BigFluidAmount by)
Directly adds the givenBigFluidAmount
to this one.FluidAmount
add(FluidAmount other)
The recommended method for adding twoFluidAmount
's together if you don't want to think about inexact answers.int
as1620()
int
as1620(java.math.RoundingMode rounding)
BigFluidAmount
asBigInt()
double
asInexactDouble()
int
asInt(int base)
int
asInt(int base, java.math.RoundingMode rounding)
long
asLong(long base)
long
asLong(long base, java.math.RoundingMode rounding)
FluidAmount
asLongIntExact()
BigFluidAmount
bigAdd(FluidAmount by)
Directly adds the givenFluidAmount
to this one, returning the result as aBigFluidAmount
.BigFluidAmount
bigDiv(FluidAmount by)
BigFluidAmount
bigMul(FluidAmount by)
BigFluidAmount
bigReciprocal()
FluidAmount
checkedAdd(FluidAmount by)
Directly adds the givenFluidAmount
to this one.FluidAmount
checkedDiv(long by)
FluidAmount
checkedDiv(FluidAmount by)
FluidAmount
checkedMul(long by)
FluidAmount
checkedMul(FluidAmount by)
FluidAmount
checkedSub(FluidAmount by)
int
compareTo(FluidAmount o)
Null is treated as zeroFluidAmount
div(long other)
The recommended method for dividing this by aLong
if you don't want to think about inexact answers.BigFluidAmount
div(BigFluidAmount by)
FluidAmount
div(FluidAmount other)
The recommended method for dividing this by anotherFluidAmount
if you don't want to think about inexact answers.boolean
equals(FluidAmount other)
boolean
equals(java.lang.Object obj)
static FluidAmount
fromDouble(double value)
Deprecated.Useparse(String)
instead.static FluidAmount
fromMcBuffer(net.minecraft.network.PacketByteBuf buffer)
static FluidAmount
fromNbt(net.minecraft.nbt.CompoundTag tag)
static FluidAmount
fromStdBuffer(io.netty.buffer.ByteBuf buffer)
BigFluidAmount
gcd(BigFluidAmount other)
BigFluidAmount
gcd(FluidAmount other)
long
getCountOf(FluidAmount by)
FluidAmount
getDivisor()
int
hashCode()
boolean
isGreaterThan(FluidAmount other)
Null is treated as zeroboolean
isGreaterThanOrEqual(FluidAmount other)
Null is treated as zeroboolean
isLessThan(FluidAmount other)
Null is treated as zeroboolean
isLessThanOrEqual(FluidAmount other)
Null is treated as zeroboolean
isNegative()
boolean
isOverflow()
boolean
isPositive()
boolean
isZero()
FluidAmount
lcm(FluidAmount other)
FluidAmount
max(FluidAmount other)
static FluidAmount.FluidMergeResult
merge(FluidAmount target, FluidAmount toAdd)
static FluidAmount.FluidMergeResult
merge(FluidAmount target, FluidAmount toAdd, long denominatorTarget, long denominatorAdd)
static FluidAmount.FluidMergeResult
merge(FluidAmount target, FluidAmount toAdd, FluidAmount.FluidMergeRounding rounding)
FluidAmount
min(FluidAmount other)
FluidAmount
mul(long by)
The recommended method for multiplying this by a long if you don't want to think about inexact answers.BigFluidAmount
mul(BigFluidAmount by)
FluidAmount
mul(FluidAmount by)
The recommended method for multiplying twoFluidAmount
's together if you don't want to think about inexact answers.FluidAmount
negate()
static FluidAmount
of(long numerator, long denominator)
Creates a newFluidAmount
with the given values.static FluidAmount
of(long whole, long numerator, long denominator)
Creates a newFluidAmount
with the given values.static FluidAmount
of1620(int amount)
Legacy conversion method for creating a fraction with the given amount as it's numerator, and 1620 as it's denominatorstatic FluidAmount
ofWhole(long whole)
Creates a newFluidAmount
with the given values.static FluidAmount
parse(java.lang.String text)
Attempts to parse the given text as aFluidAmount
.FluidAmount
reciprocal()
FluidAmount
roundedAdd(FluidAmount other)
FluidAmount
roundedAdd(FluidAmount other, java.math.RoundingMode rounding)
FluidAmount
roundedDiv(long by)
FluidAmount
roundedDiv(long by, java.math.RoundingMode rounding)
FluidAmount
roundedDiv(FluidAmount by)
FluidAmount
roundedDiv(FluidAmount by, java.math.RoundingMode rounding)
FluidAmount
roundedMul(long by)
FluidAmount
roundedMul(long by, java.math.RoundingMode rounding)
FluidAmount
roundedMul(FluidAmount by)
FluidAmount
roundedMul(FluidAmount by, java.math.RoundingMode rounding)
FluidAmount
roundedSub(FluidAmount by)
FluidAmount
roundedSub(FluidAmount by, java.math.RoundingMode rounding)
FluidAmount.SafeAddResult
safeAdd(FluidAmount other)
Safely adds the givenFluidAmount
to this one, returning the merged result.FluidAmount.SafeAddResult
safeAdd(FluidAmount other, java.math.RoundingMode rounding)
Safely adds the givenFluidAmount
to this one, returning the merged result.FluidAmount.SafeAddResult
safeSub(FluidAmount by)
FluidAmount.SafeAddResult
safeSub(FluidAmount by, java.math.RoundingMode rounding)
FluidAmount
saturatedAdd(FluidAmount by)
Similar tocheckedAdd(FluidAmount)
, but returns eitherMAX_BUCKETS
orMIN_BUCKETS
instead of throwing an exception.FluidAmount
saturatedDiv(long by)
FluidAmount
saturatedDiv(FluidAmount by)
FluidAmount
saturatedMul(long by)
FluidAmount
saturatedMul(FluidAmount by)
FluidAmount
saturatedSub(FluidAmount by)
int
sign()
FluidAmount
sub(long by)
BigFluidAmount
sub(BigFluidAmount by)
FluidAmount
sub(FluidAmount other)
The recommended method for subtracting anotherFluidAmount
from this if you don't want to think about inexact answers.java.lang.String
toDisplayString()
void
toMcBuffer(net.minecraft.network.PacketByteBuf buffer)
net.minecraft.nbt.CompoundTag
toNbt()
java.lang.String
toParseableString()
void
toStdBuffer(io.netty.buffer.ByteBuf buffer)
java.lang.String
toString()
static java.lang.Object
tryParse(java.lang.String text)
Attempts to parse the given text as aFluidAmount
.
-
Field Details
-
ZERO
-
ONE
-
NEGATIVE_ONE
-
A_MILLION
A very large amount of fluid - one million buckets. Used primarily in cases where we need to test if any fluid is insertable, so we go above normal values (without going so far out of range to make common calculations overflow intoBigFluidAmount
). -
BUCKET
One bucket of fluid - which is alwaysONE
. -
BOTTLE
One bottle is equal to a third of a bucket. -
MAX_BUCKETS
Long.MAX_VALUE
of buckets. -
MIN_BUCKETS
Long.MIN_VALUE
of buckets. -
ABSOLUTE_MAXIMUM
The maximum possible value that a validFluidAmount
can hold. It's not recommended to use this as it can cause headaches when adding or subtracting values from this. -
ABSOLUTE_MINIMUM
The minimum possible value that a validFluidAmount
can hold. It's not recommended to use this as it can cause headaches when adding or subtracting values from this. -
MAX_VALUE
Deprecated.AsMAX_BUCKETS
should generally be used instead, however if you really need the absolute value then you can useABSOLUTE_MAXIMUM
.The maximum possible value that a validFluidAmount
can hold. It's not recommended to use this as it can cause headaches when adding or subtracting values from this. -
MIN_VALUE
Deprecated.AsMIN_BUCKETS
should generally be used instead, however if you really need the absolute minimum value then you can useABSOLUTE_MINIMUM
.The minimum possible value that a validFluidAmount
can hold. It's not recommended to use this as it can cause headaches when adding or subtracting values from this. -
whole
public final long whole -
numerator
public final long numerator -
denominator
public final long denominatorAlways greater than 0.
-
-
Constructor Details
-
FluidAmount
@Deprecated public FluidAmount(long whole)Deprecated.AsofWhole(long)
should be used instead.Constructs a newFluidAmount
with the given whole value. The numerator is set to 0, and the denominator is set to 1.
-
-
Method Details
-
ofWhole
Creates a newFluidAmount
with the given values. This will reduce the fraction into it's simplest form. -
of
Creates a newFluidAmount
with the given values. This will reduce the fraction into it's simplest form. -
of1620
Legacy conversion method for creating a fraction with the given amount as it's numerator, and 1620 as it's denominator -
of
Creates a newFluidAmount
with the given values. This will reduce the fraction into it's simplest form.- Throws:
java.lang.IllegalArgumentException
- if either whole or numerator are negative, or if denominator is less than or equal to 0.
-
fromDouble
Deprecated.Useparse(String)
instead. -
parse
Attempts to parse the given text as aFluidAmount
.The text is parsed according to the following rules:
- If the text is a valid
Long
then that is parsed and returned as if fromofWhole(long)
.
- (Optional) "-": The minus sign, which indicates that the whole part is negative.
- (Optional) "(": An opening bracket.
- A valid
Long
, that only contains [0-9] (FromLong.parseLong(String)
). This is taken as thewhole
- Either a single "+" or "-", which indicates that the
- (Optional) "(": An opening bracket.
- A valid
Long
, that only contains [0-9] (FromLong.parseLong(String)
). This is taken as thenumerator
. This may be greater than thedenominator
. - A "/" symbol.
- A valid
Long
, that only contains [0-9], and doesn't equal 0. (FromLong.parseLong(String)
). This is taken as thedenominator
- Either one or two ")" symbols, matching the two optional opening "(" symbols.
Alternatively the first 4 items can be left off.
For example these strings are all valid:
- "1"
- "-1"
- "-1 - 3 / 4"
- "-(1 + 3 / 4)"
- "1 + 3 / 4"
- "12 / 4"
- "6 + 12 / 4"
- "6+12/4"
- "-(6+12/4)"
- "-(6+(12/4))"
- "-6-(12/4)"
- Throws:
java.lang.NumberFormatException
- if the input text was not a valid fraction.
- If the text is a valid
-
tryParse
public static java.lang.Object tryParse(java.lang.String text)Attempts to parse the given text as aFluidAmount
. This uses the same parsing rules asparse(String)
, except that this returns either aFluidAmount
(if it parsed correctly) or aString
(if it didn't parse correctly). This will never throw an exception.- Returns:
String
(if there's an error and _throw is false) or the parsedFluidAmount
if the text could be parsed.
-
fromNbt
-
toNbt
public net.minecraft.nbt.CompoundTag toNbt() -
fromStdBuffer
-
toStdBuffer
public void toStdBuffer(io.netty.buffer.ByteBuf buffer) -
fromMcBuffer
-
toMcBuffer
public void toMcBuffer(net.minecraft.network.PacketByteBuf buffer) -
isZero
public boolean isZero() -
isNegative
public boolean isNegative() -
isPositive
public boolean isPositive() -
sign
public int sign()- Returns:
- The sign: Either -1 if this is negative, +1 if this is positive, or 0 if this is zero.
-
isOverflow
public boolean isOverflow()- Returns:
- True if this
FluidAmount
has potentially overflowed out of a long. This will return true if eitherwhole
ornumerator
isLong.MIN_VALUE
orLong.MAX_VALUE
, or ifdenominator
isLong.MAX_VALUE
.
-
getDivisor
- Returns:
- The denominator represented in a new fraction, as 1/this.denominator.
-
as1620
public int as1620()- Returns:
- Rounded-up value of this
FluidAmount
using a base of 1620.
-
as1620
public int as1620(java.math.RoundingMode rounding)- Returns:
- Rounded-up value of this
FluidAmount
using a base of 1620.
-
asInt
public int asInt(int base)- Returns:
- Rounded-up integer value of this
FluidAmount
using the given base. ReturnsInteger.MAX_VALUE
orInteger.MIN_VALUE
if the true value is out-of-range.
-
asInt
public int asInt(int base, java.math.RoundingMode rounding) -
asLong
public long asLong(long base)- Returns:
- Rounded-up long value of this
FluidAmount
using the given base. ReturnsLong.MAX_VALUE
orLong.MIN_VALUE
if the true value is out-of-range.
-
asLong
public long asLong(long base, java.math.RoundingMode rounding)- Returns:
- Rounded-up long value of this
FluidAmount
using the given base. ReturnsLong.MAX_VALUE
orLong.MIN_VALUE
if the true value is out-of-range.
-
equals
public boolean equals(java.lang.Object obj)- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCode
in classjava.lang.Object
-
toDisplayString
public java.lang.String toDisplayString()- Returns:
- A string version of this
FluidAmount
that should be shown to players.
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-
toParseableString
public java.lang.String toParseableString()- Returns:
- This
FluidAmount
represented as a string that could be parsed byparse(String)
to return anotherFluidAmount
equal to itself.
-
equals
- Returns:
- True if the number that this
FluidAmount
represents is equal to the number that the givenFluidAmount
represents.
-
compareTo
Null is treated as zero- Specified by:
compareTo
in interfacejava.lang.Comparable<FluidAmount>
-
asInexactDouble
public double asInexactDouble() -
negate
-
lcm
-
add
The recommended method for adding twoFluidAmount
's together if you don't want to think about inexact answers.(Internally this calls
safeAdd(FluidAmount)
if you want to know the details).- Parameters:
other
- The otherFluidAmount
. Null values will return "this".
-
safeAdd
public FluidAmount.SafeAddResult safeAdd(@Nullable FluidAmount other, java.math.RoundingMode rounding)Safely adds the givenFluidAmount
to this one, returning the merged result. UnlikecheckedAdd(FluidAmount)
this will only throw anArithmeticException
if the result is out-of-range and the rounding mode isRoundingMode.UNNECESSARY
, instead it will round the real answer to the nearest validFluidAmount
(usingBigFluidAmount.asLongIntRounded(RoundingMode)
) -
safeAdd
Safely adds the givenFluidAmount
to this one, returning the merged result. UnlikecheckedAdd(FluidAmount)
this will never throw anArithmeticException
if the result is out-of-range, instead it will round the real answer to the nearest validFluidAmount
(usingBigFluidAmount.asLongIntRounded(RoundingMode)
with a rounding mode ofHALF_EVEN
). -
roundedAdd
- Returns:
- the result of
safeAdd(FluidAmount, RoundingMode)
.roundedResult
.
-
roundedAdd
- Returns:
- the result of
safeAdd(FluidAmount)
.roundedResult
.
-
add
Adds the given long value to thisFluidAmount
, without performing any checking or saturation. -
checkedAdd
Directly adds the givenFluidAmount
to this one.- Parameters:
by
- The amount to add. If it's null or zero then "this" will be returned.- Throws:
java.lang.ArithmeticException
- if the result doesn't fit into aFluidAmount
.
-
saturatedAdd
Similar tocheckedAdd(FluidAmount)
, but returns eitherMAX_BUCKETS
orMIN_BUCKETS
instead of throwing an exception. -
bigAdd
Directly adds the givenFluidAmount
to this one, returning the result as aBigFluidAmount
.- Parameters:
by
- The amount to add. If it's null or zero then this will be returned, converted to aBigFluidAmount
.
-
add
Directly adds the givenBigFluidAmount
to this one.- Parameters:
by
- The amount to add. If it's null or zero then this will be returned, converted into aBigFluidAmount
.
-
sub
-
sub
The recommended method for subtracting anotherFluidAmount
from this if you don't want to think about inexact answers.(Internally this calls
roundedSub(FluidAmount)
if you want to know the details).- Parameters:
other
- The otherFluidAmount
. Null values will return "this".
-
checkedSub
- Parameters:
by
- Either Null or a value that will benegated
and then passed tocheckedAdd(FluidAmount)
.- Throws:
java.lang.ArithmeticException
- if the result doesn't fit into aFluidAmount
.
-
saturatedSub
- Parameters:
by
- Either Null or a value that will benegated
and then passed tosaturatedAdd(FluidAmount)
.
-
safeSub
public FluidAmount.SafeAddResult safeSub(@Nullable FluidAmount by, java.math.RoundingMode rounding)- Parameters:
by
- Either Null or a value that will benegated
and then passed tosafeAdd(FluidAmount, RoundingMode)
.
-
safeSub
- Parameters:
by
- Either Null or a value that will benegated
and then passed tosafeAdd(FluidAmount)
.
-
roundedSub
- Parameters:
by
- Either Null or a value that will benegated
and then passed tosafeAdd(FluidAmount, RoundingMode)
.- Returns:
- The
FluidAmount.SafeAddResult.roundedResult
.
-
roundedSub
- Parameters:
by
- Either Null or a value that will benegated
and then passed tosafeAdd(FluidAmount)
.- Returns:
- The
FluidAmount.SafeAddResult.roundedResult
.
-
sub
- Parameters:
by
- Either Null or a value that will benegated
and then passed toadd(BigFluidAmount)
.
-
merge
-
merge
public static FluidAmount.FluidMergeResult merge(FluidAmount target, FluidAmount toAdd, FluidAmount.FluidMergeRounding rounding)- Parameters:
target
-toAdd
-rounding
- TheFluidAmount.FluidMergeRounding
to use if the addition doesn't result in an exactFluidAmount
.- Returns:
-
merge
public static FluidAmount.FluidMergeResult merge(FluidAmount target, FluidAmount toAdd, long denominatorTarget, long denominatorAdd)- Parameters:
target
-toAdd
-denominatorTarget
- The denominator that the target must be under (a multiple of it's reciprocal), or 0 if the target doesn't need to stay under a particular denominator.denominatorAdd
- The denominator that the excess must be under (a multiple of it's reciprocal), or 0 if the excess doesn't need to stay under a particular denominator.
-
mul
The recommended method for multiplying twoFluidAmount
's together if you don't want to think about inexact answers.(Internally this calls
roundedMul(FluidAmount)
if you want to know the details). -
mul
The recommended method for multiplying this by a long if you don't want to think about inexact answers.(Internally this calls
roundedMul(long)
if you want to know the details). -
checkedMul
-
saturatedMul
-
roundedMul
-
roundedMul
-
checkedMul
-
saturatedMul
-
roundedMul
-
roundedMul
-
mul
-
bigMul
-
reciprocal
- Returns:
- This fraction, but inverted. (With the numerator and denominator swapped).
-
bigReciprocal
-
getCountOf
- Returns:
- The
whole
value fromsaturatedDiv(FluidAmount)
.
-
div
The recommended method for dividing this by aLong
if you don't want to think about inexact answers.(Internally this calls
roundedDiv(long)
if you want to know the details). -
div
The recommended method for dividing this by anotherFluidAmount
if you don't want to think about inexact answers.(Internally this calls
roundedDiv(FluidAmount)
if you want to know the details). -
checkedDiv
-
saturatedDiv
-
roundedDiv
-
roundedDiv
-
checkedDiv
-
saturatedDiv
-
roundedDiv
-
roundedDiv
-
div
-
bigDiv
-
asLongIntExact
-
asBigInt
-
isGreaterThan
Null is treated as zero -
isGreaterThanOrEqual
Null is treated as zero -
isLessThan
Null is treated as zero -
isLessThanOrEqual
Null is treated as zero -
gcd
-
gcd
-
min
- Returns:
- The smaller of this value and then given value.
-
max
- Returns:
- The greater of this value and then given value.
-