20 package net.sf.gridarta.preferences;
22 import java.util.Arrays;
23 import java.util.prefs.AbstractPreferences;
24 import java.util.prefs.BackingStoreException;
25 import java.util.prefs.Preferences;
26 import org.apache.log4j.Category;
27 import org.apache.log4j.Logger;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
71 protected FilePreferences(@Nullable
final AbstractPreferences parent, @NotNull
final String name, @NotNull
final NodeType nodeType, @NotNull
final Storage storage) {
76 final StringBuilder sb =
new StringBuilder(name);
77 for (Preferences p = parent; p != null; p = p.parent()) {
78 sb.insert(0,
'_').insert(0, p.name());
80 sb.insert(0, nodeType.getPrefix());
81 fullName = sb.toString();
82 storage.newNode(fullName);
98 if (LOG.isDebugEnabled()) {
99 LOG.debug(
"childrenNamesSpi(" + fullName +
")=" + Arrays.toString(result));
105 protected AbstractPreferences
childSpi(@NotNull
final String name) {
110 protected void flushSpi() throws BackingStoreException {
111 throw new InternalError();
118 public void flush() throws BackingStoreException {
119 if (LOG.isDebugEnabled()) {
120 LOG.debug(
"flush(" + fullName +
")");
131 public String
get(@NotNull
final String key, @Nullable
final String def) {
134 throw new IllegalArgumentException(
"null key");
138 synchronized (lock) {
140 if (LOG.isDebugEnabled()) {
141 LOG.debug(
"get(" + fullName +
", " + key +
") called for removed node");
144 throw new IllegalStateException(
"removed node");
150 }
catch (
final Exception ignored) {
162 if (LOG.isDebugEnabled()) {
163 LOG.debug(
"get(" + fullName +
", " + key +
", " + def +
")=" + value);
169 public boolean getBoolean(@NotNull
final String key,
final boolean def) {
170 final boolean result = super.getBoolean(key, def);
171 putSpi(key, Boolean.toString(result));
176 public double getDouble(@NotNull
final String key,
final double def) {
177 final double result = super.getDouble(key, def);
178 putSpi(key, Double.toString(result));
183 public float getFloat(@NotNull
final String key,
final float def) {
184 final float result = super.getFloat(key, def);
185 putSpi(key, Float.toString(result));
190 public int getInt(@NotNull
final String key,
final int def) {
191 final int result = super.getInt(key, def);
192 putSpi(key, Integer.toString(result));
197 public long getLong(@NotNull
final String key,
final long def) {
198 final long result = super.getLong(key, def);
199 putSpi(key, Long.toString(result));
205 protected String
getSpi(@NotNull
final String key) {
206 if (LOG.isDebugEnabled()) {
207 LOG.debug(
"getSpi(" + fullName +
", key=" + key +
")");
210 return storage.
getValue(fullName, key);
215 protected String[]
keysSpi() throws BackingStoreException {
216 if (LOG.isDebugEnabled()) {
217 LOG.debug(
"keysSpi(" + fullName +
")");
220 return storage.
getKeys(fullName);
224 protected void putSpi(@NotNull
final String key, @NotNull
final String value) {
225 if (LOG.isDebugEnabled()) {
226 LOG.debug(
"putSpi(" + fullName +
", key=" + key +
", value=" + value +
")");
229 storage.
putValue(fullName, key, value);
234 if (LOG.isDebugEnabled()) {
235 LOG.debug(
"removeNodeSpi(" + fullName +
")");
243 if (LOG.isDebugEnabled()) {
244 LOG.debug(
"removeSpi(" + fullName +
", key=" + key +
")");
251 protected void syncSpi() throws BackingStoreException {
252 throw new InternalError();
259 public void sync() throws BackingStoreException {
260 if (LOG.isDebugEnabled()) {
261 LOG.debug(
"sync(" + fullName +
")");
An FilePreferences that represents an internal (non-root) node.
double getDouble(@NotNull final String key, final double def)
Implements a Preferences that stores all values in a Storage instance.
String [] getKeys(@NotNull final String path)
Return all of the keys that have an associated value in a node.
float getFloat(@NotNull final String key, final float def)
AbstractPreferences childSpi(@NotNull final String name)
int getInt(@NotNull final String key, final int def)
void sync()
RefusedBequest
String getValue(@NotNull final String path, @NotNull final String key)
Return the value associated with the specified key at a node, or.
void removeValue(@NotNull final String path, @NotNull final String key)
Remove the association (if any) for the specified key at a node.
Maintains a set of preference values.
final Storage storage
The Storage instance used for loading/saving values.
void putValue(@NotNull final String path, @NotNull final String key, @NotNull final String value)
Put the given key-value association into a node.
long getLong(@NotNull final String key, final long def)
void removeSpi(@NotNull final String key)
String [] childrenNames(@NotNull final String path)
Return the names of the children of a node.
void putSpi(@NotNull final String key, @NotNull final String value)
String getSpi(@NotNull final String key)
The type of a FilePreferences node.
final String fullName
The full path name of this node.
final NodeType nodeType
The node type of this node.
void removeNode(@NotNull final String path)
Remove a preference node including all preferences that it contains.
String [] childrenNamesSpi()
void sync(final boolean sync)
Save changes to the underlying file.
Storage getStorage()
Return the storage instance used for loading/saving values.
boolean getBoolean(@NotNull final String key, final boolean def)
static final Category LOG
The Logger for printing log messages.
FilePreferences(@Nullable final AbstractPreferences parent, @NotNull final String name, @NotNull final NodeType nodeType, @NotNull final Storage storage)
Create a new instance.
void flush()
RefusedBequest