Changeset 15863 in josm for trunk/src


Ignore:
Timestamp:
2020-02-16T15:46:02+01:00 (4 years ago)
Author:
simon04
Message:

Utils: use Stream API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r15862 r15863  
    6262import java.util.regex.Pattern;
    6363import java.util.stream.Collectors;
     64import java.util.stream.IntStream;
    6465import java.util.stream.Stream;
    6566import java.util.zip.ZipFile;
     
    240241        if (values == null)
    241242            return null;
    242         StringBuilder s = null;
    243         for (Object a : values) {
    244             if (a == null) {
    245                 a = "";
    246             }
    247             if (s != null) {
    248                 s.append(sep).append(a);
    249             } else {
    250                 s = new StringBuilder(a.toString());
    251             }
    252         }
    253         return s != null ? s.toString() : "";
     243        return values.stream()
     244                .map(v -> v != null ? v.toString() : "")
     245                .collect(Collectors.joining(sep));
    254246    }
    255247
     
    10231015    public static List<String> getMatches(final Matcher m) {
    10241016        if (m.matches()) {
    1025             List<String> result = new ArrayList<>(m.groupCount() + 1);
    1026             for (int i = 0; i <= m.groupCount(); i++) {
    1027                 result.add(m.group(i));
    1028             }
    1029             return result;
     1017            return IntStream.rangeClosed(0, m.groupCount())
     1018                    .mapToObj(m::group)
     1019                    .collect(Collectors.toList());
    10301020        } else {
    10311021            return null;
     
    14451435    public static boolean hasExtension(String filename, String... extensions) {
    14461436        String name = filename.toLowerCase(Locale.ENGLISH).replace("?format=raw", "");
    1447         for (String ext : extensions) {
    1448             if (name.endsWith('.' + ext.toLowerCase(Locale.ENGLISH)))
    1449                 return true;
    1450         }
    1451         return false;
     1437        return Arrays.stream(extensions)
     1438                .anyMatch(ext -> name.endsWith('.' + ext.toLowerCase(Locale.ENGLISH)));
    14521439    }
    14531440
Note: See TracChangeset for help on using the changeset viewer.