Ignore:
Timestamp:
2020-05-17T12:08:17+02:00 (5 years ago)
Author:
simon04
Message:

see #19251 - Java 8: use Stream

Location:
trunk/src/org/openstreetmap/josm/spi/preferences
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/spi/preferences/AbstractPreferences.java

    r14147 r16436  
    77import java.util.Map.Entry;
    88import java.util.TreeMap;
     9import java.util.stream.Collectors;
    910
    1011import org.openstreetmap.josm.tools.Logging;
     
    158159     */
    159160    public Map<String, String> getAllPrefix(String prefix) {
    160         final Map<String, String> all = new TreeMap<>();
    161         for (final Entry<String, Setting<?>> e : getAllSettings().entrySet()) {
    162             if (e.getKey().startsWith(prefix) && (e.getValue() instanceof StringSetting)) {
    163                 all.put(e.getKey(), ((StringSetting) e.getValue()).getValue());
    164             }
    165         }
    166         return all;
     161        return getAllSettings().entrySet().stream()
     162                .filter(e -> e.getKey().startsWith(prefix) && (e.getValue() instanceof StringSetting))
     163                .collect(Collectors.toMap(Entry::getKey, e -> ((StringSetting) e.getValue()).getValue(), (a, b) -> b, TreeMap::new));
    167164    }
    168165
     
    173170     */
    174171    public List<String> getAllPrefixCollectionKeys(String prefix) {
    175         final List<String> all = new LinkedList<>();
    176         for (Entry<String, Setting<?>> entry : getAllSettings().entrySet()) {
    177             if (entry.getKey().startsWith(prefix) && entry.getValue() instanceof ListSetting) {
    178                 all.add(entry.getKey());
    179             }
    180         }
    181         return all;
     172        return getAllSettings().entrySet().stream()
     173                .filter(entry -> entry.getKey().startsWith(prefix) && entry.getValue() instanceof ListSetting)
     174                .map(Entry::getKey)
     175                .collect(Collectors.toCollection(LinkedList::new));
    182176    }
    183177}
  • trunk/src/org/openstreetmap/josm/spi/preferences/ListListSetting.java

    r12882 r16436  
    44import java.util.ArrayList;
    55import java.util.Collection;
    6 import java.util.Collections;
    76import java.util.List;
     7import java.util.stream.Collectors;
     8
     9import org.openstreetmap.josm.tools.StreamUtils;
     10import org.openstreetmap.josm.tools.Utils;
    811
    912/**
     
    2932    public static ListListSetting create(Collection<Collection<String>> value) {
    3033        if (value != null) {
    31             List<List<String>> valueList = new ArrayList<>(value.size());
    32             for (Collection<String> lst : value) {
    33                 valueList.add(new ArrayList<>(lst));
    34             }
     34            List<List<String>> valueList = value.stream()
     35                    .map(ArrayList::new)
     36                    .collect(Collectors.toList());
    3537            return new ListListSetting(valueList);
    3638        }
     
    4345            return new ListListSetting(null);
    4446
    45         List<List<String>> copy = new ArrayList<>(value.size());
    46         for (Collection<String> lst : value) {
    47             List<String> lstCopy = new ArrayList<>(lst);
    48             copy.add(Collections.unmodifiableList(lstCopy));
    49         }
    50         return new ListListSetting(Collections.unmodifiableList(copy));
     47        List<List<String>> copy = value.stream()
     48                .map(Utils::toUnmodifiableList)
     49                .collect(StreamUtils.toUnmodifiableList());
     50        return new ListListSetting(copy);
    5151    }
    5252
  • trunk/src/org/openstreetmap/josm/spi/preferences/MapListSetting.java

    r14827 r16436  
    22package org.openstreetmap.josm.spi.preferences;
    33
    4 import java.util.ArrayList;
    5 import java.util.Collections;
    64import java.util.LinkedHashMap;
    75import java.util.List;
    86import java.util.Map;
    97import java.util.SortedMap;
     8
     9import org.openstreetmap.josm.tools.StreamUtils;
     10import org.openstreetmap.josm.tools.Utils;
    1011
    1112/**
     
    2829        if (value == null)
    2930            return new MapListSetting(null);
    30         List<Map<String, String>> copy = new ArrayList<>(value.size());
    31         for (Map<String, String> map : value) {
    32             Map<String, String> mapCopy = new LinkedHashMap<>(map);
    33             copy.add(Collections.unmodifiableMap(mapCopy));
    34         }
    35         return new MapListSetting(Collections.unmodifiableList(copy));
     31        List<Map<String, String>> copy = value.stream()
     32                .map(LinkedHashMap::new)
     33                .map(Utils::toUnmodifiableMap)
     34                .collect(StreamUtils.toUnmodifiableList());
     35        return new MapListSetting(copy);
    3636    }
    3737
Note: See TracChangeset for help on using the changeset viewer.