Changeset 12592 in josm


Ignore:
Timestamp:
2017-08-11T19:47:58+02:00 (7 years ago)
Author:
bastiK
Message:

use Optional instead of 0-or-1-element Collection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r12517 r12592  
    1717import java.util.Iterator;
    1818import java.util.LinkedList;
     19import java.util.Optional;
    1920import java.util.Set;
    2021
     
    250251     */
    251252    private boolean giveUserFeedback(MouseEvent e, int modifiers) {
    252         Collection<OsmPrimitive> c = asColl(
     253        Optional<OsmPrimitive> c = Optional.ofNullable(
    253254                mv.getNearestNodeOrWay(e.getPoint(), mv.isSelectablePredicate, true));
    254255
    255256        updateKeyModifiersEx(modifiers);
    256         determineMapMode(!c.isEmpty());
     257        determineMapMode(c.isPresent());
    257258
    258259        Set<OsmPrimitive> newHighlights = new HashSet<>();
     
    272273
    273274        // return early if there can't be any highlights
    274         if (!drawTargetHighlight || mode != Mode.MOVE || c.isEmpty())
     275        if (!drawTargetHighlight || mode != Mode.MOVE || !c.isPresent())
    275276            return repaintIfRequired(newHighlights);
    276277
    277278        // CTRL toggles selection, but if while dragging CTRL means merge
    278279        final boolean isToggleMode = ctrl && !dragInProgress();
    279         for (OsmPrimitive x : c) {
     280        if (c.isPresent() && (isToggleMode || !c.get().isSelected())) {
    280281            // only highlight primitives that will change the selection
    281282            // when clicked. I.e. don't highlight selected elements unless
    282283            // we are in toggle mode.
    283             if (isToggleMode || !x.isSelected()) {
    284                 newHighlights.add(x);
    285             }
     284            newHighlights.add(c.get());
    286285        }
    287286        return repaintIfRequired(newHighlights);
     
    295294     * @return the cursor that should be displayed
    296295     */
    297     private Cursor getCursor(Collection<OsmPrimitive> nearbyStuff) {
     296    private Cursor getCursor(Optional<OsmPrimitive> nearbyStuff) {
    298297        String c = "rect";
    299298        switch(mode) {
     
    303302                break;
    304303            }
    305             final Iterator<OsmPrimitive> it = nearbyStuff.iterator();
    306             final OsmPrimitive osm = it.hasNext() ? it.next() : null;
     304            final OsmPrimitive osm = nearbyStuff.orElse(null);
    307305
    308306            if (dragInProgress()) {
     
    526524                needsRepaint = true;
    527525            }
    528             mv.setNewCursor(getCursor(asColl(p)), this);
     526            mv.setNewCursor(getCursor(Optional.ofNullable(p)), this);
    529527            // also update the stored mouse event, so we can display the correct cursor
    530528            // when dragging a node onto another one and then press CTRL to merge
Note: See TracChangeset for help on using the changeset viewer.