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 classFluidAmount.FluidMergeResultstatic classFluidAmount.FluidMergeRoundingSpecifies how mergedFluidAmount's should handle rounding.static classFluidAmount.SafeAddResult -
Field Summary
Fields Modifier and Type Field Description static FluidAmountA_MILLIONA very large amount of fluid - one million buckets.static FluidAmountABSOLUTE_MAXIMUMThe maximum possible value that a validFluidAmountcan hold.static FluidAmountABSOLUTE_MINIMUMThe minimum possible value that a validFluidAmountcan hold.static FluidAmountBOTTLEOne bottle is equal to a third of a bucket.static FluidAmountBUCKETOne bucket of fluid - which is alwaysONE.longdenominatorAlways greater than 0.static FluidAmountMAX_BUCKETSLong.MAX_VALUEof buckets.static FluidAmountMAX_VALUEDeprecated.AsMAX_BUCKETSshould generally be used instead, however if you really need the absolute value then you can useABSOLUTE_MAXIMUM.static FluidAmountMIN_BUCKETSLong.MIN_VALUEof buckets.static FluidAmountMIN_VALUEDeprecated.AsMIN_BUCKETSshould generally be used instead, however if you really need the absolute minimum value then you can useABSOLUTE_MINIMUM.static FluidAmountNEGATIVE_ONElongnumeratorstatic FluidAmountONElongwholestatic FluidAmountZERO -
Constructor Summary
Constructors Constructor Description FluidAmount(long whole)Deprecated.AsofWhole(long)should be used instead. -
Method Summary
Modifier and Type Method Description FluidAmountadd(long by)Adds the given long value to thisFluidAmount, without performing any checking or saturation.BigFluidAmountadd(BigFluidAmount by)Directly adds the givenBigFluidAmountto this one.FluidAmountadd(FluidAmount other)The recommended method for adding twoFluidAmount's together if you don't want to think about inexact answers.intas1620()intas1620(java.math.RoundingMode rounding)BigFluidAmountasBigInt()doubleasInexactDouble()intasInt(int base)intasInt(int base, java.math.RoundingMode rounding)longasLong(long base)longasLong(long base, java.math.RoundingMode rounding)FluidAmountasLongIntExact()BigFluidAmountbigAdd(FluidAmount by)Directly adds the givenFluidAmountto this one, returning the result as aBigFluidAmount.BigFluidAmountbigDiv(FluidAmount by)BigFluidAmountbigMul(FluidAmount by)BigFluidAmountbigReciprocal()FluidAmountcheckedAdd(FluidAmount by)Directly adds the givenFluidAmountto this one.FluidAmountcheckedDiv(long by)FluidAmountcheckedDiv(FluidAmount by)FluidAmountcheckedMul(long by)FluidAmountcheckedMul(FluidAmount by)FluidAmountcheckedSub(FluidAmount by)intcompareTo(FluidAmount o)Null is treated as zeroFluidAmountdiv(long other)The recommended method for dividing this by aLongif you don't want to think about inexact answers.BigFluidAmountdiv(BigFluidAmount by)FluidAmountdiv(FluidAmount other)The recommended method for dividing this by anotherFluidAmountif you don't want to think about inexact answers.booleanequals(FluidAmount other)booleanequals(java.lang.Object obj)static FluidAmountfromDouble(double value)Deprecated.Useparse(String)instead.static FluidAmountfromMcBuffer(net.minecraft.network.PacketByteBuf buffer)static FluidAmountfromNbt(net.minecraft.nbt.CompoundTag tag)static FluidAmountfromStdBuffer(io.netty.buffer.ByteBuf buffer)BigFluidAmountgcd(BigFluidAmount other)BigFluidAmountgcd(FluidAmount other)longgetCountOf(FluidAmount by)FluidAmountgetDivisor()inthashCode()booleanisGreaterThan(FluidAmount other)Null is treated as zerobooleanisGreaterThanOrEqual(FluidAmount other)Null is treated as zerobooleanisLessThan(FluidAmount other)Null is treated as zerobooleanisLessThanOrEqual(FluidAmount other)Null is treated as zerobooleanisNegative()booleanisOverflow()booleanisPositive()booleanisZero()FluidAmountlcm(FluidAmount other)FluidAmountmax(FluidAmount other)static FluidAmount.FluidMergeResultmerge(FluidAmount target, FluidAmount toAdd)static FluidAmount.FluidMergeResultmerge(FluidAmount target, FluidAmount toAdd, long denominatorTarget, long denominatorAdd)static FluidAmount.FluidMergeResultmerge(FluidAmount target, FluidAmount toAdd, FluidAmount.FluidMergeRounding rounding)FluidAmountmin(FluidAmount other)FluidAmountmul(long by)The recommended method for multiplying this by a long if you don't want to think about inexact answers.BigFluidAmountmul(BigFluidAmount by)FluidAmountmul(FluidAmount by)The recommended method for multiplying twoFluidAmount's together if you don't want to think about inexact answers.FluidAmountnegate()static FluidAmountof(long numerator, long denominator)Creates a newFluidAmountwith the given values.static FluidAmountof(long whole, long numerator, long denominator)Creates a newFluidAmountwith the given values.static FluidAmountof1620(int amount)Legacy conversion method for creating a fraction with the given amount as it's numerator, and 1620 as it's denominatorstatic FluidAmountofWhole(long whole)Creates a newFluidAmountwith the given values.static FluidAmountparse(java.lang.String text)Attempts to parse the given text as aFluidAmount.FluidAmountreciprocal()FluidAmountroundedAdd(FluidAmount other)FluidAmountroundedAdd(FluidAmount other, java.math.RoundingMode rounding)FluidAmountroundedDiv(long by)FluidAmountroundedDiv(long by, java.math.RoundingMode rounding)FluidAmountroundedDiv(FluidAmount by)FluidAmountroundedDiv(FluidAmount by, java.math.RoundingMode rounding)FluidAmountroundedMul(long by)FluidAmountroundedMul(long by, java.math.RoundingMode rounding)FluidAmountroundedMul(FluidAmount by)FluidAmountroundedMul(FluidAmount by, java.math.RoundingMode rounding)FluidAmountroundedSub(FluidAmount by)FluidAmountroundedSub(FluidAmount by, java.math.RoundingMode rounding)FluidAmount.SafeAddResultsafeAdd(FluidAmount other)Safely adds the givenFluidAmountto this one, returning the merged result.FluidAmount.SafeAddResultsafeAdd(FluidAmount other, java.math.RoundingMode rounding)Safely adds the givenFluidAmountto this one, returning the merged result.FluidAmount.SafeAddResultsafeSub(FluidAmount by)FluidAmount.SafeAddResultsafeSub(FluidAmount by, java.math.RoundingMode rounding)FluidAmountsaturatedAdd(FluidAmount by)Similar tocheckedAdd(FluidAmount), but returns eitherMAX_BUCKETSorMIN_BUCKETSinstead of throwing an exception.FluidAmountsaturatedDiv(long by)FluidAmountsaturatedDiv(FluidAmount by)FluidAmountsaturatedMul(long by)FluidAmountsaturatedMul(FluidAmount by)FluidAmountsaturatedSub(FluidAmount by)intsign()FluidAmountsub(long by)BigFluidAmountsub(BigFluidAmount by)FluidAmountsub(FluidAmount other)The recommended method for subtracting anotherFluidAmountfrom this if you don't want to think about inexact answers.java.lang.StringtoDisplayString()voidtoMcBuffer(net.minecraft.network.PacketByteBuf buffer)net.minecraft.nbt.CompoundTagtoNbt()java.lang.StringtoParseableString()voidtoStdBuffer(io.netty.buffer.ByteBuf buffer)java.lang.StringtoString()static java.lang.ObjecttryParse(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_VALUEof buckets. -
MIN_BUCKETS
Long.MIN_VALUEof buckets. -
ABSOLUTE_MAXIMUM
The maximum possible value that a validFluidAmountcan 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 validFluidAmountcan hold. It's not recommended to use this as it can cause headaches when adding or subtracting values from this. -
MAX_VALUE
Deprecated.AsMAX_BUCKETSshould generally be used instead, however if you really need the absolute value then you can useABSOLUTE_MAXIMUM.The maximum possible value that a validFluidAmountcan hold. It's not recommended to use this as it can cause headaches when adding or subtracting values from this. -
MIN_VALUE
Deprecated.AsMIN_BUCKETSshould generally be used instead, however if you really need the absolute minimum value then you can useABSOLUTE_MINIMUM.The minimum possible value that a validFluidAmountcan 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 newFluidAmountwith the given whole value. The numerator is set to 0, and the denominator is set to 1.
-
-
Method Details
-
ofWhole
Creates a newFluidAmountwith the given values. This will reduce the fraction into it's simplest form. -
of
Creates a newFluidAmountwith 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 newFluidAmountwith 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
Longthen 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 parsedFluidAmountif 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
FluidAmounthas potentially overflowed out of a long. This will return true if eitherwholeornumeratorisLong.MIN_VALUEorLong.MAX_VALUE, or ifdenominatorisLong.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
FluidAmountusing a base of 1620.
-
as1620
public int as1620(java.math.RoundingMode rounding)- Returns:
- Rounded-up value of this
FluidAmountusing a base of 1620.
-
asInt
public int asInt(int base)- Returns:
- Rounded-up integer value of this
FluidAmountusing the given base. ReturnsInteger.MAX_VALUEorInteger.MIN_VALUEif 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
FluidAmountusing the given base. ReturnsLong.MAX_VALUEorLong.MIN_VALUEif the true value is out-of-range.
-
asLong
public long asLong(long base, java.math.RoundingMode rounding)- Returns:
- Rounded-up long value of this
FluidAmountusing the given base. ReturnsLong.MAX_VALUEorLong.MIN_VALUEif the true value is out-of-range.
-
equals
public boolean equals(java.lang.Object obj)- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
toDisplayString
public java.lang.String toDisplayString()- Returns:
- A string version of this
FluidAmountthat should be shown to players.
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
toParseableString
public java.lang.String toParseableString()- Returns:
- This
FluidAmountrepresented as a string that could be parsed byparse(String)to return anotherFluidAmountequal to itself.
-
equals
- Returns:
- True if the number that this
FluidAmountrepresents is equal to the number that the givenFluidAmountrepresents.
-
compareTo
Null is treated as zero- Specified by:
compareToin 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 givenFluidAmountto this one, returning the merged result. UnlikecheckedAdd(FluidAmount)this will only throw anArithmeticExceptionif 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 givenFluidAmountto this one, returning the merged result. UnlikecheckedAdd(FluidAmount)this will never throw anArithmeticExceptionif 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 givenFluidAmountto 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_BUCKETSorMIN_BUCKETSinstead of throwing an exception. -
bigAdd
Directly adds the givenFluidAmountto 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 givenBigFluidAmountto 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 anotherFluidAmountfrom 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 benegatedand 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 benegatedand 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 benegatedand then passed tosafeAdd(FluidAmount, RoundingMode).
-
safeSub
- Parameters:
by- Either Null or a value that will benegatedand then passed tosafeAdd(FluidAmount).
-
roundedSub
- Parameters:
by- Either Null or a value that will benegatedand then passed tosafeAdd(FluidAmount, RoundingMode).- Returns:
- The
FluidAmount.SafeAddResult.roundedResult.
-
roundedSub
- Parameters:
by- Either Null or a value that will benegatedand then passed tosafeAdd(FluidAmount).- Returns:
- The
FluidAmount.SafeAddResult.roundedResult.
-
sub
- Parameters:
by- Either Null or a value that will benegatedand then passed toadd(BigFluidAmount).
-
merge
-
merge
public static FluidAmount.FluidMergeResult merge(FluidAmount target, FluidAmount toAdd, FluidAmount.FluidMergeRounding rounding)- Parameters:
target-toAdd-rounding- TheFluidAmount.FluidMergeRoundingto 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
wholevalue fromsaturatedDiv(FluidAmount).
-
div
The recommended method for dividing this by aLongif 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 anotherFluidAmountif 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.
-