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.Dimensionless;
008
009/**
010 * A type of unit that corresponds to raw values and not any physical dimension, such as percentage.
011 */
012public final class DimensionlessUnit extends Unit {
013  /**
014   * Creates a new unit with the given name and multiplier to the base unit.
015   *
016   * @param baseUnitEquivalent the multiplier to convert this unit to the base unit of this type.
017   * @param name the name of the unit
018   * @param symbol the symbol of the unit
019   */
020  DimensionlessUnit(
021      DimensionlessUnit baseUnit, double baseUnitEquivalent, String name, String symbol) {
022    super(baseUnit, baseUnitEquivalent, name, symbol);
023  }
024
025  DimensionlessUnit(
026      DimensionlessUnit baseUnit,
027      UnaryFunction toBaseConverter,
028      UnaryFunction fromBaseConverter,
029      String name,
030      String symbol) {
031    super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
032  }
033
034  @Override
035  public DimensionlessUnit getBaseUnit() {
036    return (DimensionlessUnit) super.getBaseUnit();
037  }
038
039  @Override
040  public Dimensionless of(double magnitude) {
041    return new Dimensionless(magnitude, toBaseUnits(magnitude), this);
042  }
043
044  @Override
045  public Dimensionless ofBaseUnits(double baseUnitMagnitude) {
046    return new Dimensionless(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
047  }
048
049  @Override
050  public Dimensionless zero() {
051    return (Dimensionless) super.zero();
052  }
053
054  @Override
055  public Dimensionless one() {
056    return (Dimensionless) super.one();
057  }
058
059  /**
060   * Converts a measurement value in terms of another dimensionless unit to this unit.
061   *
062   * @param magnitude the magnitude of the measurement in terms of the other dimensionless unit
063   * @param otherUnit the other dimensionless unit
064   * @return the value of the measurement in terms of this unit
065   */
066  public double convertFrom(double magnitude, DimensionlessUnit otherUnit) {
067    return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
068  }
069
070  /**
071   * Creates a frequency unit as the ratio of this dimensionless unit to the period of time in which
072   * a single cycle is made.
073   *
074   * @param period the cycle period
075   * @return the combined frequency unit
076   */
077  @Override
078  public FrequencyUnit per(TimeUnit period) {
079    return FrequencyUnit.combine(this, period);
080  }
081
082  /**
083   * Creates a ratio unit between this unit and an arbitrary other unit.
084   *
085   * @param other the other unit
086   * @param <U> the type of the other unit
087   * @return the ratio unit
088   */
089  public <U extends Unit> PerUnit<DimensionlessUnit, U> per(U other) {
090    return PerUnit.combine(this, other);
091  }
092}