Class TemperatureRecord
- All Implemented Interfaces:
Comparable<TemperatureRecord>
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.mojang.serialization.Codec<TemperatureRecord> Codec that allows for the value to be stored as a simple double (in which case the value will be in Celsius) or as an explicit (value, unit) tuple.static final com.mojang.serialization.Codec<TemperatureRecord> Codec for a record that is represented as a named tuple of the value and unit. -
Constructor Summary
ConstructorsConstructorDescriptionTemperatureRecord(double value) Constructs a Celsius record out of a value.TemperatureRecord(double value, TemperatureUnit unit) Constructs a record out of a value and a unit -
Method Summary
Modifier and TypeMethodDescriptionadd(TemperatureRecord other) Adds a temperature change from another record to the temperature in this record.intcompareTo(@NotNull TemperatureRecord other) Compares another record to this one, in the space of this record's unit.convertToUnit(TemperatureUnit unit) Converts this temperature record to another unitbooleanequals(TemperatureRecord other, double tolerance) Checks if this record stores a roughly equivalent temperature value to the one given in the other record.booleanChecks if this record stores an equivalent temperature value to the one given in the other record.inthashCode()Computes the hash value of this record's Celsius valuebooleanstrictEquals(TemperatureRecord other) A stricter equality method that checks both records are equal in both value and unit.booleanstrictEquals(TemperatureRecord other, double tolerance) A stricter equality method that checks both records are roughly equal in both value and unit.sum(TemperatureRecord other) Returns the sum of two temperature records.toString()unit()doublevalue()doublevalueInUnit(TemperatureUnit unit) Converts this record's value into another unit
-
Field Details
-
UNIT_CODEC
Codec for a record that is represented as a named tuple of the value and unit.Usage Example
Room temperature in Celsius:
{ "value": 20.0, "unit": "celsius" }Room temperature in Fahrenheit:
{ "value": 68.0, "unit": "fahrenheit" } -
CODEC
Codec that allows for the value to be stored as a simple double (in which case the value will be in Celsius) or as an explicit (value, unit) tuple.Usage Example
Storing a Celsius value:
20.0Is equivalent to:
{ "value": 20.0, "unit": "celsius" }Using
Codec.fieldOf(String)to add a key, for example:"temperature": 0.0Is equivalent to
"temperature": { "value": 0.0, "unit": "celsius" }
-
-
Constructor Details
-
TemperatureRecord
Constructs a record out of a value and a unit- Parameters:
value- The value of the recordunit- The unit of the record
-
TemperatureRecord
public TemperatureRecord(double value) Constructs a Celsius record out of a value.- Parameters:
value- The Celsius value of the record.
-
-
Method Details
-
value
public double value()- Returns:
- The value of the record
-
unit
- Returns:
- The unit of the record
-
sum
Returns the sum of two temperature records.This is different from add in that the
otheris treated as an actual temperature value, not a temperature difference. This operation is generally useful for data analysis, e.g., finding the mean of N temperature records.For example
20C + 10K = -263.15C.- Parameters:
other- the other record to sum- Returns:
- Returns a new record that is the sum of this record and the other
- See Also:
-
add
Adds a temperature change from another record to the temperature in this record.This is different from the sum in that the
othertemperature is a temperature difference, not a temperature value. If you want to be 10 degrees warmer, this is what you want to use.For example
20C + 10K = 30C- Parameters:
other- the other record to add- Returns:
- Returns a new record that is this record shifted by the temperature in the other record
- See Also:
-
valueInUnit
Converts this record's value into another unit- Parameters:
unit- The unit to convert to- Returns:
- This records value in the given unit
-
convertToUnit
Converts this temperature record to another unit- Parameters:
unit- The unit to convert to- Returns:
- Returns a new temperature record if the unit is different from this record's unit, returns this record if the unit is the same as this record's unit
-
equals
Checks if this record stores a roughly equivalent temperature value to the one given in the other record.The comparison is performed in this record's unit.
- Parameters:
other- The other record to compare totolerance- A positive fuzz factor for how much the units are allowed to be. It must be a temperature value in this record's unit.- Returns:
- Returns true if the value of this record is roughly equivalent to the value of the other record
-
equals
Checks if this record stores an equivalent temperature value to the one given in the other record.The comparison is performed in Celsius
-
hashCode
public int hashCode()Computes the hash value of this record's Celsius value -
toString
-
strictEquals
A stricter equality method that checks both records are roughly equal in both value and unit.- Parameters:
other- the other record to compare totolerance- A positive fuzz factor for how much the units are allowed to be. It must be a temperature value in this record's unit.- Returns:
- Returns true if both records have the same unit, and roughly the same value
-
strictEquals
A stricter equality method that checks both records are equal in both value and unit.- Parameters:
other- the other record to compare to- Returns:
- Returns true if both records have the same unit, and the same value
-
compareTo
Compares another record to this one, in the space of this record's unit. The comparison is based on equivalence, for example 20.0°C is equivalent to 68°F.- Specified by:
compareToin interfaceComparable<TemperatureRecord>- Parameters:
other- the record to be compared.- Returns:
- The value
0if this record represents and equivalent temperature to the other record; a negative value if this record represents a temperature less than the other record; and a positive value if this record represents a temperature greater than the other record.
-