Class Preferences

java.lang.Object
edu.wpi.first.wpilibj.Preferences

public final class Preferences
extends Object
The preferences class provides a relatively simple way to save important values to the roboRIO to access the next time the roboRIO is booted.

This class loads and saves from a file inside the roboRIO. The user can not access the file directly, but may modify values at specific fields which will then be automatically saved to the file by the NetworkTables server.

This class is thread safe.

This will also interact with NetworkTable by creating a table called "Preferences" with all the key-value pairs.

  • Method Summary

    Modifier and Type Method Description
    boolean containsKey​(String key)
    Returns whether or not there is a key with the given name.
    boolean getBoolean​(String key, boolean backup)
    Returns the boolean at the given key.
    double getDouble​(String key, double backup)
    Returns the double at the given key.
    float getFloat​(String key, float backup)
    Returns the float at the given key.
    static Preferences getInstance()
    Returns the preferences instance.
    int getInt​(String key, int backup)
    Returns the int at the given key.
    Collection<String> getKeys()
    Gets the preferences keys.
    long getLong​(String key, long backup)
    Returns the long at the given key.
    String getString​(String key, String backup)
    Returns the string at the given key.
    void initBoolean​(String key, boolean value)
    Puts the given boolean into the preferences table if it doesn't already exist.
    void initDouble​(String key, double value)
    Puts the given double into the preferences table if it doesn't already exist.
    void initFloat​(String key, float value)
    Puts the given float into the preferences table if it doesn't already exist.
    void initInt​(String key, int value)
    Puts the given int into the preferences table if it doesn't already exist.
    void initLong​(String key, long value)
    Puts the given long into the preferences table if it doesn't already exist.
    void initString​(String key, String value)
    Puts the given string into the preferences table if it doesn't already exist.
    void putBoolean​(String key, boolean value)
    Puts the given boolean into the preferences table.
    void putDouble​(String key, double value)
    Puts the given double into the preferences table.
    void putFloat​(String key, float value)
    Puts the given float into the preferences table.
    void putInt​(String key, int value)
    Puts the given int into the preferences table.
    void putLong​(String key, long value)
    Puts the given long into the preferences table.
    void putString​(String key, String value)
    Puts the given string into the preferences table.
    void remove​(String key)
    Remove a preference.
    void removeAll()
    Remove all preferences.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getInstance

      public static Preferences getInstance()
      Returns the preferences instance.
      Returns:
      the preferences instance
    • getKeys

      public Collection<String> getKeys()
      Gets the preferences keys.
      Returns:
      a collection of the keys
    • putString

      public void putString​(String key, String value)
      Puts the given string into the preferences table.
      Parameters:
      key - the key
      value - the value
      Throws:
      NullPointerException - if value is null
    • initString

      public void initString​(String key, String value)
      Puts the given string into the preferences table if it doesn't already exist.
      Parameters:
      key - The key
      value - The value
    • putInt

      public void putInt​(String key, int value)
      Puts the given int into the preferences table.
      Parameters:
      key - the key
      value - the value
    • initInt

      public void initInt​(String key, int value)
      Puts the given int into the preferences table if it doesn't already exist.
      Parameters:
      key - The key
      value - The value
    • putDouble

      public void putDouble​(String key, double value)
      Puts the given double into the preferences table.
      Parameters:
      key - the key
      value - the value
    • initDouble

      public void initDouble​(String key, double value)
      Puts the given double into the preferences table if it doesn't already exist.
      Parameters:
      key - The key
      value - The value
    • putFloat

      public void putFloat​(String key, float value)
      Puts the given float into the preferences table.
      Parameters:
      key - the key
      value - the value
    • initFloat

      public void initFloat​(String key, float value)
      Puts the given float into the preferences table if it doesn't already exist.
      Parameters:
      key - The key
      value - The value
    • putBoolean

      public void putBoolean​(String key, boolean value)
      Puts the given boolean into the preferences table.
      Parameters:
      key - the key
      value - the value
    • initBoolean

      public void initBoolean​(String key, boolean value)
      Puts the given boolean into the preferences table if it doesn't already exist.
      Parameters:
      key - The key
      value - The value
    • putLong

      public void putLong​(String key, long value)
      Puts the given long into the preferences table.
      Parameters:
      key - the key
      value - the value
    • initLong

      public void initLong​(String key, long value)
      Puts the given long into the preferences table if it doesn't already exist.
      Parameters:
      key - The key
      value - The value
    • containsKey

      public boolean containsKey​(String key)
      Returns whether or not there is a key with the given name.
      Parameters:
      key - the key
      Returns:
      if there is a value at the given key
    • remove

      public void remove​(String key)
      Remove a preference.
      Parameters:
      key - the key
    • removeAll

      public void removeAll()
      Remove all preferences.
    • getString

      public String getString​(String key, String backup)
      Returns the string at the given key. If this table does not have a value for that position, then the given backup value will be returned.
      Parameters:
      key - the key
      backup - the value to return if none exists in the table
      Returns:
      either the value in the table, or the backup
    • getInt

      public int getInt​(String key, int backup)
      Returns the int at the given key. If this table does not have a value for that position, then the given backup value will be returned.
      Parameters:
      key - the key
      backup - the value to return if none exists in the table
      Returns:
      either the value in the table, or the backup
    • getDouble

      public double getDouble​(String key, double backup)
      Returns the double at the given key. If this table does not have a value for that position, then the given backup value will be returned.
      Parameters:
      key - the key
      backup - the value to return if none exists in the table
      Returns:
      either the value in the table, or the backup
    • getBoolean

      public boolean getBoolean​(String key, boolean backup)
      Returns the boolean at the given key. If this table does not have a value for that position, then the given backup value will be returned.
      Parameters:
      key - the key
      backup - the value to return if none exists in the table
      Returns:
      either the value in the table, or the backup
    • getFloat

      public float getFloat​(String key, float backup)
      Returns the float at the given key. If this table does not have a value for that position, then the given backup value will be returned.
      Parameters:
      key - the key
      backup - the value to return if none exists in the table
      Returns:
      either the value in the table, or the backup
    • getLong

      public long getLong​(String key, long backup)
      Returns the long at the given key. If this table does not have a value for that position, then the given backup value will be returned.
      Parameters:
      key - the key
      backup - the value to return if none exists in the table
      Returns:
      either the value in the table, or the backup