-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathPrefs.java
More file actions
326 lines (265 loc) · 8.88 KB
/
Prefs.java
File metadata and controls
326 lines (265 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2026 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.util;
import java.util.List;
import java.util.Map;
import java.util.prefs.Preferences;
import org.scijava.prefs.DefaultPrefService;
import org.scijava.prefs.PrefService;
/**
* Simple utility class that stores and retrieves user preferences.
* <p>
* Some of this code was adapted from the <a href=
* "http://www.java2s.com/Code/Java/Development-Class/Utilityclassforpreferences.htm"
* >PrefsUtil class by Robin Sharp of Javelin Software.</a>.
* </p>
*
* @author Curtis Rueden
* @author Barry DeZonia
* @author Grant Harris
* @deprecated See {@link PrefService}
*/
@Deprecated
public final class Prefs {
private static PrefService prefService;
private static PrefService prefServiceNoContext;
private static double servicePriority = Double.MIN_VALUE;
private Prefs() {
// prevent instantiation of utility class
}
// -- Global preferences --
public static String get(final String name) {
return service().get(name);
}
public static String get(final String name, final String defaultValue) {
return service().get(name, defaultValue);
}
public static boolean
getBoolean(final String name, final boolean defaultValue)
{
return service().getBoolean(name, defaultValue);
}
public static double getDouble(final String name, final double defaultValue) {
return service().getDouble(name, defaultValue);
}
public static float getFloat(final String name, final float defaultValue) {
return service().getFloat(name, defaultValue);
}
public static int getInt(final String name, final int defaultValue) {
return service().getInt(name, defaultValue);
}
public static long getLong(final String name, final long defaultValue) {
return service().getLong(name, defaultValue);
}
public static void put(final String name, final String value) {
service().put(name, value);
}
public static void put(final String name, final boolean value) {
service().put(name, value);
}
public static void put(final String name, final double value) {
service().put(name, value);
}
public static void put(final String name, final float value) {
service().put(name, value);
}
public static void put(final String name, final int value) {
service().put(name, value);
}
public static void put(final String name, final long value) {
service().put(name, value);
}
// -- Class-specific preferences --
public static String get(final Class<?> c, final String name) {
return service().get(c, name);
}
public static String get(final Class<?> c, final String name,
final String defaultValue)
{
return service().get(c, name, defaultValue);
}
public static boolean getBoolean(final Class<?> c, final String name,
final boolean defaultValue)
{
return service().getBoolean(c, name, defaultValue);
}
public static double getDouble(final Class<?> c, final String name,
final double defaultValue)
{
return service().getDouble(c, name, defaultValue);
}
public static float getFloat(final Class<?> c, final String name,
final float defaultValue)
{
return service().getFloat(c, name, defaultValue);
}
public static int getInt(final Class<?> c, final String name,
final int defaultValue)
{
return service().getInt(c, name, defaultValue);
}
public static long getLong(final Class<?> c, final String name,
final long defaultValue)
{
return service().getLong(c, name, defaultValue);
}
public static void
put(final Class<?> c, final String name, final String value)
{
service().put(c, name, value);
}
public static void put(final Class<?> c, final String name,
final boolean value)
{
service().put(c, name, value);
}
public static void
put(final Class<?> c, final String name, final double value)
{
service().put(c, name, value);
}
public static void
put(final Class<?> c, final String name, final float value)
{
service().put(c, name, value);
}
public static void put(final Class<?> c, final String name, final int value) {
service().put(c, name, value);
}
public static void put(final Class<?> c, final String name, final long value)
{
service().put(c, name, value);
}
public static void clear(final Class<?> c) {
service().clear(c);
}
// -- Other/unsorted --
// TODO - Evaluate which of these methods are really needed, and which are
// duplicate of similar functionality above.
/** Clears everything. */
public static void clearAll() {
service().clearAll();
}
/** Clears the node. */
public static void clear(final String key) {
service().clear(key);
}
public static void clear(final Preferences preferences, final String key) {
service().clear(preferences.absolutePath(), key);
}
/** Removes the node. */
public static void remove(final Preferences preferences, final String key) {
service().remove(preferences.absolutePath(), key);
}
/** Puts a map into the preferences. */
public static void putMap(final Map<String, String> map, final String key) {
service().putMap(map, key);
}
public static void putMap(final Preferences preferences,
final Map<String, String> map, final String key)
{
service().putMap(preferences.absolutePath(), map, key);
}
/** Puts a map into the preferences. */
public static void putMap(final Preferences preferences,
final Map<String, String> map)
{
service().putMap(preferences.absolutePath(), map);
}
/** Gets a Map from the preferences. */
public static Map<String, String> getMap(final String key) {
return service().getMap(key);
}
public static Map<String, String> getMap(final Preferences preferences,
final String key)
{
return service().getMap(preferences.absolutePath(), key);
}
/** Gets a Map from the preferences. */
public static Map<String, String> getMap(final Preferences preferences) {
return service().getMap(preferences.absolutePath());
}
/** Puts a list into the preferences. */
public static void putList(final List<String> list, final String key) {
service().putList(list, key);
}
public static void putList(final Preferences preferences,
final List<String> list, final String key)
{
service().putList(preferences.absolutePath(), list, key);
}
/** Puts a list into the preferences. */
public static void putList(final Preferences preferences,
final List<String> list)
{
service().putList(preferences.absolutePath(), list);
}
/** Gets a List from the preferences. */
public static List<String> getList(final String key) {
return service().getList(key);
}
public static List<String> getList(final Preferences preferences,
final String key)
{
return service().getList(preferences.absolutePath(), key);
}
/**
* Gets a List from the preferences. Returns an empty list if nothing in
* prefs.
*/
public static List<String> getList(final Preferences preferences) {
return service().getList(preferences.absolutePath());
}
// -- PrefService setter --
/**
* Sets the {@link PrefService}
*/
public static void setDelegateService(final PrefService prefService,
final double priority)
{
if (Double.compare(priority, Prefs.servicePriority) > 0) {
Prefs.prefService = prefService;
Prefs.servicePriority = priority;
}
}
// -- Helper methods --
/**
* Gets the delegate {@link PrefService} to use for preference operations. If
* this service has not been explicitly set, then a {@link DefaultPrefService}
* will be used.
*
* @return The current {@link PrefService} to use for delegation.
*/
private static PrefService service() {
if (prefService != null) return prefService;
if (prefServiceNoContext == null) prefServiceNoContext =
new DefaultPrefService();
return prefServiceNoContext;
}
}