# Class Preferences

java.lang.Object
com.codename1.io.Preferences

public final class Preferences extends java.lang.Object

Simple map like class to store application and Codename One preference settings in the `com.codename1.io.Storage`.

Simple usage of the class for storing a `String` token:

    // save a token to storage
    String myToken = "abc123";
    Preferences.set("token", myToken);

    // get the token from storage or null if it isn't there
    String token = Preferences.get("token", null);

Notice that this class might get somewhat confusing with primitive numbers e.g. if you use `Preferences.set("primitiveLongValue", myLongNumber)` then invoke `Preferences.get("primitiveLongValue", 0)` you might get an exception!

This would happen because the value is physically a `Long` object but you are trying to get an `Integer`.

The workaround is to remain consistent and use code like this `Preferences.get("primitiveLongValue", (long)0)`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `addPreferenceListener(String pref, PreferenceListener listener)`
  Adds a preference listener for the specified property to the list of listeners.
  `static void`
  `clearAll()`
  Remove all preferences
  `static void`
  `delete(String pref)`
  Deletes a value for the given setting
  `static boolean`
  `get(String pref, boolean def)`
  Gets the value as a number
  `static double`
  `get(String pref, double def)`
  Gets the value as a number
  `static float`
  `get(String pref, float def)`
  Gets the value as a number
  `static int`
  `get(String pref, int def)`
  Gets the value as a number
  `static long`
  `get(String pref, long def)`
  Gets the value as a number
  `static String`
  `get(String pref, String def)`
  Gets the value as a String
  `static boolean`
  `getAndSet(String pref, boolean def)`
  Gets the value as a number if the value is null def is returned and saved
  `static double`
  `getAndSet(String pref, double def)`
  Gets the value as a number if the value is null def is returned and saved
  `static float`
  `getAndSet(String pref, float def)`
  Gets the value as a number if the value is null def is returned and saved
  `static int`
  `getAndSet(String pref, int def)`
  Gets the value as a number if the value is null def is returned and saved
  `static long`
  `getAndSet(String pref, long def)`
  Gets the value as a number if the value is null def is returned and saved
  `static String`
  `getAndSet(String pref, String def)`
  Gets the value as a String if the value is null def is returned and saved
  `static String`
  `getPreferencesLocation()`
  Returns the location within the storage of the preferences file to an arbitrary name.
  `static boolean`
  `removePreferenceListener(String pref, PreferenceListener listener)`
  Remove the listener for the specified preference.
  `static void`
  `set(String pref, boolean b)`
  Sets a preference value
  `static void`
  `set(String pref, double d)`
  Sets a preference value
  `static void`
  `set(String pref, float f)`
  Sets a preference value
  `static void`
  `set(String pref, int i)`
  Sets a preference value
  `static void`
  `set(String pref, long l)`
  Sets a preference value
  `static void`
  `set(String pref, String s)`
  Sets a preference value
  `static void`
  `set(Map<String, java.lang.Object> values)`
  Sets a set of preference values as a batch, and performs a single save.
  `static void`
  `setPreferencesLocation(String storageFileName)`
  Sets the location within the storage of the preferences file to an arbitrary name.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getPreferencesLocation

    public static String getPreferencesLocation()

    Returns the location within the storage of the preferences file to an arbitrary name. This is useful in a case of encryption where we would want preferences to use a different file name.

    ###### Returns

    the storage file name

  - ### setPreferencesLocation

    public static void setPreferencesLocation(String storageFileName)

    Sets the location within the storage of the preferences file to an arbitrary name. This is useful in a case of encryption where we would want preferences to use a different file name.

    ###### Parameters

    - `storageFileName`: the name of the preferences file

  - ### set

    public static void set(Map<String, java.lang.Object> values)

    Sets a set of preference values as a batch, and performs a single save.

    ###### Parameters

    - `values`: The key/value pairs to set in preferences.

  - ### set

    public static void set(String pref, String s)

    Sets a preference value

    ###### Parameters

    - `pref`: the key any unique none null value that doesn't start with cn1

    - `s`: a String

  - ### set

    public static void set(String pref, int i)

    Sets a preference value

    ###### Parameters

    - `pref`: the key any unique none null value that doesn't start with cn1

    - `i`: a number

  - ### set

    public static void set(String pref, long l)

    Sets a preference value

    ###### Parameters

    - `pref`: the key any unique none null value that doesn't start with cn1

    - `l`: a number

  - ### set

    public static void set(String pref, double d)

    Sets a preference value

    ###### Parameters

    - `pref`: the key any unique none null value that doesn't start with cn1

    - `d`: a number

  - ### set

    public static void set(String pref, float f)

    Sets a preference value

    ###### Parameters

    - `pref`: the key any unique none null value that doesn't start with cn1

    - `f`: a number

  - ### delete

    public static void delete(String pref)

    Deletes a value for the given setting

    ###### Parameters

    - `pref`: the preference value

  - ### clearAll

    public static void clearAll()
    Remove all preferences

  - ### set

    public static void set(String pref, boolean b)

    Sets a preference value

    ###### Parameters

    - `pref`: the key any unique none null value that doesn't start with cn1

    - `b`: the value

  - ### get

    public static String get(String pref, String def)

    Gets the value as a String

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### getAndSet

    public static String getAndSet(String pref, String def)

    Gets the value as a String if the value is null def is returned and saved

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### getAndSet

    public static int getAndSet(String pref, int def)

    Gets the value as a number if the value is null def is returned and saved

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### get

    public static int get(String pref, int def)

    Gets the value as a number

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### getAndSet

    public static long getAndSet(String pref, long def)

    Gets the value as a number if the value is null def is returned and saved

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### get

    public static long get(String pref, long def)

    Gets the value as a number

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### getAndSet

    public static double getAndSet(String pref, double def)

    Gets the value as a number if the value is null def is returned and saved

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### get

    public static double get(String pref, double def)

    Gets the value as a number

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### getAndSet

    public static float getAndSet(String pref, float def)

    Gets the value as a number if the value is null def is returned and saved

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### get

    public static float get(String pref, float def)

    Gets the value as a number

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### getAndSet

    public static boolean getAndSet(String pref, boolean def)

    Gets the value as a number if the value is null def is returned and saved

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### get

    public static boolean get(String pref, boolean def)

    Gets the value as a number

    ###### Parameters

    - `pref`: the preference key

    - `def`: the default value

    ###### Returns

    the default value or the value

  - ### addPreferenceListener

    public static void addPreferenceListener(String pref, PreferenceListener listener)

    Adds a preference listener for the specified property to the list of listeners. When calling this method, it is advisable to also read the current value and set it, since the value may have changed since the last time the listener was removed. (Should this return the current value of the preference?)

    ###### Parameters

    - `pref`: The preference to listen to

    - `listener`: The listener to add, which cannot be null.

  - ### removePreferenceListener

    public static boolean removePreferenceListener(String pref, PreferenceListener listener)

    Remove the listener for the specified preference.

    ###### Parameters

    - `pref`: The preference that the listener listens to

    - `listener`: The listener to remove

    ###### Returns

    true if the listener was removed, false if it was not found.