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