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