001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.units; 006 007import edu.wpi.first.units.measure.ImmutableTemperature; 008import edu.wpi.first.units.measure.MutTemperature; 009import edu.wpi.first.units.measure.Temperature; 010 011/** 012 * Unit of temperature dimension. 013 * 014 * <p>This is the base type for units of temperature dimension. It is also used to specify the 015 * dimension for {@link Measure}: <code>Measure<TemperatureUnit></code>. 016 * 017 * <p>Actual units (such as {@link Units#Celsius} and {@link Units#Fahrenheit}) can be found in the 018 * {@link Units} class. 019 */ 020public final class TemperatureUnit extends Unit { 021 TemperatureUnit( 022 TemperatureUnit baseUnit, 023 UnaryFunction toBaseConverter, 024 UnaryFunction fromBaseConverter, 025 String name, 026 String symbol) { 027 super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol); 028 } 029 030 @Override 031 public TemperatureUnit getBaseUnit() { 032 return (TemperatureUnit) super.getBaseUnit(); 033 } 034 035 @Override 036 public Temperature of(double magnitude) { 037 return new ImmutableTemperature(magnitude, toBaseUnits(magnitude), this); 038 } 039 040 @Override 041 public Temperature ofBaseUnits(double baseUnitMagnitude) { 042 return new ImmutableTemperature(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this); 043 } 044 045 @Override 046 public Temperature zero() { 047 return (Temperature) super.zero(); 048 } 049 050 @Override 051 public Temperature one() { 052 return (Temperature) super.one(); 053 } 054 055 @Override 056 public MutTemperature mutable(double initialMagnitude) { 057 return new MutTemperature(initialMagnitude, toBaseUnits(initialMagnitude), this); 058 } 059 060 @Override 061 public VelocityUnit<TemperatureUnit> per(TimeUnit period) { 062 return VelocityUnit.combine(this, period); 063 } 064 065 /** 066 * Creates a ratio unit between this unit and an arbitrary other unit. 067 * 068 * @param other the other unit 069 * @param <U> the type of the other unit 070 * @return the ratio unit 071 */ 072 public <U extends Unit> PerUnit<TemperatureUnit, U> per(U other) { 073 return PerUnit.combine(this, other); 074 } 075 076 /** 077 * Converts a measurement value in terms of another temperature unit to this unit. 078 * 079 * @param magnitude the magnitude of the measurement in terms of the other temperature unit 080 * @param otherUnit the other temperature unit 081 * @return the value of the measurement in terms of this unit 082 */ 083 public double convertFrom(double magnitude, TemperatureUnit otherUnit) { 084 return fromBaseUnits(otherUnit.toBaseUnits(magnitude)); 085 } 086}