Class TemperatureRecord

java.lang.Object
com.github.thedeathlycow.thermoo.api.util.TemperatureRecord
All Implemented Interfaces:
Comparable<TemperatureRecord>

public final class TemperatureRecord extends Object implements Comparable<TemperatureRecord>
A class to record a temperature value in a particular unit.
  • Field Details

    • UNIT_CODEC

      public static final com.mojang.serialization.Codec<TemperatureRecord> 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

      public static final com.mojang.serialization.Codec<TemperatureRecord> 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(String) to add a key, for example:

      
       "temperature": 0.0
       

      Is equivalent to

      
       "temperature": {
           "value": 0.0,
           "unit": "celsius"
       }
       
  • Constructor Details

    • TemperatureRecord

      public TemperatureRecord(double value, TemperatureUnit unit)
      Constructs a record out of a value and a unit
      Parameters:
      value - The value of the record
      unit - 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

      public TemperatureUnit unit()
      Returns:
      The unit of the record
    • sum

      Returns the sum of two temperature records.

      This is different from add in that the other is 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

      @Contract("_->new") public TemperatureRecord add(TemperatureRecord other)
      Adds a temperature change from another record to the temperature in this record.

      This is different from the sum in that the other temperature 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

      public double valueInUnit(TemperatureUnit unit)
      Converts this record's value into another unit
      Parameters:
      unit - The unit to convert to
      Returns:
      This records value in the given unit
    • convertToUnit

      public TemperatureRecord convertToUnit(TemperatureUnit unit)
      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

      public boolean equals(TemperatureRecord other, double tolerance)
      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 to
      tolerance - 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

      public boolean equals(Object o)
      Checks if this record stores an equivalent temperature value to the one given in the other record.

      The comparison is performed in Celsius

      Overrides:
      equals in class Object
      Parameters:
      o - The other record to compare to.
      Returns:
      Returns true if the value of this record is equivalent to the value of the other record, in the unit of this record.
    • hashCode

      public int hashCode()
      Computes the hash value of this record's Celsius value
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • strictEquals

      public boolean strictEquals(TemperatureRecord other, double tolerance)
      A stricter equality method that checks both records are roughly equal in both value and unit.
      Parameters:
      other - the other record to compare to
      tolerance - 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

      public boolean strictEquals(TemperatureRecord other)
      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

      public int compareTo(@NotNull @NotNull TemperatureRecord other)
      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:
      compareTo in interface Comparable<TemperatureRecord>
      Parameters:
      other - the record to be compared.
      Returns:
      The value 0 if 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.