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