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.LinearVelocity; 008 009/** A unit of linear velocity like {@link edu.wpi.first.units.Units#MetersPerSecond}. */ 010public final class LinearVelocityUnit extends PerUnit<DistanceUnit, TimeUnit> { 011 private static final CombinatoryUnitCache<DistanceUnit, TimeUnit, LinearVelocityUnit> cache = 012 new CombinatoryUnitCache<>(LinearVelocityUnit::new); 013 014 LinearVelocityUnit(DistanceUnit unit, TimeUnit period) { 015 super( 016 unit.isBaseUnit() && period.isBaseUnit() 017 ? null 018 : combine(unit.getBaseUnit(), period.getBaseUnit()), 019 unit, 020 period); 021 } 022 023 LinearVelocityUnit( 024 PerUnit<DistanceUnit, TimeUnit> baseUnit, 025 UnaryFunction toBaseConverter, 026 UnaryFunction fromBaseConverter, 027 String name, 028 String symbol) { 029 super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol); 030 } 031 032 /** 033 * Combines a distance and time unit for form a combined unit of velocity. 034 * 035 * @param distance the unit of distance 036 * @param period the unit of time 037 * @return the combined velocity unit 038 */ 039 public static LinearVelocityUnit combine(DistanceUnit distance, TimeUnit period) { 040 return cache.combine(distance, period); 041 } 042 043 @Override 044 public LinearVelocityUnit getBaseUnit() { 045 return (LinearVelocityUnit) super.getBaseUnit(); 046 } 047 048 @Override 049 public LinearVelocity of(double magnitude) { 050 return new LinearVelocity(magnitude, toBaseUnits(magnitude), this); 051 } 052 053 @Override 054 public LinearVelocity ofBaseUnits(double baseUnitMagnitude) { 055 return new LinearVelocity(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this); 056 } 057 058 @Override 059 public LinearVelocity zero() { 060 return (LinearVelocity) super.zero(); 061 } 062 063 @Override 064 public LinearVelocity one() { 065 return (LinearVelocity) super.one(); 066 } 067 068 /** 069 * Combines this velocity with a time period of change to form a unit of acceleration. 070 * 071 * @param period the period of change in the velocity 072 * @return the combined acceleration unit 073 */ 074 @Override 075 public LinearAccelerationUnit per(TimeUnit period) { 076 return LinearAccelerationUnit.combine(this, period); 077 } 078 079 /** 080 * Creates a ratio unit between this unit and an arbitrary other unit. 081 * 082 * @param other the other unit 083 * @param <U> the type of the other unit 084 * @return the ratio unit 085 */ 086 public <U extends Unit> PerUnit<LinearVelocityUnit, U> per(U other) { 087 return PerUnit.combine(this, other); 088 } 089 090 /** 091 * Converts a measurement value in terms of another unit to this unit. 092 * 093 * @param magnitude the magnitude of the measurement in terms of the other unit 094 * @param otherUnit the other unit 095 * @return the value of the measurement in terms of this unit 096 */ 097 public double convertFrom(double magnitude, LinearVelocityUnit otherUnit) { 098 return fromBaseUnits(otherUnit.toBaseUnits(magnitude)); 099 } 100}