Gridarta Editor
FilePreferences.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.preferences;
21 
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;
30 
36 public abstract class FilePreferences extends AbstractPreferences {
37 
41  @NotNull
42  private static final Category LOG = Logger.getLogger(FilePreferences.class);
43 
47  @NotNull
48  private final NodeType nodeType;
49 
54  @NotNull
55  private final String fullName;
56 
60  @NotNull
61  private final Storage storage;
62 
71  protected FilePreferences(@Nullable final AbstractPreferences parent, @NotNull final String name, @NotNull final NodeType nodeType, @NotNull final Storage storage) {
72  super(parent, name);
73  this.nodeType = nodeType;
74  this.storage = storage;
75 
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());
79  }
80  sb.insert(0, nodeType.getPrefix());
81  fullName = sb.toString();
83  }
84 
89  @NotNull
90  protected Storage getStorage() {
91  return storage;
92  }
93 
94  @NotNull
95  @Override
96  protected String @NotNull [] childrenNamesSpi() {
97  final String[] result = storage.childrenNames(fullName);
98  if (LOG.isDebugEnabled()) {
99  LOG.debug("childrenNamesSpi(" + fullName + ")=" + Arrays.toString(result));
100  }
101  return result;
102  }
103 
104  @Override
105  protected AbstractPreferences childSpi(@NotNull final String name) {
106  return new FilePreferencesNode(this, name, nodeType);
107  }
108 
109  @Override
110  protected void flushSpi() {
111  throw new InternalError();
112  }
113 
117  @Override
118  public void flush() throws BackingStoreException {
119  if (LOG.isDebugEnabled()) {
120  LOG.debug("flush(" + fullName + ")");
121  }
122 
123  storage.sync(false);
124  }
125 
129  @Nullable
130  @Override
131  public String get(@NotNull final String key, @Nullable final String def) {
132  //noinspection ConstantConditions
133  if (key == null) {
134  throw new IllegalArgumentException("null key");
135  }
136 
137  String value;
138  synchronized (lock) {
139  if (isRemoved()) {
140  if (LOG.isDebugEnabled()) {
141  LOG.debug("get(" + fullName + ", " + key + ") called for removed node");
142  }
143 
144  throw new IllegalStateException("removed node");
145  }
146 
147  //noinspection CatchGenericClass
148  try {
149  value = getSpi(key);
150  } catch (final Exception ignored) {
151  //noinspection AssignmentToNull
152  value = null;
153  }
154  if (value == null) {
155  if (def != null) {
156  putSpi(key, def);
157  }
158 
159  value = def;
160  }
161  }
162  if (LOG.isDebugEnabled()) {
163  LOG.debug("get(" + fullName + ", " + key + ", " + def + ")=" + value);
164  }
165  return value;
166  }
167 
168  @Override
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));
172  return result;
173  }
174 
175  @Override
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));
179  return result;
180  }
181 
182  @Override
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));
186  return result;
187  }
188 
189  @Override
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));
193  return result;
194  }
195 
196  @Override
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));
200  return result;
201  }
202 
203  @Nullable
204  @Override
205  protected String getSpi(@NotNull final String key) {
206  if (LOG.isDebugEnabled()) {
207  LOG.debug("getSpi(" + fullName + ", key=" + key + ")");
208  }
209 
210  return storage.getValue(fullName, key);
211  }
212 
213  @NotNull
214  @Override
215  protected String @NotNull [] keysSpi() {
216  if (LOG.isDebugEnabled()) {
217  LOG.debug("keysSpi(" + fullName + ")");
218  }
219 
220  return storage.getKeys(fullName);
221  }
222 
223  @Override
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 + ")");
227  }
228 
229  storage.putValue(fullName, key, value);
230  }
231 
232  @Override
233  protected void removeNodeSpi() {
234  if (LOG.isDebugEnabled()) {
235  LOG.debug("removeNodeSpi(" + fullName + ")");
236  }
237 
239  }
240 
241  @Override
242  protected void removeSpi(@NotNull final String key) {
243  if (LOG.isDebugEnabled()) {
244  LOG.debug("removeSpi(" + fullName + ", key=" + key + ")");
245  }
246 
248  }
249 
250  @Override
251  protected void syncSpi() {
252  throw new InternalError();
253  }
254 
258  @Override
259  public void sync() throws BackingStoreException {
260  if (LOG.isDebugEnabled()) {
261  LOG.debug("sync(" + fullName + ")");
262  }
263 
264  storage.sync(true);
265  }
266 
267 }
net.sf.gridarta.preferences.FilePreferences.getLong
long getLong(@NotNull final String key, final long def)
Definition: FilePreferences.java:197
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.preferences.FilePreferences.FilePreferences
FilePreferences(@Nullable final AbstractPreferences parent, @NotNull final String name, @NotNull final NodeType nodeType, @NotNull final Storage storage)
Creates a new instance.
Definition: FilePreferences.java:71
net.sf.gridarta.preferences.FilePreferences.storage
final Storage storage
The Storage instance used for loading/saving values.
Definition: FilePreferences.java:61
net.sf.gridarta.preferences.FilePreferences.getStorage
Storage getStorage()
Returns the storage instance used for loading/saving values.
Definition: FilePreferences.java:90
net.sf.gridarta.preferences.FilePreferences.getBoolean
boolean getBoolean(@NotNull final String key, final boolean def)
Definition: FilePreferences.java:169
net.sf.gridarta.preferences.FilePreferences.flush
void flush()
@noinspection RefusedBequest
Definition: FilePreferences.java:118
net.sf.gridarta.preferences.Storage.removeValue
void removeValue(@NotNull final String path, @NotNull final String key)
Removes the association (if any) for the specified key at a node.
Definition: Storage.java:221
net.sf.gridarta.preferences.Storage.newNode
void newNode(@NotNull final String path)
Makes sure a node exists.
Definition: Storage.java:115
net.sf.gridarta.preferences.Storage.getValue
String getValue(@NotNull final String path, @NotNull final String key)
Returns the value associated with the specified key at a node, or.
Definition: Storage.java:155
net.sf.gridarta.preferences.FilePreferences.childrenNamesSpi
String[] childrenNamesSpi()
Definition: FilePreferences.java:96
net.sf.gridarta.preferences.FilePreferences.getDouble
double getDouble(@NotNull final String key, final double def)
Definition: FilePreferences.java:176
net.sf.gridarta.preferences.FilePreferences
Implements a Preferences that stores all values in a Storage instance.
Definition: FilePreferences.java:36
net.sf.gridarta.preferences.FilePreferences.getSpi
String getSpi(@NotNull final String key)
Definition: FilePreferences.java:205
net.sf.gridarta.preferences.Storage.putValue
void putValue(@NotNull final String path, @NotNull final String key, @NotNull final String value)
Puts the given key-value association into a node.
Definition: Storage.java:189
net.sf.gridarta.preferences.FilePreferences.LOG
static final Category LOG
The Logger for printing log messages.
Definition: FilePreferences.java:42
net.sf.gridarta.preferences.FilePreferences.nodeType
final NodeType nodeType
The node type of this node.
Definition: FilePreferences.java:48
net.sf.gridarta.preferences.FilePreferences.syncSpi
void syncSpi()
Definition: FilePreferences.java:251
net.sf.gridarta.preferences.FilePreferences.fullName
final String fullName
The full path name of this node.
Definition: FilePreferences.java:55
net.sf.gridarta.preferences.FilePreferences.getFloat
float getFloat(@NotNull final String key, final float def)
Definition: FilePreferences.java:183
net.sf.gridarta.preferences.FilePreferences.putSpi
void putSpi(@NotNull final String key, @NotNull final String value)
Definition: FilePreferences.java:224
net.sf.gridarta.preferences.FilePreferences.getInt
int getInt(@NotNull final String key, final int def)
Definition: FilePreferences.java:190
net.sf.gridarta.preferences.Storage.childrenNames
String[] childrenNames(@NotNull final String path)
Returns the names of the children of a node.
Definition: Storage.java:132
net.sf.gridarta.preferences.FilePreferences.removeNodeSpi
void removeNodeSpi()
Definition: FilePreferences.java:233
net.sf.gridarta.preferences.FilePreferences.removeSpi
void removeSpi(@NotNull final String key)
Definition: FilePreferences.java:242
net.sf.gridarta.preferences.Storage.sync
void sync(final boolean sync)
Saves changes to the underlying file.
Definition: Storage.java:239
net.sf.gridarta.preferences.FilePreferences.keysSpi
String[] keysSpi()
Definition: FilePreferences.java:215
net.sf.gridarta.preferences.Storage.removeNode
void removeNode(@NotNull final String path)
Removes a preference node including all preferences that it contains.
Definition: Storage.java:206
net.sf.gridarta.preferences.FilePreferencesNode
An FilePreferences that represents an internal (non-root) node.
Definition: FilePreferencesNode.java:28
net.sf.gridarta.preferences.NodeType
The type of a FilePreferences node.
Definition: NodeType.java:28
net.sf.gridarta.preferences.Storage
Maintains a set of preference values.
Definition: Storage.java:50
net.sf.gridarta.preferences.FilePreferences.childSpi
AbstractPreferences childSpi(@NotNull final String name)
Definition: FilePreferences.java:105
net.sf.gridarta.preferences.Storage.getKeys
String[] getKeys(@NotNull final String path)
Returns all of the keys that have an associated value in a node.
Definition: Storage.java:172
net.sf.gridarta.preferences.FilePreferences.sync
void sync()
@noinspection RefusedBequest
Definition: FilePreferences.java:259
net.sf.gridarta.preferences.FilePreferences.flushSpi
void flushSpi()
Definition: FilePreferences.java:110
net.sf.gridarta.preferences.NodeType.getPrefix
String getPrefix()
Returns the prefix for building key names.
Definition: NodeType.java:59