From a6c164e5ba00fd9a17cc7dd4b6df055db576a481 Mon Sep 17 00:00:00 2001 From: Rongrui Zhu Date: Thu, 5 Mar 2026 19:31:05 -0800 Subject: [PATCH] Added TimeProperty --- .../common/properties/PropertyFactory.java | 24 ++++++++++++++++++- .../xbot/common/properties/TimeProperty.java | 20 ++++++++++++++++ .../properties/PropertyFactoryTest.java | 22 +++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/main/java/xbot/common/properties/TimeProperty.java diff --git a/src/main/java/xbot/common/properties/PropertyFactory.java b/src/main/java/xbot/common/properties/PropertyFactory.java index 5d28d509..c548ef32 100644 --- a/src/main/java/xbot/common/properties/PropertyFactory.java +++ b/src/main/java/xbot/common/properties/PropertyFactory.java @@ -3,6 +3,7 @@ import javax.inject.Inject; import edu.wpi.first.units.measure.AngularVelocity; +import edu.wpi.first.units.measure.Time; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -10,7 +11,6 @@ import edu.wpi.first.units.measure.Distance; import xbot.common.logging.RobotAssertionManager; import xbot.common.properties.Property.PropertyLevel; -import xbot.common.properties.Property.PropertyPersistenceType; /** * A factory for creating properties. This is the preferred way to create properties. @@ -233,4 +233,26 @@ public AngularVelocityProperty createPersistentProperty(String suffix, AngularVe checkPrefixSet(); return new AngularVelocityProperty(getCleanPrefix(), suffix, defaultValue, this.propertyManager, level); } + + /** + * Creates a persistent property for an angle. + * @param suffix The suffix for the property. + * @param defaultValue The default value for the property. + * @return The property. + */ + public TimeProperty createPersistentProperty(String suffix, Time defaultValue) { + return this.createPersistentProperty(suffix, defaultValue, defaultLevel); + } + + /** + * Creates a persistent property for an angle with a specified property level. + * @param suffix The suffix for the property. + * @param defaultValue The default value for the property. + * @param level The property level. + * @return The property. + */ + public TimeProperty createPersistentProperty(String suffix, Time defaultValue, PropertyLevel level) { + checkPrefixSet(); + return new TimeProperty(getCleanPrefix(), suffix, defaultValue, this.propertyManager, level); + } } \ No newline at end of file diff --git a/src/main/java/xbot/common/properties/TimeProperty.java b/src/main/java/xbot/common/properties/TimeProperty.java new file mode 100644 index 00000000..ddccd734 --- /dev/null +++ b/src/main/java/xbot/common/properties/TimeProperty.java @@ -0,0 +1,20 @@ +package xbot.common.properties; + +import edu.wpi.first.units.TimeUnit; +import edu.wpi.first.units.measure.MutTime; +import edu.wpi.first.units.measure.Time; + +/** + * This manages an Angle in the property system. + * + * @author Alex + */ +public class TimeProperty extends MeasureProperty { + public TimeProperty(String prefix, String name, Time defaultValue, XPropertyManager manager) { + super(prefix, name, defaultValue, manager); + } + + public TimeProperty(String prefix, String name, Time defaultValue, XPropertyManager manager, PropertyLevel level) { + super(prefix, name, defaultValue, manager, level); + } +} diff --git a/src/test/java/xbot/common/properties/PropertyFactoryTest.java b/src/test/java/xbot/common/properties/PropertyFactoryTest.java index 0512e5ff..79dcb39b 100644 --- a/src/test/java/xbot/common/properties/PropertyFactoryTest.java +++ b/src/test/java/xbot/common/properties/PropertyFactoryTest.java @@ -7,8 +7,10 @@ import static edu.wpi.first.units.Units.DegreesPerSecond; import static edu.wpi.first.units.Units.Inches; import static edu.wpi.first.units.Units.Meters; +import static edu.wpi.first.units.Units.Milliseconds; import static edu.wpi.first.units.Units.Radians; import static edu.wpi.first.units.Units.RadiansPerSecond; +import static edu.wpi.first.units.Units.Seconds; import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -86,4 +88,24 @@ public void testAngularVelocityProperty() { // suffix should be modified to state the units assertEquals("test-in-Radian per Seconds", propertyRadians.suffix); } + + @Test + public void testTimeProperty() { + PropertyFactory factory = getInjectorComponent().propertyFactory(); + factory.setPrefix("myPrefix"); + + // should get back the same value as we put in with the same unit as the defaultValue + TimeProperty propertySeconds = factory.createPersistentProperty("test", Seconds.of(5)); + assertEquals(5, propertySeconds.get().in(Seconds), 0.0001); + // suffix should be modified to state the units + assertEquals("test-in-Seconds", propertySeconds.suffix); + propertySeconds.set(Seconds.of(10)); + assertEquals(10, propertySeconds.get().in(Seconds), 0.0001); + + // should get back the same value as we put in with the same unit as the defaultValue + TimeProperty propertyMilliseconds = factory.createPersistentProperty("test", Milliseconds.of(Math.PI)); + assertEquals(Math.PI, propertyMilliseconds.get().in(Milliseconds), 0.0001); + // suffix should be modified to state the units + assertEquals("test-in-Milliseconds", propertyMilliseconds.suffix); + } } \ No newline at end of file