Java Preference keyExists(String className, String prefName)

Here you can find the source of keyExists(String className, String prefName)

Description

note that this method is not guaranteed to return an accurate answer if the key in question is nullable.

License

Apache License

Declaration

static boolean keyExists(String className, String prefName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.prefs.Preferences;

public class Main {
    /**/*from  w  w w .  j a v  a 2  s. co  m*/
     * note that this method is not guaranteed to return an accurate answer if
     * the key in question is nullable.
     */
    static boolean keyExists(String className, String prefName) {
        final Preferences prefs = userNodeForClass(className);
        return prefs.get(prefName, null) != null;
    }

    private static Preferences userNodeForClass(Class<?> clazz) {
        final Preferences prefs = Preferences.userNodeForPackage(clazz);
        return prefs.node(clazz.getSimpleName());
    }

    private static Preferences userNodeForClass(String className) {
        final int pkgEndIndex = className.lastIndexOf('.');
        String packageName = className.substring(0, pkgEndIndex);
        String simpleClassName = className.substring(pkgEndIndex + 1);
        String packagePath = "/" + packageName.replace('.', '/');

        final Preferences prefs = Preferences.userRoot().node(packagePath);
        return prefs.node(simpleClassName);
    }
}

Related

  1. clearPreference(Object context)
  2. count(Preferences preferences, String preferenceName)
  3. createNode(String path)
  4. hasEnv(String key, String value)
  5. initEnv(String key, String value)
  6. readAllReg(String path)
  7. savePreference(String key, Object value)
  8. userNodeForClass(Class clazz)
  9. writeReg2(String name, String value, Class clazz)