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

Last change on this file since 12659 was 12659, checked in by Don-vip, 7 years ago

see #15182 - extract SearchMode and SearchSetting from actions.search.SearchAction to data.osm.search

File size: 3.1 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.osm.search.SearchParseError;
12import org.openstreetmap.josm.data.osm.search.SearchSetting;
13import org.openstreetmap.josm.data.osm.search.SearchCompiler;
14import org.openstreetmap.josm.data.preferences.CollectionProperty;
15
16/**
17 * Manages list of recently used tags that will be displayed in the {@link PropertiesDialog}.
18 */
19class RecentTagCollection {
20
21 /**
22 * LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
23 */
24 static final class LruCache extends LinkedHashMap<Tag, Void> {
25 private final int capacity;
26
27 LruCache(int capacity) {
28 super(capacity + 1, 1.1f, true);
29 this.capacity = capacity;
30 }
31
32 @Override
33 protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
34 return size() > capacity;
35 }
36 }
37
38 private final Map<Tag, Void> recentTags;
39 private SearchCompiler.Match tagsToIgnore;
40
41 RecentTagCollection(final int capacity) {
42 recentTags = new LruCache(capacity);
43 tagsToIgnore = SearchCompiler.Never.INSTANCE;
44 }
45
46 public void loadFromPreference(CollectionProperty property) {
47 recentTags.clear();
48 Iterator<String> it = property.get().iterator();
49 while (it.hasNext()) {
50 String key = it.next();
51 String value = it.next();
52 add(new Tag(key, value));
53 }
54 }
55
56 public void saveToPreference(CollectionProperty property) {
57 List<String> c = new ArrayList<>(recentTags.size() * 2);
58 for (Tag t : recentTags.keySet()) {
59 c.add(t.getKey());
60 c.add(t.getValue());
61 }
62 property.put(c);
63 }
64
65 public void add(Tag tag) {
66 if (!tagsToIgnore.match(tag)) {
67 recentTags.put(tag, null);
68 }
69 }
70
71 public boolean isEmpty() {
72 return recentTags.isEmpty();
73 }
74
75 public List<Tag> toList() {
76 return new ArrayList<>(recentTags.keySet());
77 }
78
79 public void setTagsToIgnore(SearchCompiler.Match tagsToIgnore) {
80 this.tagsToIgnore = tagsToIgnore;
81 recentTags.keySet().removeIf(tagsToIgnore::match);
82 }
83
84 public void setTagsToIgnore(SearchSetting tagsToIgnore) throws SearchParseError {
85 setTagsToIgnore(tagsToIgnore.text.isEmpty() ? SearchCompiler.Never.INSTANCE : SearchCompiler.compile(tagsToIgnore));
86 }
87
88 public SearchSetting ignoreTag(Tag tagToIgnore, SearchSetting settingToUpdate) throws SearchParseError {
89 final String forTag = SearchCompiler.buildSearchStringForTag(tagToIgnore.getKey(), tagToIgnore.getValue());
90 settingToUpdate.text = settingToUpdate.text.isEmpty()
91 ? forTag
92 : settingToUpdate.text + " OR " + forTag;
93 setTagsToIgnore(settingToUpdate);
94 return settingToUpdate;
95 }
96}
Note: See TracBrowser for help on using the repository browser.