1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Iterator;
|
---|
6 | import java.util.LinkedHashMap;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
11 | import org.openstreetmap.josm.data.preferences.CollectionProperty;
|
---|
12 |
|
---|
13 | class 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 | }
|
---|