Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/main/java/xbot/common/properties/PropertyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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;

import edu.wpi.first.units.measure.Angle;
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.
Expand Down Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/main/java/xbot/common/properties/TimeProperty.java
Original file line number Diff line number Diff line change
@@ -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<Time, MutTime, TimeUnit> {
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);
}
}
22 changes: 22 additions & 0 deletions src/test/java/xbot/common/properties/PropertyFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}