Last change
on this file since 13908 was 12981, checked in by bastiK, 7 years ago |
update field name to be more clear
|
-
Property svn:eol-style
set to
native
|
File size:
1.3 KB
|
Line | |
---|
1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.data.preferences.AbstractProperty.ValueChangeListener;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * This is a special wrapper of {@link AbstractProperty}.
|
---|
8 | * The current preference value is cached. The value is invalidated if the preference was changed.
|
---|
9 | * @author Michael Zangl
|
---|
10 | *
|
---|
11 | * @param <T> property type
|
---|
12 | * @since 10824
|
---|
13 | */
|
---|
14 | public class CachingProperty<T> extends AbstractProperty<T> implements ValueChangeListener<T> {
|
---|
15 |
|
---|
16 | private T cache;
|
---|
17 | private boolean cacheValid;
|
---|
18 | private final AbstractProperty<T> toCache;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Create a new caching property.
|
---|
22 | * @param toCache The property to cache.
|
---|
23 | */
|
---|
24 | CachingProperty(AbstractProperty<T> toCache) {
|
---|
25 | super(toCache.getKey(), toCache.getDefaultValue());
|
---|
26 | this.toCache = toCache;
|
---|
27 | addWeakListener(this);
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public synchronized T get() {
|
---|
32 | if (!cacheValid) {
|
---|
33 | cache = toCache.get();
|
---|
34 | cacheValid = true;
|
---|
35 | }
|
---|
36 | return cache;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public boolean put(T value) {
|
---|
41 | return toCache.put(value);
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public synchronized void valueChanged(ValueChangeEvent<? extends T> e) {
|
---|
46 | cacheValid = false;
|
---|
47 | }
|
---|
48 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.