jdk/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java
author bpb
Fri, 22 Jan 2016 12:44:32 -0800
changeset 35311 613162418a3d
parent 32037 ab4526f4ac10
permissions -rw-r--r--
8147545: Remove sun.misc.ManagedLocalsThread from java.prefs Summary: Replace ManagedLocalsThread with Thread(null,null,threadName,0,false) Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35311
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 32037
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.prefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
// These imports needed only as a workaround for a JavaDoc bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.Integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.Long;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.Float;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.Double;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class provides a skeletal implementation of the {@link Preferences}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * class, greatly easing the task of implementing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    42
 * <p><strong>This class is for {@code Preferences} implementers only.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    43
 * Normal users of the {@code Preferences} facility should have no need to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * consult this documentation.  The {@link Preferences} documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * should suffice.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>Implementors must override the nine abstract service-provider interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * (SPI) methods: {@link #getSpi(String)}, {@link #putSpi(String,String)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@link #removeSpi(String)}, {@link #childSpi(String)}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * #removeNodeSpi()}, {@link #keysSpi()}, {@link #childrenNamesSpi()}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * #syncSpi()} and {@link #flushSpi()}.  All of the concrete methods specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * precisely how they are implemented atop these SPI methods.  The implementor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * may, at his discretion, override one or more of the concrete methods if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * default implementation is unsatisfactory for any reason, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>The SPI methods fall into three groups concerning exception
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    58
 * behavior. The {@code getSpi} method should never throw exceptions, but it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * doesn't really matter, as any exception thrown by this method will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * intercepted by {@link #get(String,String)}, which will return the specified
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    61
 * default value to the caller.  The {@code removeNodeSpi, keysSpi,
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    62
 * childrenNamesSpi, syncSpi} and {@code flushSpi} methods are specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * to throw {@link BackingStoreException}, and the implementation is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * to throw this checked exception if it is unable to perform the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * The exception propagates outward, causing the corresponding API method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>The remaining SPI methods {@link #putSpi(String,String)}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * #removeSpi(String)} and {@link #childSpi(String)} have more complicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * exception behavior.  They are not specified to throw
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    71
 * {@code BackingStoreException}, as they can generally obey their contracts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * even if the backing store is unavailable.  This is true because they return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * no information and their effects are not required to become permanent until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * a subsequent call to {@link Preferences#flush()} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * {@link Preferences#sync()}. Generally speaking, these SPI methods should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * throw exceptions.  In some implementations, there may be circumstances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * under which these calls cannot even enqueue the requested operation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * later processing.  Even under these circumstances it is generally better to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * simply ignore the invocation and return, rather than throwing an
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
    80
 * exception.  Under these circumstances, however, subsequently invoking
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    81
 * {@code flush()} or {@code sync} would not imply that all previous
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
    82
 * operations had successfully been made permanent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    84
 * <p>There is one circumstance under which {@code putSpi, removeSpi and
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
    85
 * childSpi} <i>should</i> throw an exception: if the caller lacks
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * sufficient privileges on the underlying operating system to perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * requested operation.  This will, for instance, occur on most systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * if a non-privileged user attempts to modify system preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * (The required privileges will vary from implementation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * implementation.  On some implementations, they are the right to modify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * contents of some directory in the file system; on others they are the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * to modify contents of some key in a registry.)  Under any of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * circumstances, it would generally be undesirable to let the program
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * continue executing as if these operations would become permanent at a later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * time.  While implementations are not required to throw an exception under
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * these circumstances, they are encouraged to do so.  A {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * SecurityException} would be appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>Most of the SPI methods require the implementation to read or write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * information at a preferences node.  The implementor should beware of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * fact that another VM may have concurrently deleted this node from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * backing store.  It is the implementation's responsibility to recreate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * node if it has been deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   105
 * <p>Implementation note: In Sun's default {@code Preferences}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * implementations, the user's identity is inherited from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * operating system and does not change for the lifetime of the virtual
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   108
 * machine.  It is recognized that server-side {@code Preferences}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * implementations may have the user identity change from request to request,
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   110
 * implicitly passed to {@code Preferences} methods via the use of a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * static {@link ThreadLocal} instance.  Authors of such implementations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <i>strongly</i> encouraged to determine the user at the time preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * are accessed (for example by the {@link #get(String,String)} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * #put(String,String)} method) rather than permanently associating a user
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   115
 * with each {@code Preferences} instance.  The latter behavior conflicts
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   116
 * with normal {@code Preferences} usage and would lead to great confusion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @see     Preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
public abstract class AbstractPreferences extends Preferences {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   124
     * The code point U+0000, assigned to the null control character, is the
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   125
     * only character encoded in Unicode and ISO/IEC 10646 that is always
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   126
     * invalid in any XML 1.0 and 1.1 document.
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   127
     */
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   128
    static final int CODE_POINT_U0000 = '\u0000';
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   129
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   130
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Our name relative to parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Our absolute path name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private final String absolutePath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Our parent node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    final AbstractPreferences parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Our root node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private final AbstractPreferences root; // Relative to this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   151
     * This field should be {@code true} if this node did not exist in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * backing store prior to the creation of this object.  The field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * is initialized to false, but may be set to true by a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * constructor (and should not be modified thereafter).  This field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * indicates whether a node change event should be fired when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * creation is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    protected boolean newNode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * All known unremoved children of this node.  (This "cache" is consulted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * prior to calling childSpi() or getChild().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   164
    private Map<String, AbstractPreferences> kidCache = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * This field is used to keep track of whether or not this node has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * been removed.  Once it's set to true, it will never be reset to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private boolean removed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Registered preference change listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private PreferenceChangeListener[] prefListeners =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        new PreferenceChangeListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Registered node change listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private NodeChangeListener[] nodeListeners = new NodeChangeListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * An object whose monitor is used to lock this node.  This object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * is used in preference to the node itself to reduce the likelihood of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * intentional or unintentional denial of service due to a locked node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * To avoid deadlock, a node is <i>never</i> locked by a thread that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * holds a lock on a descendant of that node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Creates a preference node with the specified parent and the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * name relative to its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param parent the parent of this preference node, or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *               is the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param name the name of this preference node, relative to its parent,
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   199
     *             or {@code ""} if this is the root.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   200
     * @throws IllegalArgumentException if {@code name} contains a slash
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   201
     *          ({@code '/'}),  or {@code parent} is {@code null} and
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   202
     *          name isn't {@code ""}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected AbstractPreferences(AbstractPreferences parent, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (parent==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (!name.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                throw new IllegalArgumentException("Root name '"+name+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                                   "' must be \"\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            this.absolutePath = "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            root = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (name.indexOf('/') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                throw new IllegalArgumentException("Name '" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                 "' contains '/'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (name.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
              throw new IllegalArgumentException("Illegal name: empty string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            root = parent.root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            absolutePath = (parent==root ? "/" + name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                         : parent.absolutePath() + "/" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   227
     * Implements the {@code put} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * {@link Preferences#put(String,String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <p>This implementation checks that the key and value are legal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * obtains this preference node's lock, checks that the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * has not been removed, invokes {@link #putSpi(String,String)}, and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * there are any preference change listeners, enqueues a notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * event for processing by the event dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param key key with which the specified value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param value value to be associated with the specified key.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   238
     * @throws NullPointerException if key or value is {@code null}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   239
     * @throws IllegalArgumentException if {@code key.length()} exceeds
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   240
     *       {@code MAX_KEY_LENGTH} or if {@code value.length} exceeds
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   241
     *       {@code MAX_VALUE_LENGTH}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   242
     * @throws IllegalArgumentException if either key or value contain
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   243
     *         the null control character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public void put(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (key==null || value==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (key.length() > MAX_KEY_LENGTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new IllegalArgumentException("Key too long: "+key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (value.length() > MAX_VALUE_LENGTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new IllegalArgumentException("Value too long: "+value);
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   254
        if (key.indexOf(CODE_POINT_U0000) != -1)
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   255
            throw new IllegalArgumentException("Key contains code point U+0000");
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   256
        if (value.indexOf(CODE_POINT_U0000) != -1)
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   257
            throw new IllegalArgumentException("Value contains code point U+0000");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            putSpi(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            enqueuePreferenceChangeEvent(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   269
     * Implements the {@code get} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * {@link Preferences#get(String,String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   272
     * <p>This implementation first checks to see if {@code key} is
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   273
     * {@code null} throwing a {@code NullPointerException} if this is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * the case.  Then it obtains this preference node's lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * checks that the node has not been removed, invokes {@link
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   276
     * #getSpi(String)}, and returns the result, unless the {@code getSpi}
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   277
     * invocation returns {@code null} or throws an exception, in which case
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   278
     * this invocation returns {@code def}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param key key whose associated value is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param def the value to be returned in the event that this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   282
     *        preference node has no value associated with {@code key}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   283
     * @return the value associated with {@code key}, or {@code def}
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   284
     *         if no value is associated with {@code key}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *         removed with the {@link #removeNode()} method.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   287
     * @throws NullPointerException if key is {@code null}.  (A
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   288
     *         {@code null} default <i>is</i> permitted.)
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   289
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   290
     *         character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public String get(String key, String def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (key==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new NullPointerException("Null key");
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   295
        if (key.indexOf(CODE_POINT_U0000) != -1)
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   296
            throw new IllegalArgumentException("Key contains code point U+0000");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                result = getSpi(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                // Ignoring exception causes default to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return (result==null ? def : result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   312
     * Implements the {@code remove(String)} method as per the specification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * in {@link Preferences#remove(String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>This implementation obtains this preference node's lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * checks that the node has not been removed, invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * {@link #removeSpi(String)} and if there are any preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * change listeners, enqueues a notification event for processing by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * event dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param key key whose mapping is to be removed from the preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *         removed with the {@link #removeNode()} method.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   324
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   325
     *         character, code point U+0000.
12681
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 9035
diff changeset
   326
     * @throws NullPointerException {@inheritDoc}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void remove(String key) {
12681
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 9035
diff changeset
   329
        Objects.requireNonNull(key, "Specified key cannot be null");
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   330
        if (key.indexOf(CODE_POINT_U0000) != -1)
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   331
            throw new IllegalArgumentException("Key contains code point U+0000");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            removeSpi(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            enqueuePreferenceChangeEvent(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   342
     * Implements the {@code clear} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * {@link Preferences#clear()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>This implementation obtains this preference node's lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * invokes {@link #keys()} to obtain an array of keys, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * iterates over the array invoking {@link #remove(String)} on each key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void clear() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        synchronized(lock) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
   357
            for (String key : keys())
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
   358
                remove(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   363
     * Implements the {@code putInt} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * {@link Preferences#putInt(String,int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   366
     * <p>This implementation translates {@code value} to a string with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * {@link Integer#toString(int)} and invokes {@link #put(String,String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * on the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param key key with which the string form of value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param value value whose string form is to be associated with key.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   372
     * @throws NullPointerException if key is {@code null}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   373
     * @throws IllegalArgumentException if {@code key.length()} exceeds
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   374
     *         {@code MAX_KEY_LENGTH}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   375
     * @throws IllegalArgumentException if key contains
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   376
     *         the null control character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public void putInt(String key, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        put(key, Integer.toString(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   385
     * Implements the {@code getInt} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * {@link Preferences#getInt(String,int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   388
     * <p>This implementation invokes {@link #get(String,String) get(key,
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   389
     * null)}.  If the return value is non-null, the implementation
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   390
     * attempts to translate it to an {@code int} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * {@link Integer#parseInt(String)}.  If the attempt succeeds, the return
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   392
     * value is returned by this method.  Otherwise, {@code def} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param key key whose associated value is to be returned as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param def the value to be returned in the event that this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   396
     *        preference node has no value associated with {@code key}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *        or the associated value cannot be interpreted as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return the int value represented by the string associated with
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   399
     *         {@code key} in this preference node, or {@code def} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *         associated value does not exist or cannot be interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *         an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *         removed with the {@link #removeNode()} method.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   404
     * @throws NullPointerException if {@code key} is {@code null}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   405
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   406
     *         character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public int getInt(String key, int def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        int result = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            String value = get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (value != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                result = Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // Ignoring exception causes specified default to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   422
     * Implements the {@code putLong} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * {@link Preferences#putLong(String,long)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   425
     * <p>This implementation translates {@code value} to a string with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * {@link Long#toString(long)} and invokes {@link #put(String,String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * on the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param key key with which the string form of value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param value value whose string form is to be associated with key.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   431
     * @throws NullPointerException if key is {@code null}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   432
     * @throws IllegalArgumentException if {@code key.length()} exceeds
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   433
     *         {@code MAX_KEY_LENGTH}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   434
     * @throws IllegalArgumentException if key contains
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   435
     *         the null control character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void putLong(String key, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        put(key, Long.toString(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   444
     * Implements the {@code getLong} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * {@link Preferences#getLong(String,long)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   447
     * <p>This implementation invokes {@link #get(String,String) get(key,
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   448
     * null)}.  If the return value is non-null, the implementation
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   449
     * attempts to translate it to a {@code long} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * {@link Long#parseLong(String)}.  If the attempt succeeds, the return
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   451
     * value is returned by this method.  Otherwise, {@code def} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param key key whose associated value is to be returned as a long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param def the value to be returned in the event that this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   455
     *        preference node has no value associated with {@code key}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *        or the associated value cannot be interpreted as a long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return the long value represented by the string associated with
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   458
     *         {@code key} in this preference node, or {@code def} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *         associated value does not exist or cannot be interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *         a long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *         removed with the {@link #removeNode()} method.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   463
     * @throws NullPointerException if {@code key} is {@code null}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   464
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   465
     *         character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public long getLong(String key, long def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        long result = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            String value = get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (value != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                result = Long.parseLong(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // Ignoring exception causes specified default to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   481
     * Implements the {@code putBoolean} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * {@link Preferences#putBoolean(String,boolean)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   484
     * <p>This implementation translates {@code value} to a string with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * {@link String#valueOf(boolean)} and invokes {@link #put(String,String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * on the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param key key with which the string form of value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param value value whose string form is to be associated with key.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   490
     * @throws NullPointerException if key is {@code null}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   491
     * @throws IllegalArgumentException if {@code key.length()} exceeds
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   492
     *         {@code MAX_KEY_LENGTH}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   493
     * @throws IllegalArgumentException if key contains
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   494
     *         the null control character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void putBoolean(String key, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        put(key, String.valueOf(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   503
     * Implements the {@code getBoolean} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * {@link Preferences#getBoolean(String,boolean)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   506
     * <p>This implementation invokes {@link #get(String,String) get(key,
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   507
     * null)}.  If the return value is non-null, it is compared with
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   508
     * {@code "true"} using {@link String#equalsIgnoreCase(String)}.  If the
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   509
     * comparison returns {@code true}, this invocation returns
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   510
     * {@code true}.  Otherwise, the original return value is compared with
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   511
     * {@code "false"}, again using {@link String#equalsIgnoreCase(String)}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   512
     * If the comparison returns {@code true}, this invocation returns
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   513
     * {@code false}.  Otherwise, this invocation returns {@code def}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param key key whose associated value is to be returned as a boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param def the value to be returned in the event that this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   517
     *        preference node has no value associated with {@code key}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *        or the associated value cannot be interpreted as a boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return the boolean value represented by the string associated with
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   520
     *         {@code key} in this preference node, or {@code def} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *         associated value does not exist or cannot be interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *         a boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *         removed with the {@link #removeNode()} method.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   525
     * @throws NullPointerException if {@code key} is {@code null}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   526
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   527
     *         character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public boolean getBoolean(String key, boolean def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        boolean result = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        String value = get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (value.equalsIgnoreCase("true"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            else if (value.equalsIgnoreCase("false"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   543
     * Implements the {@code putFloat} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * {@link Preferences#putFloat(String,float)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   546
     * <p>This implementation translates {@code value} to a string with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * {@link Float#toString(float)} and invokes {@link #put(String,String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * on the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param key key with which the string form of value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param value value whose string form is to be associated with key.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   552
     * @throws NullPointerException if key is {@code null}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   553
     * @throws IllegalArgumentException if {@code key.length()} exceeds
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   554
     *         {@code MAX_KEY_LENGTH}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   555
     * @throws IllegalArgumentException if key contains
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   556
     *         the null control character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void putFloat(String key, float value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        put(key, Float.toString(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   565
     * Implements the {@code getFloat} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * {@link Preferences#getFloat(String,float)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   568
     * <p>This implementation invokes {@link #get(String,String) get(key,
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   569
     * null)}.  If the return value is non-null, the implementation
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   570
     * attempts to translate it to an {@code float} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   572
     * value is returned by this method.  Otherwise, {@code def} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param key key whose associated value is to be returned as a float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param def the value to be returned in the event that this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   576
     *        preference node has no value associated with {@code key}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *        or the associated value cannot be interpreted as a float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @return the float value represented by the string associated with
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   579
     *         {@code key} in this preference node, or {@code def} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *         associated value does not exist or cannot be interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *         a float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *         removed with the {@link #removeNode()} method.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   584
     * @throws NullPointerException if {@code key} is {@code null}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   585
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   586
     *         character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public float getFloat(String key, float def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        float result = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            String value = get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            if (value != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                result = Float.parseFloat(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // Ignoring exception causes specified default to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   602
     * Implements the {@code putDouble} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * {@link Preferences#putDouble(String,double)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   605
     * <p>This implementation translates {@code value} to a string with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * {@link Double#toString(double)} and invokes {@link #put(String,String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * on the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param key key with which the string form of value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param value value whose string form is to be associated with key.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   611
     * @throws NullPointerException if key is {@code null}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   612
     * @throws IllegalArgumentException if {@code key.length()} exceeds
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   613
     *         {@code MAX_KEY_LENGTH}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   614
     * @throws IllegalArgumentException if key contains
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   615
     *         the null control character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public void putDouble(String key, double value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        put(key, Double.toString(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   624
     * Implements the {@code getDouble} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * {@link Preferences#getDouble(String,double)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   627
     * <p>This implementation invokes {@link #get(String,String) get(key,
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   628
     * null)}.  If the return value is non-null, the implementation
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   629
     * attempts to translate it to an {@code double} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * {@link Double#parseDouble(String)}.  If the attempt succeeds, the return
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   631
     * value is returned by this method.  Otherwise, {@code def} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param key key whose associated value is to be returned as a double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param def the value to be returned in the event that this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   635
     *        preference node has no value associated with {@code key}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *        or the associated value cannot be interpreted as a double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return the double value represented by the string associated with
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   638
     *         {@code key} in this preference node, or {@code def} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *         associated value does not exist or cannot be interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *         a double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *         removed with the {@link #removeNode()} method.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   643
     * @throws NullPointerException if {@code key} is {@code null}.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   644
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   645
     *         character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public double getDouble(String key, double def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        double result = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            String value = get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (value != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                result = Double.parseDouble(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            // Ignoring exception causes specified default to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   661
     * Implements the {@code putByteArray} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * {@link Preferences#putByteArray(String,byte[])}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @param key key with which the string form of value is to be associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @param value value whose string form is to be associated with key.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   666
     * @throws NullPointerException if key or value is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @throws IllegalArgumentException if key.length() exceeds MAX_KEY_LENGTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *         or if value.length exceeds MAX_VALUE_LENGTH*3/4.
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   669
     * @throws IllegalArgumentException if key contains
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   670
     *         the null control character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public void putByteArray(String key, byte[] value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        put(key, Base64.byteArrayToBase64(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   679
     * Implements the {@code getByteArray} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * {@link Preferences#getByteArray(String,byte[])}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @param key key whose associated value is to be returned as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param def the value to be returned in the event that this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   684
     *        preference node has no value associated with {@code key}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *        or the associated value cannot be interpreted as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @return the byte array value represented by the string associated with
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   687
     *         {@code key} in this preference node, or {@code def} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *         associated value does not exist or cannot be interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *         a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *         removed with the {@link #removeNode()} method.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   692
     * @throws NullPointerException if {@code key} is {@code null}.  (A
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   693
     *         {@code null} value for {@code def} <i>is</i> permitted.)
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   694
     * @throws IllegalArgumentException if key contains the null control
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29919
diff changeset
   695
     *         character, code point U+0000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public byte[] getByteArray(String key, byte[] def) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        byte[] result = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        String value = get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            if (value != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                result = Base64.base64ToByteArray(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            // Ignoring exception causes specified default to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   712
     * Implements the {@code keys} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * {@link Preferences#keys()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <p>This implementation obtains this preference node's lock, checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * the node has not been removed and invokes {@link #keysSpi()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @return an array of the keys that have an associated value in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *         preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public String[] keys() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return keysSpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   736
     * Implements the {@code children} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * {@link Preferences#childrenNames()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <p>This implementation obtains this preference node's lock, checks that
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   740
     * the node has not been removed, constructs a {@code TreeSet} initialized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * to the names of children already cached (the children in this node's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * "child-cache"), invokes {@link #childrenNamesSpi()}, and adds all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * returned child-names into the set.  The elements of the tree set are
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   744
     * dumped into a {@code String} array using the {@code toArray} method,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * and this array is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @return the names of the children of this preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @see #cachedChildren()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    public String[] childrenNames() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   760
            Set<String> s = new TreeSet<>(kidCache.keySet());
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   761
            for (String kid : childrenNamesSpi())
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   762
                s.add(kid);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   763
            return s.toArray(EMPTY_STRING_ARRAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Returns all known unremoved children of this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @return all known unremoved children of this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    protected final AbstractPreferences[] cachedChildren() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   775
        return kidCache.values().toArray(EMPTY_ABSTRACT_PREFS_ARRAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    private static final AbstractPreferences[] EMPTY_ABSTRACT_PREFS_ARRAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        = new AbstractPreferences[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   782
     * Implements the {@code parent} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * {@link Preferences#parent()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * <p>This implementation obtains this preference node's lock, checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * the node has not been removed and returns the parent value that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * passed to this node's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @return the parent of this preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    public Preferences parent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   803
     * Implements the {@code node} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * {@link Preferences#node(String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <p>This implementation obtains this preference node's lock and checks
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   807
     * that the node has not been removed.  If {@code path} is {@code ""},
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   808
     * this node is returned; if {@code path} is {@code "/"}, this node's
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   809
     * root is returned.  If the first character in {@code path} is
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   810
     * not {@code '/'}, the implementation breaks {@code path} into
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * tokens and recursively traverses the path from this node to the
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   812
     * named node, "consuming" a name and a slash from {@code path} at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * each step of the traversal.  At each step, the current node is locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * and the node's child-cache is checked for the named node.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * not found, the name is checked to make sure its length does not
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   816
     * exceed {@code MAX_NAME_LENGTH}.  Then the {@link #childSpi(String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * method is invoked, and the result stored in this node's child-cache.
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   818
     * If the newly created {@code Preferences} object's {@link #newNode}
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   819
     * field is {@code true} and there are any node change listeners,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * a notification event is enqueued for processing by the event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * <p>When there are no more tokens, the last value found in the
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   824
     * child-cache or returned by {@code childSpi} is returned by this
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   825
     * method.  If during the traversal, two {@code "/"} tokens occur
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   826
     * consecutively, or the final token is {@code "/"} (rather than a name),
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   827
     * an appropriate {@code IllegalArgumentException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   829
     * <p> If the first character of {@code path} is {@code '/'}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * (indicating an absolute path name) this preference node's
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   831
     * lock is dropped prior to breaking {@code path} into tokens, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * this method recursively traverses the path starting from the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * (rather than starting from this node).  The traversal is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * identical to the one described for relative path names.  Dropping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * the lock on this node prior to commencing the traversal at the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * node is essential to avoid the possibility of deadlock, as per the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * {@link #lock locking invariant}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @param path the path name of the preference node to return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @return the specified preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @throws IllegalArgumentException if the path name is invalid (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *         it contains multiple consecutive slash characters, or ends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *         with a slash character and is more than one character long).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public Preferences node(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            if (path.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            if (path.equals("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            if (path.charAt(0) != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                return node(new StringTokenizer(path, "/", true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        // Absolute path.  Note that we've dropped our lock to avoid deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return root.node(new StringTokenizer(path.substring(1), "/", true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * tokenizer contains <name> {'/' <name>}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    private Preferences node(StringTokenizer path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        String token = path.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if (token.equals("/"))  // Check for consecutive slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            throw new IllegalArgumentException("Consecutive slashes in path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        synchronized(lock) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   871
            AbstractPreferences child = kidCache.get(token);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                if (token.length() > MAX_NAME_LENGTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                        "Node name " + token + " too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                child = childSpi(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                if (child.newNode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    enqueueNodeAddedEvent(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                kidCache.put(token, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            if (!path.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            path.nextToken();  // Consume slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            if (!path.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                throw new IllegalArgumentException("Path ends with slash");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return child.node(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   891
     * Implements the {@code nodeExists} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * {@link Preferences#nodeExists(String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * <p>This implementation is very similar to {@link #node(String)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * except that {@link #getChild(String)} is used instead of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * #childSpi(String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @param path the path name of the node whose existence is to be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @return true if the specified node exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @throws IllegalArgumentException if the path name is invalid (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *         it contains multiple consecutive slash characters, or ends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *         with a slash character and is more than one character long).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *         removed with the {@link #removeNode()} method and
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   908
     *         {@code pathname} is not the empty string ({@code ""}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    public boolean nodeExists(String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        throws BackingStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            if (path.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                return !removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            if (path.equals("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            if (path.charAt(0) != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                return nodeExists(new StringTokenizer(path, "/", true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        // Absolute path.  Note that we've dropped our lock to avoid deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        return root.nodeExists(new StringTokenizer(path.substring(1), "/",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                                                   true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * tokenizer contains <name> {'/' <name>}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    private boolean nodeExists(StringTokenizer path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        throws BackingStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        String token = path.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        if (token.equals("/"))  // Check for consecutive slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            throw new IllegalArgumentException("Consecutive slashes in path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        synchronized(lock) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   939
            AbstractPreferences child = kidCache.get(token);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            if (child == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                child = getChild(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            if (child==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            if (!path.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            path.nextToken();  // Consume slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if (!path.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                throw new IllegalArgumentException("Path ends with slash");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            return child.nodeExists(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   955
     * Implements the {@code removeNode()} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * {@link Preferences#removeNode()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <p>This implementation checks to see that this node is the root; if so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * it throws an appropriate exception.  Then, it locks this node's parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * and calls a recursive helper method that traverses the subtree rooted at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * this node.  The recursive method locks the node on which it was called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * checks that it has not already been removed, and then ensures that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * of its children are cached: The {@link #childrenNamesSpi()} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * invoked and each returned child name is checked for containment in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * child-cache.  If a child is not already cached, the {@link
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
   966
     * #childSpi(String)} method is invoked to create a {@code Preferences}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * instance for it, and this instance is put into the child-cache.  Then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * the helper method calls itself recursively on each node contained in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * child-cache.  Next, it invokes {@link #removeNodeSpi()}, marks itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * as removed, and removes itself from its parent's child-cache.  Finally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * if there are any node change listeners, it enqueues a notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * event for processing by the event dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * <p>Note that the helper method is always invoked with all ancestors up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * to the "closest non-removed ancestor" locked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * @throws IllegalStateException if this node (or an ancestor) has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *         been removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @throws UnsupportedOperationException if this method is invoked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *         the root node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    public void removeNode() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        if (this==root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            throw new UnsupportedOperationException("Can't remove the root!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        synchronized(parent.lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            removeNode2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            parent.kidCache.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * Called with locks on all nodes on path from parent of "removal root"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * to this (including the former but excluding the latter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    private void removeNode2() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                throw new IllegalStateException("Node already removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            // Ensure that all children are cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            String[] kidNames = childrenNamesSpi();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1005
            for (String kidName : kidNames)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1006
                if (!kidCache.containsKey(kidName))
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1007
                    kidCache.put(kidName, childSpi(kidName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            // Recursively remove all cached children
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1010
            for (Iterator<AbstractPreferences> i = kidCache.values().iterator();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1011
                 i.hasNext();) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1013
                    i.next().removeNode2();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                } catch (BackingStoreException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            // Now we have no descendants - it's time to die!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            removeNodeSpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            removed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            parent.enqueueNodeRemovedEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1026
     * Implements the {@code name} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * {@link Preferences#name()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * <p>This implementation merely returns the name that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * passed to this node's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @return this preference node's name, relative to its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1039
     * Implements the {@code absolutePath} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * {@link Preferences#absolutePath()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * <p>This implementation merely returns the absolute path name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * was computed at the time that this node was constructed (based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * the name that was passed to this node's constructor, and the names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * that were passed to this node's ancestors' constructors).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @return this preference node's absolute path name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public String absolutePath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return absolutePath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1054
     * Implements the {@code isUserNode} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * {@link Preferences#isUserNode()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * <p>This implementation compares this node's root node (which is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * in a private field) with the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * {@link Preferences#userRoot()}.  If the two object references are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * identical, this method returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1062
     * @return {@code true} if this preference node is in the user
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1063
     *         preference tree, {@code false} if it's in the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *         preference tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    public boolean isUserNode() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1067
        return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1068
            new PrivilegedAction<Boolean>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1069
                public Boolean run() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1070
                    return root == Preferences.userRoot();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1072
            }).booleanValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    public void addPreferenceChangeListener(PreferenceChangeListener pcl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        if (pcl==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            throw new NullPointerException("Change listener is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            // Copy-on-write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            PreferenceChangeListener[] old = prefListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            prefListeners = new PreferenceChangeListener[old.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            System.arraycopy(old, 0, prefListeners, 0, old.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            prefListeners[old.length] = pcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        startEventDispatchThreadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    public void removePreferenceChangeListener(PreferenceChangeListener pcl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            if ((prefListeners == null) || (prefListeners.length == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                throw new IllegalArgumentException("Listener not registered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            // Copy-on-write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            PreferenceChangeListener[] newPl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                new PreferenceChangeListener[prefListeners.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            while (i < newPl.length && prefListeners[i] != pcl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                newPl[i] = prefListeners[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            if (i == newPl.length &&  prefListeners[i] != pcl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                throw new IllegalArgumentException("Listener not registered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            while (i < newPl.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                newPl[i] = prefListeners[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            prefListeners = newPl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    public void addNodeChangeListener(NodeChangeListener ncl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        if (ncl==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            throw new NullPointerException("Change listener is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            // Copy-on-write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if (nodeListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                nodeListeners = new NodeChangeListener[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                nodeListeners[0] = ncl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                NodeChangeListener[] old = nodeListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                nodeListeners = new NodeChangeListener[old.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                System.arraycopy(old, 0, nodeListeners, 0, old.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                nodeListeners[old.length] = ncl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        startEventDispatchThreadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    public void removeNodeChangeListener(NodeChangeListener ncl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                throw new IllegalStateException("Node has been removed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            if ((nodeListeners == null) || (nodeListeners.length == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                throw new IllegalArgumentException("Listener not registered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            // Copy-on-write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            while (i < nodeListeners.length && nodeListeners[i] != ncl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            if (i == nodeListeners.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                throw new IllegalArgumentException("Listener not registered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            NodeChangeListener[] newNl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                new NodeChangeListener[nodeListeners.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            if (i != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                System.arraycopy(nodeListeners, 0, newNl, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if (i != newNl.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                System.arraycopy(nodeListeners, i + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                                 newNl, i, newNl.length - i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            nodeListeners = newNl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    // "SPI" METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * Put the given key-value association into this preference node.  It is
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1162
     * guaranteed that {@code key} and {@code value} are non-null and of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * legal length.  Also, it is guaranteed that this node has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * removed.  (The implementor needn't check for any of these things.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * <p>This method is invoked with the lock on this node held.
18563
35827454c772 8019304: Fix doclint issues in java.util.prefs
darcy
parents: 14342
diff changeset
  1167
     * @param key the key
35827454c772 8019304: Fix doclint issues in java.util.prefs
darcy
parents: 14342
diff changeset
  1168
     * @param value the value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    protected abstract void putSpi(String key, String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * Return the value associated with the specified key at this preference
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1174
     * node, or {@code null} if there is no association for this key, or the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * association cannot be determined at this time.  It is guaranteed that
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1176
     * {@code key} is non-null.  Also, it is guaranteed that this node has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * not been removed.  (The implementor needn't check for either of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * things.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * <p> Generally speaking, this method should not throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * under any circumstances.  If, however, if it does throw an exception,
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1182
     * the exception will be intercepted and treated as a {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * <p>This method is invoked with the lock on this node held.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
18563
35827454c772 8019304: Fix doclint issues in java.util.prefs
darcy
parents: 14342
diff changeset
  1187
     * @param key the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @return the value associated with the specified key at this preference
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1189
     *          node, or {@code null} if there is no association for this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *          key, or the association cannot be determined at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    protected abstract String getSpi(String key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * Remove the association (if any) for the specified key at this
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1196
     * preference node.  It is guaranteed that {@code key} is non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Also, it is guaranteed that this node has not been removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * (The implementor needn't check for either of these things.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * <p>This method is invoked with the lock on this node held.
18563
35827454c772 8019304: Fix doclint issues in java.util.prefs
darcy
parents: 14342
diff changeset
  1201
     * @param key the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    protected abstract void removeSpi(String key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * Removes this preference node, invalidating it and any preferences that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * it contains.  The named child will have no descendants at the time this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * invocation is made (i.e., the {@link Preferences#removeNode()} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * invokes this method repeatedly in a bottom-up fashion, removing each of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * a node's descendants before removing the node itself).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * <p>This method is invoked with the lock held on this node and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * parent (and all ancestors that are being removed as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * result of a single invocation to {@link Preferences#removeNode()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * <p>The removal of a node needn't become persistent until the
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1217
     * {@code flush} method is invoked on this node (or an ancestor).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1219
     * <p>If this node throws a {@code BackingStoreException}, the exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * will propagate out beyond the enclosing {@link #removeNode()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    protected abstract void removeNodeSpi() throws BackingStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * Returns all of the keys that have an associated value in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * preference node.  (The returned array will be of size zero if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * this node has no preferences.)  It is guaranteed that this node has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * been removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * <p>This method is invoked with the lock on this node held.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1237
     * <p>If this node throws a {@code BackingStoreException}, the exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * will propagate out beyond the enclosing {@link #keys()} invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * @return an array of the keys that have an associated value in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *         preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    protected abstract String[] keysSpi() throws BackingStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * Returns the names of the children of this preference node.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * returned array will be of size zero if this node has no children.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * This method need not return the names of any nodes already cached,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * but may do so without harm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * <p>This method is invoked with the lock on this node held.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1256
     * <p>If this node throws a {@code BackingStoreException}, the exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * will propagate out beyond the enclosing {@link #childrenNames()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @return an array containing the names of the children of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     *         preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    protected abstract String[] childrenNamesSpi()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        throws BackingStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1270
     * Returns the named child if it exists, or {@code null} if it does not.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1271
     * It is guaranteed that {@code nodeName} is non-null, non-empty,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * does not contain the slash character ('/'), and is no longer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * {@link #MAX_NAME_LENGTH} characters.  Also, it is guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * that this node has not been removed.  (The implementor needn't check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * for any of these things if he chooses to override this method.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * <p>Finally, it is guaranteed that the named node has not been returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * by a previous invocation of this method or {@link #childSpi} after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * last time that it was removed.  In other words, a cached value will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * always be used in preference to invoking this method.  (The implementor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * needn't maintain his own cache of previously returned children if he
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * chooses to override this method.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * <p>This implementation obtains this preference node's lock, invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * {@link #childrenNames()} to get an array of the names of this node's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * children, and iterates over the array comparing the name of each child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * with the specified node name.  If a child node has the correct name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * the {@link #childSpi(String)} method is invoked and the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * node is returned.  If the iteration completes without finding the
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1290
     * specified name, {@code null} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @param nodeName name of the child to be searched for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @return the named child if it exists, or null if it does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    protected AbstractPreferences getChild(String nodeName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            // assert kidCache.get(nodeName)==null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            String[] kidNames = childrenNames();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1303
            for (String kidName : kidNames)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1304
                if (kidName.equals(nodeName))
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1305
                    return childSpi(kidName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * Returns the named child of this preference node, creating it if it does
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1312
     * not already exist.  It is guaranteed that {@code name} is non-null,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * non-empty, does not contain the slash character ('/'), and is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * than {@link #MAX_NAME_LENGTH} characters.  Also, it is guaranteed that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * this node has not been removed.  (The implementor needn't check for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * of these things.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * <p>Finally, it is guaranteed that the named node has not been returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * by a previous invocation of this method or {@link #getChild(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * after the last time that it was removed.  In other words, a cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * value will always be used in preference to invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * Subclasses need not maintain their own cache of previously returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * <p>The implementer must ensure that the returned node has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * removed.  If a like-named child of this node was previously removed, the
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1327
     * implementer must return a newly constructed {@code AbstractPreferences}
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1328
     * node; once removed, an {@code AbstractPreferences} node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * cannot be "resuscitated."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * <p>If this method causes a node to be created, this node is not
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1332
     * guaranteed to be persistent until the {@code flush} method is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * invoked on this node or one of its ancestors (or descendants).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * <p>This method is invoked with the lock on this node held.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @param name The name of the child node to return, relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     *        this preference node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * @return The named child node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    protected abstract AbstractPreferences childSpi(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * Returns the absolute path name of this preferences node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        return (this.isUserNode() ? "User" : "System") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
               " Preference Node: " + this.absolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1352
     * Implements the {@code sync} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * {@link Preferences#sync()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * <p>This implementation calls a recursive helper method that locks this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * node, invokes syncSpi() on it, unlocks this node, and recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * invokes this method on each "cached child."  A cached child is a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * of this node that has been created in this VM and not subsequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * removed.  In effect, this method does a depth first traversal of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * "cached subtree" rooted at this node, calling syncSpi() on each node in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * the subTree while only that node is locked. Note that syncSpi() is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * invoked top-down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @throws IllegalStateException if this node (or an ancestor) has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     *         removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * @see #flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    public void sync() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        sync2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    private void sync2() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        AbstractPreferences[] cachedKids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            if (removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                throw new IllegalStateException("Node has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            syncSpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            cachedKids = cachedChildren();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1385
        for (AbstractPreferences cachedKid : cachedKids)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1386
            cachedKid.sync2();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * This method is invoked with this node locked.  The contract of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * method is to synchronize any cached preferences stored at this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * with any stored in the backing store.  (It is perfectly possible that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * this node does not exist on the backing store, either because it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * been deleted by another VM, or because it has not yet been created.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * Note that this method should <i>not</i> synchronize the preferences in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * any subnodes of this node.  If the backing store naturally syncs an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * entire subtree at once, the implementer is encouraged to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * sync(), rather than merely overriding this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1400
     * <p>If this node throws a {@code BackingStoreException}, the exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * will propagate out beyond the enclosing {@link #sync()} invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    protected abstract void syncSpi() throws BackingStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1410
     * Implements the {@code flush} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * {@link Preferences#flush()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * <p>This implementation calls a recursive helper method that locks this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * node, invokes flushSpi() on it, unlocks this node, and recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * invokes this method on each "cached child."  A cached child is a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * of this node that has been created in this VM and not subsequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * removed.  In effect, this method does a depth first traversal of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * "cached subtree" rooted at this node, calling flushSpi() on each node in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * the subTree while only that node is locked. Note that flushSpi() is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * invoked top-down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * <p> If this method is invoked on a node that has been removed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * the {@link #removeNode()} method, flushSpi() is invoked on this node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * but not on others.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * @see #flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    public void flush() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        flush2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    private void flush2() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        AbstractPreferences[] cachedKids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            flushSpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            if(removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            cachedKids = cachedChildren();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1445
        for (AbstractPreferences cachedKid : cachedKids)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1446
            cachedKid.flush2();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * This method is invoked with this node locked.  The contract of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * method is to force any cached changes in the contents of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * preference node to the backing store, guaranteeing their persistence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * (It is perfectly possible that this node does not exist on the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * store, either because it has been deleted by another VM, or because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * has not yet been created.)  Note that this method should <i>not</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * flush the preferences in any subnodes of this node.  If the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * store naturally flushes an entire subtree at once, the implementer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * encouraged to override flush(), rather than merely overriding this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1461
     * <p>If this node throws a {@code BackingStoreException}, the exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * will propagate out beyond the enclosing {@link #flush()} invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * @throws BackingStoreException if this operation cannot be completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     *         due to a failure in the backing store, or inability to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     *         communicate with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    protected abstract void flushSpi() throws BackingStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1471
     * Returns {@code true} iff this node (or an ancestor) has been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * removed with the {@link #removeNode()} method.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * locks this node prior to returning the contents of the private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * field used to track this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1476
     * @return {@code true} iff this node (or an ancestor) has been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *       removed with the {@link #removeNode()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    protected boolean isRemoved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            return removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * Queue of pending notification events.  When a preference or node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * change event for which there are one or more listeners occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * it is placed on this queue and the queue is notified.  A background
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * thread waits on this queue and delivers the events.  This decouples
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * event delivery from preference activity, greatly simplifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * locking and reducing opportunity for deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     */
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1493
    private static final List<EventObject> eventQueue = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * These two classes are used to distinguish NodeChangeEvents on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * eventQueue so the event dispatch thread knows whether to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * childAdded or childRemoved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    private class NodeAddedEvent extends NodeChangeEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        private static final long serialVersionUID = -6743557530157328528L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        NodeAddedEvent(Preferences parent, Preferences child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            super(parent, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    private class NodeRemovedEvent extends NodeChangeEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        private static final long serialVersionUID = 8735497392918824837L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        NodeRemovedEvent(Preferences parent, Preferences child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            super(parent, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * A single background thread ("the event notification thread") monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * the event queue and delivers events that are placed on the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     */
35311
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 32037
diff changeset
  1517
    private static class EventDispatchThread extends Thread {
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 32037
diff changeset
  1518
        private EventDispatchThread() {
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 32037
diff changeset
  1519
            super(null, null, "Event Dispatch Thread", 0, false);
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 32037
diff changeset
  1520
        }
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 32037
diff changeset
  1521
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                // Wait on eventQueue till an event is present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                EventObject event = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                synchronized(eventQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                        while (eventQueue.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                            eventQueue.wait();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1530
                        event = eventQueue.remove(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                        // XXX Log "Event dispatch thread interrupted. Exiting"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                // Now we have event & hold no locks; deliver evt to listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                AbstractPreferences src=(AbstractPreferences)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                if (event instanceof PreferenceChangeEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    PreferenceChangeEvent pce = (PreferenceChangeEvent)event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    PreferenceChangeListener[] listeners = src.prefListeners();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1542
                    for (PreferenceChangeListener listener : listeners)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1543
                        listener.preferenceChange(pce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    NodeChangeEvent nce = (NodeChangeEvent)event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    NodeChangeListener[] listeners = src.nodeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    if (nce instanceof NodeAddedEvent) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1548
                        for (NodeChangeListener listener : listeners)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1549
                            listener.childAdded(nce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                        // assert nce instanceof NodeRemovedEvent;
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1552
                        for (NodeChangeListener listener : listeners)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18563
diff changeset
  1553
                            listener.childRemoved(nce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    private static Thread eventDispatchThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * This method starts the event dispatch thread the first time it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * is called.  The event dispatch thread will be started only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * if someone registers a listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    private static synchronized void startEventDispatchThreadIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        if (eventDispatchThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            // XXX Log "Starting event dispatch thread"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            eventDispatchThread = new EventDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            eventDispatchThread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            eventDispatchThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * Return this node's preference/node change listeners.  Even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * we're using a copy-on-write lists, we use synchronized accessors to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * ensure information transmission from the writing thread to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * reading thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    PreferenceChangeListener[] prefListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            return prefListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    NodeChangeListener[] nodeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            return nodeListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * Enqueue a preference change event for delivery to registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * preference change listeners unless there are no registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * listeners.  Invoked with this.lock held.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    private void enqueuePreferenceChangeEvent(String key, String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        if (prefListeners.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            synchronized(eventQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                eventQueue.add(new PreferenceChangeEvent(this, key, newValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                eventQueue.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * Enqueue a "node added" event for delivery to registered node change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * listeners unless there are no registered listeners.  Invoked with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * this.lock held.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    private void enqueueNodeAddedEvent(Preferences child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        if (nodeListeners.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            synchronized(eventQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                eventQueue.add(new NodeAddedEvent(this, child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                eventQueue.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * Enqueue a "node removed" event for delivery to registered node change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * listeners unless there are no registered listeners.  Invoked with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * this.lock held.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    private void enqueueNodeRemovedEvent(Preferences child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        if (nodeListeners.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            synchronized(eventQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                eventQueue.add(new NodeRemovedEvent(this, child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                eventQueue.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1636
     * Implements the {@code exportNode} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * {@link Preferences#exportNode(OutputStream)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * @param os the output stream on which to emit the XML document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * @throws IOException if writing to the specified output stream
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1641
     *         results in an {@code IOException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * @throws BackingStoreException if preference data cannot be read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     *         backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    public void exportNode(OutputStream os)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        throws IOException, BackingStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        XmlSupport.export(os, this, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1652
     * Implements the {@code exportSubtree} method as per the specification in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * {@link Preferences#exportSubtree(OutputStream)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * @param os the output stream on which to emit the XML document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * @throws IOException if writing to the specified output stream
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 30043
diff changeset
  1657
     *         results in an {@code IOException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * @throws BackingStoreException if preference data cannot be read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *         backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    public void exportSubtree(OutputStream os)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        throws IOException, BackingStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        XmlSupport.export(os, this, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
}