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

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

findbugs - SIC_INNER_SHOULD_BE_STATIC_ANON

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