Java Preference Get getList(String path)

Here you can find the source of getList(String path)

Description

Get a list of strings from preferences.

License

Open Source License

Parameter

Parameter Description
path The absolute path name of the node.

Return

The list of strings.

Declaration

public static ArrayList<String> getList(String path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.prefs.Preferences;
import java.util.prefs.BackingStoreException;

public class Main {
    /** Get a list of strings from preferences.
    The list is stored as a size property end element_N properties with
    N being the element index./*from   w  w w.  j av  a 2  s  . co  m*/
    @param path The absolute path name of the node.
    @return The list of strings. */
    public static ArrayList<String> getList(String path) {
        Preferences prefs = getNode(path);
        if (prefs == null)
            return new ArrayList<String>();
        int size = prefs.getInt("size", 0);
        if (size <= 0)
            return new ArrayList<String>();
        ArrayList<String> result = new ArrayList<String>(size);
        for (int i = 0; i < size; ++i) {
            String element = prefs.get("element_" + i, null);
            if (element == null)
                // Should not happen
                break;
            result.add(element);
        }
        return result;
    }

    /** Get node for package and path, return null if not already existing.
    @param path The absolute path name of the node.
    @return The node or null, if node does not exist or failure in the
    backing store. */
    public static Preferences getNode(String path) {
        assert !path.startsWith("/");
        Preferences prefs = Preferences.userRoot();
        try {
            if (!prefs.nodeExists(path))
                return null;
        } catch (BackingStoreException e) {
            return null;
        }
        return prefs.node(path);
    }
}

Related

  1. getCLOB(Preferences prefs, String key, String def)
  2. getClobStorage(Preferences prefs, String key)
  3. getDefault()
  4. getFloat(String key, Float def)
  5. getInt(final Class preferencesClass, final String preferenceName)
  6. getNode(String path)
  7. getPreference(String key, Boolean defaultValue)
  8. getPreferences()
  9. getPreferences(final Class preferencesClass)