Ignore:
Timestamp:
2020-04-30T20:53:59+02:00 (5 years ago)
Author:
simon04
Message:

Java 8: use Stream.anyMatch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r16212 r16387  
    1717import java.util.function.Consumer;
    1818import java.util.stream.Collectors;
     19import java.util.stream.IntStream;
    1920import java.util.stream.Stream;
    2021
     
    510511        if (referrers instanceof OsmPrimitive)
    511512            return referrers instanceof Relation && ((OsmPrimitive) referrers).isSelected();
    512         for (OsmPrimitive ref : (OsmPrimitive[]) referrers) {
    513             if (ref instanceof Relation && ref.isSelected())
    514                 return true;
    515         }
    516         return false;
     513        return Arrays.stream((OsmPrimitive[]) referrers)
     514                .anyMatch(ref -> ref instanceof Relation && ref.isSelected());
    517515    }
    518516
     
    524522            return isOuterMemberOfMultipolygon((OsmPrimitive) referrers);
    525523        }
    526         for (OsmPrimitive ref : (OsmPrimitive[]) referrers) {
    527             if (isOuterMemberOfMultipolygon(ref))
    528                 return true;
    529         }
    530         return false;
     524        return Arrays.stream((OsmPrimitive[]) referrers)
     525                .anyMatch(this::isOuterMemberOfMultipolygon);
    531526    }
    532527
    533528    private boolean isOuterMemberOfMultipolygon(OsmPrimitive ref) {
    534         if (ref instanceof Relation && ref.isSelected() && ((Relation) ref).isMultipolygon()) {
    535             for (RelationMember rm : ((Relation) ref).getMembersFor(Collections.singleton(this))) {
    536                 if ("outer".equals(rm.getRole())) {
    537                     return true;
    538                 }
    539             }
    540         }
    541         return false;
     529        return ref instanceof Relation
     530                && ref.isSelected()
     531                && ((Relation) ref).isMultipolygon()
     532                && ((Relation) ref).getMembersFor(Collections.singleton(this)).stream()
     533                    .anyMatch(rm -> "outer".equals(rm.getRole()));
    542534    }
    543535
     
    576568
    577569    private void updateTagged() {
    578         for (String key: keySet()) {
    579             // 'area' is not really uninteresting (putting it in that list may have unpredictable side effects)
    580             // but it's clearly not enough to consider an object as tagged (see #9261)
    581             if (!isUninterestingKey(key) && !"area".equals(key)) {
    582                 updateFlagsNoLock(FLAG_TAGGED, true);
    583                 return;
    584             }
    585         }
    586         updateFlagsNoLock(FLAG_TAGGED, false);
     570        // 'area' is not really uninteresting (putting it in that list may have unpredictable side effects)
     571        // but it's clearly not enough to consider an object as tagged (see #9261)
     572        updateFlagsNoLock(FLAG_TAGGED, keySet().stream()
     573                .anyMatch(key -> !isUninterestingKey(key) && !"area".equals(key)));
    587574    }
    588575
    589576    private void updateAnnotated() {
    590         for (String key: keySet()) {
    591             if (getWorkInProgressKeys().contains(key)) {
    592                 updateFlagsNoLock(FLAG_ANNOTATED, true);
    593                 return;
    594             }
    595         }
    596         updateFlagsNoLock(FLAG_ANNOTATED, false);
     577        updateFlagsNoLock(FLAG_ANNOTATED, keySet().stream()
     578                .anyMatch(getWorkInProgressKeys()::contains));
    597579    }
    598580
     
    746728        } else if (referrers instanceof OsmPrimitive[]) {
    747729            OsmPrimitive[] orig = (OsmPrimitive[]) referrers;
    748             int idx = -1;
    749             for (int i = 0; i < orig.length; i++) {
    750                 if (orig[i] == referrer) {
    751                     idx = i;
    752                     break;
    753                 }
    754             }
     730            int idx = IntStream.range(0, orig.length)
     731                    .filter(i -> orig[i] == referrer)
     732                    .findFirst().orElse(-1);
    755733            if (idx == -1)
    756734                return;
     
    904882
    905883    /**
    906      * Merges the technical and semantical attributes from <code>other</code> onto this.
     884     * Merges the technical and semantic attributes from <code>other</code> onto this.
    907885     *
    908886     * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
Note: See TracChangeset for help on using the changeset viewer.