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

Last change on this file since 5890 was 5890, checked in by akks, 11 years ago

Minor Javadoc fixes and adding @Override annotations

  • Property svn:eol-style set to native
File size: 6.2 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 class ChangesetCache implements PreferenceChangedListener{
35 /** the unique instance */
36 static private 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<Integer, Changeset>();
49
50 private final CopyOnWriteArrayList<ChangesetCacheListener> listeners =
51 new CopyOnWriteArrayList<ChangesetCacheListener>();
52
53 private ChangesetCache() {
54 Main.pref.addPreferenceChangeListener(this);
55 }
56
57 public void addChangesetCacheListener(ChangesetCacheListener listener) {
58 listeners.addIfAbsent(listener);
59 }
60
61 public void removeChangesetCacheListener(ChangesetCacheListener listener) {
62 listeners.remove(listener);
63 }
64
65 protected void fireChangesetCacheEvent(final ChangesetCacheEvent e) {
66 GuiHelper.runInEDT(new Runnable() {
67 @Override public void run() {
68 for(ChangesetCacheListener l: listeners) {
69 l.changesetCacheUpdated(e);
70 }
71 }
72 });
73 }
74
75 protected void update(Changeset cs, DefaultChangesetCacheEvent e) {
76 if (cs == null) return;
77 if (cs.isNew()) return;
78 Changeset inCache = cache.get(cs.getId());
79 if (inCache != null) {
80 inCache.mergeFrom(cs);
81 e.rememberUpdatedChangeset(inCache);
82 } else {
83 e.rememberAddedChangeset(cs);
84 cache.put(cs.getId(), cs);
85 }
86 }
87
88 public void update(Changeset cs) {
89 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
90 update(cs, e);
91 fireChangesetCacheEvent(e);
92 }
93
94 public void update(Collection<Changeset> changesets) {
95 if (changesets == null || changesets.isEmpty()) return;
96 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
97 for (Changeset cs: changesets) {
98 update(cs, e);
99 }
100 fireChangesetCacheEvent(e);
101 }
102
103 public boolean contains(int id) {
104 if (id <=0) return false;
105 return cache.get(id) != null;
106 }
107
108 public boolean contains(Changeset cs) {
109 if (cs == null) return false;
110 if (cs.isNew()) return false;
111 return contains(cs.getId());
112 }
113
114 public Changeset get(int id) {
115 return cache.get(id);
116 }
117
118 public Set<Changeset> getChangesets() {
119 return new HashSet<Changeset>(cache.values());
120 }
121
122 protected void remove(int id, DefaultChangesetCacheEvent e) {
123 if (id <= 0) return;
124 Changeset cs = cache.get(id);
125 if (cs == null) return;
126 cache.remove(id);
127 e.rememberRemovedChangeset(cs);
128 }
129
130 public void remove(int id) {
131 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
132 remove(id, e);
133 if (! e.isEmpty()) {
134 fireChangesetCacheEvent(e);
135 }
136 }
137
138 public void remove(Changeset cs) {
139 if (cs == null) return;
140 if (cs.isNew()) return;
141 remove(cs.getId());
142 }
143
144 /**
145 * Removes the changesets in <code>changesets</code> from the cache. A
146 * {@link ChangesetCacheEvent} is fired.
147 *
148 * @param changesets the changesets to remove. Ignored if null.
149 */
150 public void remove(Collection<Changeset> changesets) {
151 if (changesets == null) return;
152 DefaultChangesetCacheEvent evt = new DefaultChangesetCacheEvent(this);
153 for (Changeset cs : changesets) {
154 if (cs == null || cs.isNew()) {
155 continue;
156 }
157 remove(cs.getId(), evt);
158 }
159 if (! evt.isEmpty()) {
160 fireChangesetCacheEvent(evt);
161 }
162 }
163
164 public int size() {
165 return cache.size();
166 }
167
168 public void clear() {
169 DefaultChangesetCacheEvent e = new DefaultChangesetCacheEvent(this);
170 for (Changeset cs: cache.values()) {
171 e.rememberRemovedChangeset(cs);
172 }
173 cache.clear();
174 fireChangesetCacheEvent(e);
175 }
176
177 public List<Changeset> getOpenChangesets() {
178 List<Changeset> ret = new ArrayList<Changeset>();
179 for (Changeset cs: cache.values()) {
180 if (cs.isOpen()) {
181 ret.add(cs);
182 }
183 }
184 return ret;
185 }
186
187 /* ------------------------------------------------------------------------- */
188 /* interface PreferenceChangedListener */
189 /* ------------------------------------------------------------------------- */
190 @Override
191 public void preferenceChanged(PreferenceChangeEvent e) {
192 if (e.getKey() == null || ! e.getKey().equals("osm-server.url"))
193 return;
194
195 // clear the cache when the API url changes
196 if (e.getOldValue() == null || e.getNewValue() == null || !e.getOldValue().equals(e.getNewValue())) {
197 clear();
198 }
199 }
200}
Note: See TracBrowser for help on using the repository browser.