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) Deprecated.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 unit.booleanequals(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 valueshift(TemperatureRecord other) Adds a temperature change from another record to the temperature in this record.booleanstrictEquals(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.0...is equivalent to:
{ "value": 20.0, "unit": "celsius" }
Using
Codec.fieldOf(java.lang.String)to add a key, for example:{ "temperature": 20.0 }...is equivalent to:
{ "temperature": { "value": 20.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(TemperatureRecord)in that theotheris treated as an actual temperature value, and not a temperature difference. This operation is generally useful for data analysis, e.g., finding the mean of N temperature records.This operation is commutative IF AND ONLY IF the units are the same.
For example:
70F + 10F = 80F,20C + 10K => -243.15C,10K + 20C => 303.15K.- Parameters:
other- the other record to sum- Returns:
- Returns a new record that is the sum of this record and the other, in the unit of this record.
- See Also:
-
shift
Adds a temperature change from another record to the temperature in this record.
This is different from the
sum(TemperatureRecord)in that theothertemperature is a temperature difference, not a temperature value. If you want to be 10 degrees warmer, this is what you want to use.This operation is commutative IF AND ONLY IF the units are the same.
For example:
70F + 10F = 80F,20C += 10K => 30C,10K += 20C => 30K.- Parameters:
other- the other record to add- See Also:
-
add
Deprecated.Renamed toshift(TemperatureRecord)for a clearer distinction in name. -
valueInUnit
Converts this record's value into another unit- Parameters:
unit- The unit to convert to- Returns:
- This record's 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
-
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- 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.
-
shift(TemperatureRecord)for a clearer distinction in name.