Class PersistedConfiguration

java.lang.Object
com.team2813.lib2813.preferences.PersistedConfiguration

public final class PersistedConfiguration extends Object
Initializes the fields of a Record Class from values stored in Preferences.

The Preference values can be updated in Elastic and other dashboards. Updated values will be stored in the flash storage for the robot.

Example use:


 public final class Drive {

   public record DriveConfiguration(
       boolean addVisionMeasurements, long robotWeight,
       double maxSpeed, String name) {

     public static DriveConfiguration fromPreferences() {
       return PersistedConfiguration.fromPreferences("Drive", DriveConfiguration.class);
     }
   }
 }
 

In the above example, fromPreferences() would return a record instance with the values populated the "Preferences" NetworkTables table. The keys would be:

  • "Drive/addVisionMeasurements"
  • "Drive/robotWeight"
  • "Drive/maxSpeed"
  • "Drive/name"

If no value is stored in Preferences for a key, the default value returned (and initialized in Preferences) would be the default value for the type of the record component. In the above example, if none of the above preference keys existed, preferences will be created with the following values:

  • "Drive/addVisionMeasurements": false
  • "Drive/robotWeight": 0
  • "Drive/maxSpeed": 0.0
  • "Drive/name": ""

The record class could also contain suppliers:


 public final class Drive {

   public record DriveConfiguration(
       boolean addVisionMeasurements, LongSupplier robotWeight,
       DoubleSupplier powerMultiplier) {

     public static DriveConfiguration fromPreferences() {
       return PersistedConfiguration.fromPreferences("Drive", DriveConfiguration.class);
     }
   }
 }
 

In the above example, fromPreferences() would return a record instance that contained suppliers that, when queried, would return the current value in the "Preferences" NetworkTables table.

The caller could specify different default values by passing an instance of the record class:


 public final class Drive {

   public record DriveConfiguration(
       boolean addVisionMeasurements, long robotWeight,
       DoubleSupplier maxAngularVelocity) {

     public static DriveConfiguration fromPreferences() {
       DriveConfiguration defaultConfig = new DriveConfiguration(
           true, 2813, () -> 3.14);
       return PersistedConfiguration.fromPreferences("Drive", defaultConfig);
     }
   }
 }
 

In the above example, fromPreferences() would return a record instance with the values populated the "Preferences" NetworkTables table. The keys and default values would be:

  • "Drive/addVisionMeasurements" (default value: true)
  • "Drive/robotWeight" (default value: 2813)
  • "Drive/maxAngularVelocity" (default value: 3.14)

Note that PersistedConfiguration will use the default record constructor to create record instances, so any parameter validation should be done in a custom constructor; see Custom Constructor in Java Records for details.

For record classes with many component values of the same type, it is strongly recommended that a builder is provided to construct the record, to avoid callers passing the parameters in the wrong order. To make generation of a builder easier, consider using @AutoBuilder from Google Auto or @Builder from Project Lombok.

Since:
2.0.0
  • Method Details

    • fromPreferences

      public static <T extends Record> T fromPreferences(String preferenceName, T configWithDefaults)
      Creates a record class instance with fields populated from Preferences, using the provided defaults.

      To be stored in preferences, the type of the record components can be any of the following:

      • boolean or BooleanSupplier
      • int or IntSupplier
      • long or LongSupplier
      • double or DoubleSupplier
      • String or Supplier<String>
      • Record following the above rules

      The values for the components for the passed-in instance will be used as the default value for the preference. If a component is a supplier, the supplier will be called at most once to get the default instance. Suppliers cannot return null.

      Parameters:
      preferenceName - Preference subtable to use to get the values.
      configWithDefaults - Record instance with all values set to their preferred default values.
      Returns:
      An instance of the record class, populated with data from the Preferences table.
      Throws:
      IllegalArgumentException - If preferenceName is empty or contains a '/'.
      IllegalStateException - If preferenceName was used for a different record class.
    • fromPreferences

      public static <T extends Record> T fromPreferences(String preferenceName, Class<T> recordClass)
      Creates a record class instance of the provided type, with fields populated from Preferences.

      To be stored in preferences, the type of the record components can be any of the following:

      • boolean or BooleanSupplier
      • int or IntSupplier
      • long or LongSupplier
      • double or DoubleSupplier
      • String or Supplier<String>
      • Record following the above rules

      The default values for the preferences will be Java defaults (for example, zero for integers, an empty string for strings, etc.).

      Parameters:
      preferenceName - Preference subtable to use to get the values.
      recordClass - Type of the record instance to populate from preferences.
      Returns:
      An instance of the record class, populated with data from the Preferences table.
      Throws:
      IllegalArgumentException - If preferenceName is empty or contains a '/'.
      IllegalStateException - If preferenceName was used for a different record class.