📄 preferences.java
字号:
* <p>This convention does not apply to the unnamed package, whose * associated preference node is <tt><unnamed></tt>. This node * is not intended for long term use, but for convenience in the early * development of programs that do not yet belong to a package, and * for "throwaway" programs. <i>Valuable data should not be stored * at this node as it is shared by all programs that use it.</i> * * <p>A class <tt>Foo</tt> wishing to access preferences pertaining to its * package can obtain a preference node as follows: <pre> * static Preferences prefs = Preferences.systemNodeForPackage(Foo.class); * </pre> * This idiom obviates the need for using a string to describe the * preferences node and decreases the likelihood of a run-time failure. * (If the class name is is misspelled, it will typically result in a * compile-time error.) * * <p>Invoking this method will result in the creation of the returned * node and its ancestors if they do not already exist. If the returned * node did not exist prior to this call, this node and any ancestors that * were created by this call are not guaranteed to become permanent until * the <tt>flush</tt> method is called on the returned node (or one of its * ancestors or descendants). * * @param c the class for whose package a system preference node is desired. * @return the system preference node associated with the package of which * <tt>c</tt> is a member. * @throws NullPointerException if <tt>c</tt> is <tt>null</tt>. * @throws SecurityException if a security manager is present and * it denies <tt>RuntimePermission("preferences")</tt>. * @see RuntimePermission */ public static Preferences systemNodeForPackage(Class c) { return systemRoot().node(nodeName(c)); } /** * Returns the absolute path name of the node corresponding to the package * of the specified object. * * @throws IllegalArgumentException if the package has node preferences * node associated with it. */ private static String nodeName(Class c) { if (c.isArray()) throw new IllegalArgumentException( "Arrays have no associated preferences node."); String className = c.getName(); int pkgEndIndex = className.lastIndexOf('.'); if (pkgEndIndex < 0) return "/<unnamed>"; String packageName = className.substring(0, pkgEndIndex); return "/" + packageName.replace('.', '/'); } /** * This permission object represents the permission required to get * access to the user or system root (which in turn allows for all * other operations). */ private static Permission prefsPerm = new RuntimePermission("preferences"); /** * Returns the root preference node for the calling user. * * @return the root preference node for the calling user. * @throws SecurityException If a security manager is present and * it denies <tt>RuntimePermission("preferences")</tt>. * @see RuntimePermission */ public static Preferences userRoot() { SecurityManager security = System.getSecurityManager(); if (security != null) security.checkPermission(prefsPerm); return factory.userRoot(); } /** * Returns the root preference node for the system. * * @return the root preference node for the system. * @throws SecurityException If a security manager is present and * it denies <tt>RuntimePermission("preferences")</tt>. * @see RuntimePermission */ public static Preferences systemRoot() { SecurityManager security = System.getSecurityManager(); if (security != null) security.checkPermission(prefsPerm); return factory.systemRoot(); } /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) */ protected Preferences() { } /** * Associates the specified value with the specified key in this * preference node. * * @param key key with which the specified value is to be associated. * @param value value to be associated with the specified key. * @throws NullPointerException if key or value is <tt>null</tt>. * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds * <tt>MAX_KEY_LENGTH</tt> or if <tt>value.length</tt> exceeds * <tt>MAX_VALUE_LENGTH</tt>. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. */ public abstract void put(String key, String value); /** * Returns the value associated with the specified key in this preference * node. Returns the specified default if there is no value associated * with the key, or the backing store is inaccessible. * * <p>Some implementations may store default values in their backing * stores. If there is no value associated with the specified key * but there is such a <i>stored default</i>, it is returned in * preference to the specified default. * * @param key key whose associated value is to be returned. * @param def the value to be returned in the event that this * preference node has no value associated with <tt>key</tt>. * @return the value associated with <tt>key</tt>, or <tt>def</tt> * if no value is associated with <tt>key</tt>, or the backing * store is inaccessible. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A * <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.) */ public abstract String get(String key, String def); /** * Removes the value associated with the specified key in this preference * node, if any. * * <p>If this implementation supports <i>stored defaults</i>, and there is * such a default for the specified preference, the stored default will be * "exposed" by this call, in the sense that it will be returned * by a succeeding call to <tt>get</tt>. * * @param key key whose mapping is to be removed from the preference node. * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. */ public abstract void remove(String key); /** * Removes all of the preferences (key-value associations) in this * preference node. This call has no effect on any descendants * of this node. * * <p>If this implementation supports <i>stored defaults</i>, and this * node in the preferences hierarchy contains any such defaults, * the stored defaults will be "exposed" by this call, in the sense that * they will be returned by succeeding calls to <tt>get</tt>. * * @throws BackingStoreException if this operation cannot be completed * due to a failure in the backing store, or inability to * communicate with it. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. * @see #removeNode() */ public abstract void clear() throws BackingStoreException; /** * Associates a string representing the specified int value with the * specified key in this preference node. The associated string is the * one that would be returned if the int value were passed to * {@link Integer#toString(int)}. This method is intended for use in * conjunction with {@link #getInt}. * * @param key key with which the string form of value is to be associated. * @param value value whose string form is to be associated with key. * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds * <tt>MAX_KEY_LENGTH</tt>. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. * @see #getInt(String,int) */ public abstract void putInt(String key, int value); /** * Returns the int value represented by the string associated with the * specified key in this preference node. The string is converted to * an integer as by {@link Integer#parseInt(String)}. Returns the * specified default if there is no value associated with the key, * the backing store is inaccessible, or if * <tt>Integer.parseInt(String)</tt> would throw a {@link * NumberFormatException} if the associated value were passed. This * method is intended for use in conjunction with {@link #putInt}. * * <p>If the implementation supports <i>stored defaults</i> and such a * default exists, is accessible, and could be converted to an int * with <tt>Integer.parseInt</tt>, this int is returned in preference to * the specified default. * * @param key key whose associated value is to be returned as an int. * @param def the value to be returned in the event that this * preference node has no value associated with <tt>key</tt> * or the associated value cannot be interpreted as an int, * or the backing store is inaccessible. * @return the int value represented by the string associated with * <tt>key</tt> in this preference node, or <tt>def</tt> if the * associated value does not exist or cannot be interpreted as * an int. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. * @see #putInt(String,int) * @see #get(String,String) */ public abstract int getInt(String key, int def); /** * Associates a string representing the specified long value with the * specified key in this preference node. The associated string is the * one that would be returned if the long value were passed to * {@link Long#toString(long)}. This method is intended for use in * conjunction with {@link #getLong}. * * @param key key with which the string form of value is to be associated. * @param value value whose string form is to be associated with key. * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds * <tt>MAX_KEY_LENGTH</tt>. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. * @see #getLong(String,long) */ public abstract void putLong(String key, long value); /** * Returns the long value represented by the string associated with the * specified key in this preference node. The string is converted to * a long as by {@link Long#parseLong(String)}. Returns the * specified default if there is no value associated with the key, * the backing store is inaccessible, or if * <tt>Long.parseLong(String)</tt> would throw a {@link * NumberFormatException} if the associated value were passed. This * method is intended for use in conjunction with {@link #putLong}. * * <p>If the implementation supports <i>stored defaults</i> and such a * default exists, is accessible, and could be converted to a long * with <tt>Long.parseLong</tt>, this long is returned in preference to * the specified default. * * @param key key whose associated value is to be returned as a long. * @param def the value to be returned in the event that this * preference node has no value associated with <tt>key</tt> * or the associated value cannot be interpreted as a long, * or the backing store is inaccessible. * @return the long value represented by the string associated with * <tt>key</tt> in this preference node, or <tt>def</tt> if the * associated value does not exist or cannot be interpreted as * a long. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. * @see #putLong(String,long) * @see #get(String,String) */ public abstract long getLong(String key, long def); /** * Associates a string representing the specified boolean value with the * specified key in this preference node. The associated string is * <tt>"true"</tt> if the value is true, and <tt>"false"</tt> if it is * false. This method is intended for use in conjunction with * {@link #getBoolean}. * * @param key key with which the string form of value is to be associated. * @param value value whose string form is to be associated with key. * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds * <tt>MAX_KEY_LENGTH</tt>. * @throws IllegalStateException if this node (or an ancestor) has been * removed with the {@link #removeNode()} method. * @see #getBoolean(String,boolean) * @see #get(String,String) */ public abstract void putBoolean(String key, boolean value); /** * Returns the boolean value represented by the string associated with the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -