source: josm/trunk/src/org/openstreetmap/josm/data/osm/ChangesetCache.java@ 7392

Last change on this file since 7392 was 7079, checked in by Don-vip, 10 years ago

fix some unit tests

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashMap;
7import java.util.HashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11import java.util.concurrent.CopyOnWriteArrayList;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
15import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
16import org.openstreetmap.josm.gui.util.GuiHelper;
17
18/**
19 * ChangesetCache is global in-memory cache for changesets downloaded from
20 * an OSM API server. The unique instance is available as singleton, see
21 * {@link #getInstance()}.
22 *
23 * Clients interested in cache updates can register for {@link ChangesetCacheEvent}s
24 * using {@link #addChangesetCacheListener(ChangesetCacheListener)}. They can use
25 * {@link #removeChangesetCacheListener(ChangesetCacheListener)} to unregister as
26 * cache event listener.
27 *
28 * The cache itself listens to {@link java.util.prefs.PreferenceChangeEvent}s. It
29 * clears itself if the OSM API URL is changed in the preferences.
30 *
31 * {@link ChangesetCacheEvent}s are delivered on the EDT.
32 *
33 */
34public final class ChangesetCache implements PreferenceChangedListener{
35 /** the unique instance */
36 private static final ChangesetCache instance = new ChangesetCache();
37
38 /**
39 * Replies the unique instance of the cache
40 *
41 * @return the unique instance of the cache
42 */
43 public static ChangesetCache getInstance() {
44 return instance;
45 }
46
47 /** the cached changesets */
48 private final Map<Integer, Changeset> cache = new HashMap<>();
49
50 private final CopyOnWriteArrayList<ChangesetCacheListener> listeners =
51 new CopyOnWriteArrayList<>();
52
53 private ChangesetCache() {
54 Main.pref.addPreferenceChangeListener(this);
55 }
56
57 public void addChangesetCacheListener(ChangesetCacheListener listener) {
58 if (listener != null) {
59 listeners.addIfAbsent(listener);
60 }
61 }
62
63 public void removeChangesetCacheListener(ChangesetCacheListener listener) {
64 if (listener != null) {
65 listeners.remove(listener);
66 }
67 }
68
69 protected void fireChangesetCacheEvent(final ChangesetCacheEvent e) {
70 GuiHelper.runInEDT(new Runnable() {
71 @Override public void run() {
72 for(ChangesetCacheListener l: listeners) {
73 l.changesetCacheUpdated(e);
74 }
75 }
76 });
77 }
78
79 protected void update(Changeset cs, DefaultChangesetCacheEvent e) {
80 if (cs == null) return;
81 if (cs.isNew()) return;
82 Changeset inCache = cache.get(cs.getId());
83 if (inCache != null) {
84 inCache.mergeFrom(cs);
85 e.rememberUpdatedChangeset(inCache);
86 } else {
87 e.rememberAddedChangeset(cs);
88 cache.put(cs.getId(), cs);
89 }
90 }
91
92 public void update(Changeset cs) {
93 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
94 update(cs, e);
95 fireChangesetCacheEvent(e);
96 }
97
98 public void update(Collection<Changeset> changesets) {
99 if (changesets == null || changesets.isEmpty()) return;
100 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
101 for (Changeset cs: changesets) {
102 update(cs, e);
103 }
104 fireChangesetCacheEvent(e);
105 }
106
107 public boolean contains(int id) {
108 if (id <=0) return false;
109 return cache.get(id) != null;
110 }
111
112 public boolean contains(Changeset cs) {
113 if (cs == null) return false;
114 if (cs.isNew()) return false;
115 return contains(cs.getId());
116 }
117
118 public Changeset get(int id) {
119 return cache.get(id);
120 }
121
122 public Set<Changeset> getChangesets() {
123 return new HashSet<>(cache.values());
124 }
125
126 protected void remove(int id, DefaultChangesetCacheEvent e) {
127 if (id <= 0) return;
128 Changeset cs = cache.get(id);
129 if (cs == null) return;
130 cache.remove(id);
131 e.rememberRemovedChangeset(cs);
132 }
133
134 public void remove(int id) {
135 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
136 remove(id, e);
137 if (! e.isEmpty()) {
138 fireChangesetCacheEvent(e);
139 }
140 }
141
142 public void remove(Changeset cs) {
143 if (cs == null) return;
144 if (cs.isNew()) return;
145 remove(cs.getId());
146 }
147
148 /**
149 * Removes the changesets in <code>changesets</code> from the cache. A
150 * {@link ChangesetCacheEvent} is fired.
151 *
152 * @param changesets the changesets to remove. Ignored if null.
153 */
154 public void remove(Collection<Changeset> changesets) {
155 if (changesets == null) return;
156 DefaultChangesetCacheEvent evt = new DefaultChangesetCacheEvent(this);
157 for (Changeset cs : changesets) {
158 if (cs == null || cs.isNew()) {
159 continue;
160 }
161 remove(cs.getId(), evt);
162 }
163 if (! evt.isEmpty()) {
164 fireChangesetCacheEvent(evt);
165 }
166 }
167
168 public int size() {
169 return cache.size();
170 }
171
172 public void clear() {
173 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
174 for (Changeset cs: cache.values()) {
175 e.rememberRemovedChangeset(cs);
176 }
177 cache.clear();
178 fireChangesetCacheEvent(e);
179 }
180
181 /**
182 * Replies the list of open changesets.
183 * @return The list of open changesets
184 */
185 public List<Changeset> getOpenChangesets() {
186 List<Changeset> ret = new ArrayList<>();
187 for (Changeset cs: cache.values()) {
188 if (cs.isOpen()) {
189 ret.add(cs);
190 }
191 }
192 return ret;
193 }
194
195 /* ------------------------------------------------------------------------- */
196 /* interface PreferenceChangedListener */
197 /* ------------------------------------------------------------------------- */
198 @Override
199 public void preferenceChanged(PreferenceChangeEvent e) {
200 if (e.getKey() == null || !"osm-server.url".equals(e.getKey()))
201 return;
202
203 // clear the cache when the API url changes
204 if (e.getOldValue() == null || e.getNewValue() == null || !e.getOldValue().equals(e.getNewValue())) {
205 clear();
206 }
207 }
208}
Note: See TracBrowser for help on using the repository browser.