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.Energy;
008import edu.wpi.first.units.measure.ImmutableEnergy;
009import edu.wpi.first.units.measure.MutEnergy;
010
011/**
012 * Unit of energy dimension.
013 *
014 * <p>This is the base type for units of energy dimension. It is also used to specify the dimension
015 * for {@link Measure}: <code>Measure&lt;EnergyUnit&gt;</code>.
016 *
017 * <p>Actual units (such as {@link Units#Joules} and {@link Units#Kilojoules}) can be found in the
018 * {@link Units} class.
019 */
020public final class EnergyUnit extends Unit {
021  EnergyUnit(
022      EnergyUnit baseUnit,
023      UnaryFunction toBaseConverter,
024      UnaryFunction fromBaseConverter,
025      String name,
026      String symbol) {
027    super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
028  }
029
030  EnergyUnit(EnergyUnit baseUnit, double baseUnitEquivalent, String name, String symbol) {
031    super(baseUnit, baseUnitEquivalent, name, symbol);
032  }
033
034  @Override
035  public EnergyUnit getBaseUnit() {
036    return (EnergyUnit) super.getBaseUnit();
037  }
038
039  /**
040   * Combines this unit of energy with a unit of time to create a unit of power.
041   *
042   * @param period the period of the change in energy
043   * @return the combined unit of power
044   */
045  @Override
046  public PowerUnit per(TimeUnit period) {
047    return PowerUnit.combine(this, period);
048  }
049
050  /**
051   * Creates a ratio unit between this unit and an arbitrary other unit.
052   *
053   * @param other the other unit
054   * @param <U> the type of the other unit
055   * @return the ratio unit
056   */
057  public <U extends Unit> PerUnit<EnergyUnit, U> per(U other) {
058    return PerUnit.combine(this, other);
059  }
060
061  /**
062   * Converts a measurement value in terms of another unit to this unit.
063   *
064   * @param magnitude the magnitude of the measurement in terms of the other unit
065   * @param otherUnit the other unit
066   * @return the value of the measurement in terms of this unit
067   */
068  public double convertFrom(double magnitude, EnergyUnit otherUnit) {
069    return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
070  }
071
072  @Override
073  public Energy of(double magnitude) {
074    return new ImmutableEnergy(magnitude, toBaseUnits(magnitude), this);
075  }
076
077  @Override
078  public Energy ofBaseUnits(double baseUnitMagnitude) {
079    return new ImmutableEnergy(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
080  }
081
082  @Override
083  public Energy zero() {
084    return (Energy) super.zero();
085  }
086
087  @Override
088  public Energy one() {
089    return (Energy) super.one();
090  }
091
092  @Override
093  public MutEnergy mutable(double initialMagnitude) {
094    return new MutEnergy(initialMagnitude, toBaseUnits(initialMagnitude), this);
095  }
096}