source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/RecentTagCollection.java@ 9939

Last change on this file since 9939 was 9939, checked in by simon04, 8 years ago

see #12554 - Factor out RecentTagCollection, add unit tests

File size: 1.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import java.util.ArrayList;
5import java.util.Iterator;
6import java.util.LinkedHashMap;
7import java.util.List;
8import java.util.Map;
9
10import org.openstreetmap.josm.data.osm.Tag;
11import org.openstreetmap.josm.data.preferences.CollectionProperty;
12
13class RecentTagCollection {
14
15 private final Map<Tag, Void> recentTags;
16
17 RecentTagCollection(final int capacity) {
18 // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
19 recentTags = new LinkedHashMap<Tag, Void>(capacity + 1, 1.1f, true) {
20 @Override
21 protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
22 return size() > capacity;
23 }
24 };
25 }
26
27 public void loadFromPreference(CollectionProperty property) {
28 recentTags.clear();
29 Iterator<String> it = property.get().iterator();
30 while (it.hasNext()) {
31 String key = it.next();
32 String value = it.next();
33 recentTags.put(new Tag(key, value), null);
34 }
35 }
36
37 public void saveToPreference(CollectionProperty property) {
38 List<String> c = new ArrayList<>(recentTags.size() * 2);
39 for (Tag t : recentTags.keySet()) {
40 c.add(t.getKey());
41 c.add(t.getValue());
42 }
43 property.put(c);
44 }
45
46 public void add(Tag key) {
47 recentTags.put(key, null);
48 }
49
50 public boolean isEmpty() {
51 return recentTags.isEmpty();
52 }
53
54 public List<Tag> toList() {
55 return new ArrayList<>(recentTags.keySet());
56 }
57}
Note: See TracBrowser for help on using the repository browser.