Changeset 9939 in josm


Ignore:
Timestamp:
2016-03-06T12:00:32+01:00 (4 years ago)
Author:
simon04
Message:

see #12554 - Factor out RecentTagCollection, add unit tests

Location:
trunk
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    r9877 r9939  
    3535import java.util.HashMap;
    3636import java.util.Iterator;
    37 import java.util.LinkedHashMap;
    38 import java.util.LinkedList;
    3937import java.util.List;
    4038import java.util.Map;
     
    7068import org.openstreetmap.josm.data.osm.Tag;
    7169import org.openstreetmap.josm.data.preferences.BooleanProperty;
     70import org.openstreetmap.josm.data.preferences.CollectionProperty;
    7271import org.openstreetmap.josm.data.preferences.EnumProperty;
    7372import org.openstreetmap.josm.data.preferences.IntegerProperty;
     
    125124    public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = new IntegerProperty("properties.recently-added-tags",
    126125            DEFAULT_LRU_TAGS_NUMBER);
     126    /** The preference storage of recent tags */
     127    public static final CollectionProperty COLLECTION_PROPERTY = new CollectionProperty("properties.recent-tags",
     128            Collections.<String>emptyList());
    127129
    128130    /**
     
    156158        "properties.refresh-recently-added-tags", RefreshRecent.class, RefreshRecent.STATUS);
    157159
    158     // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
    159     private final Map<Tag, Void> recentTags = new LinkedHashMap<Tag, Void>(MAX_LRU_TAGS_NUMBER+1, 1.1f, true) {
    160         @Override
    161         protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
    162             return size() > MAX_LRU_TAGS_NUMBER;
    163         }
    164     };
     160    final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
    165161
    166162    // Copy of recently added tags, used to cache initial status
     
    291287    public void loadTagsIfNeeded() {
    292288        if (PROPERTY_REMEMBER_TAGS.get() && recentTags.isEmpty()) {
    293             recentTags.clear();
    294             Collection<String> c = Main.pref.getCollection("properties.recent-tags");
    295             Iterator<String> it = c.iterator();
    296             while (it.hasNext()) {
    297                 String key = it.next();
    298                 String value = it.next();
    299                 recentTags.put(new Tag(key, value), null);
    300             }
     289            recentTags.loadFromPreference(COLLECTION_PROPERTY);
    301290        }
    302291    }
     
    307296    public void saveTagsIfNeeded() {
    308297        if (PROPERTY_REMEMBER_TAGS.get() && !recentTags.isEmpty()) {
    309             List<String> c = new ArrayList<>(recentTags.size()*2);
    310             for (Tag t: recentTags.keySet()) {
    311                 c.add(t.getKey());
    312                 c.add(t.getValue());
    313             }
    314             Main.pref.putCollection("properties.recent-tags", c);
     298            recentTags.saveToPreference(COLLECTION_PROPERTY);
    315299        }
    316300    }
     
    320304     */
    321305    private void cacheRecentTags() {
    322         tags = new LinkedList<>(recentTags.keySet());
     306        tags = recentTags.toList();
    323307    }
    324308
     
    857841            // This implies to iterate in descending order, as the oldest elements will only be removed after we reach the maximum
    858842            // number and not the number of tags to show.
    859             // However, as Set does not allow to iterate in descending order, we need to copy its elements into a List we can access
    860             // in reverse order.
    861843            for (int i = tags.size()-1; i >= 0 && count < tagsToShow; i--) {
    862844                final Tag t = tags.get(i);
     
    995977            lastAddKey = key;
    996978            lastAddValue = value;
    997             recentTags.put(new Tag(key, value), null);
     979            recentTags.add(new Tag(key, value));
    998980            valueCount.put(key, new TreeMap<String, Integer>());
    999981            AutoCompletionManager.rememberUserInput(key, value, false);
Note: See TracChangeset for help on using the changeset viewer.