Ignore:
Timestamp:
2010-09-10T21:24:17+02:00 (14 years ago)
Author:
skela
Message:

'Do not mutate the collection of selected ways. Convert TAB to spaces.'

Location:
applications/editors/josm/plugins/wayselector
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wayselector/build.xml

    r21706 r23100  
    3131
    3232        <!-- enter the SVN commit message -->
    33         <property name="commit.message" value="Adjust to read-only JOSM selection object" />
     33        <property name="commit.message" value="Do not mutate the collection of selected ways. Convert TAB to spaces." />
    3434        <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
    3535        <property name="plugin.main.version" value="3095" />
  • applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelection.java

    r20784 r23100  
    1818 */
    1919public class WaySelection {
    20     /** selected ways */
    21     Collection<Way> ways;
    2220    /** outer endpoints of selected ways */
    2321    TreeSet<Node> outerNodes;
     
    2826     @param[in] selection a selection of ways
    2927     */
    30     public WaySelection(Collection<Way> ways) {
    31         this.ways = ways;
    32         outerNodes = null;
    33         nodes = null;
     28    public WaySelection(final Collection<Way> ways) {
     29        if (ways.isEmpty()) {
     30            // The selection cannot be extended.
     31            outerNodes = null;
     32            nodes = null;
     33        }
     34        else {
     35            nodes = new TreeSet<Node>();
     36            outerNodes = new TreeSet<Node>();
     37
     38            for (Way way : ways)
     39                addNodes(way);
     40        }
    3441    }
    3542
     
    3845    private void addNodes(Node node) {
    3946        if (node == null);
    40         else if (!nodes.add(node))
    41             outerNodes.remove(node);
    42         else
    43             outerNodes.add(node);
     47        else if (!nodes.add(node))
     48            outerNodes.remove(node);
     49        else
     50            outerNodes.add(node);
    4451    }
    4552
     
    4855    private void addNodes(Way way) {
    4956        addNodes(way.firstNode());
    50         addNodes(way.lastNode());
     57        addNodes(way.lastNode());
    5158    }
    5259
     
    5461     @return true if the selection can be extended */
    5562    public boolean canExtend() {
    56         if (ways.isEmpty())
    57             return false;
    58 
    59         nodes = new TreeSet<Node>();
    60         outerNodes = new TreeSet<Node>();
    61 
    62         for (Way way : ways)
    63             addNodes(way);
    64 
    65         return !outerNodes.isEmpty();
     63        return outerNodes != null && !outerNodes.isEmpty();
    6664    }
    6765
     
    7573
    7674        for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(),
    77                                                     Way.class))
    78             if (way.getNodesCount() >= 2 && !selection.contains(way) &&
    79                 way.isFirstLastNode(node))
     75                                                    Way.class))
     76            if (way.getNodesCount() >= 2 && !selection.contains(way) &&
     77                way.isFirstLastNode(node))
    8078                foundWays.add(way);
    8179
    82         return foundWays.size() == 1 ? foundWays.first() : null;
     80        return foundWays.size() == 1 ? foundWays.first() : null;
    8381    }
    8482
     
    8684     Finds out if the current selection can be extended.
    8785
    88      The members ways, outerNodes, nodes must have been
    89      initialized; @see canExtend(). How to update these
    90      members when extending the selection, @see extend().
     86     The members outerNodes, nodes must have been initialized.
     87     How to update these members when extending the selection, @see extend().
    9188     @param selection current selection
    9289     @return a way by which to extend the selection, or null */
    9390    private Way findWay(Collection<OsmPrimitive> selection) {
    94         for (Node node : outerNodes) {
    95             Way way = findWay(selection, node);
    96             if (way != null)
    97                 return way;
    98         }
     91        for (Node node : outerNodes) {
     92            Way way = findWay(selection, node);
     93            if (way != null)
     94                return way;
     95        }
    9996
    100         return null;
     97        return null;
    10198    }
    10299
     
    104101     @param data the data set in which to extend the selection */
    105102    void extend(DataSet data) {
    106         Collection<OsmPrimitive> currentSelection;
    107         LinkedList<OsmPrimitive> selection;
    108         boolean selectionChanged = false;
     103        Collection<OsmPrimitive> currentSelection;
     104        LinkedList<OsmPrimitive> selection;
     105        boolean selectionChanged = false;
    109106        Way way;
    110107
    111108        if (!canExtend())
    112             return;
     109            return;
    113110
    114         currentSelection = data.getSelected();
     111        currentSelection = data.getSelected();
    115112
    116         way = findWay(currentSelection);
     113        way = findWay(currentSelection);
    117114
    118         if (way == null)
    119             return;
     115        if (way == null)
     116            return;
    120117
    121         selection = new LinkedList<OsmPrimitive>();
    122         for (OsmPrimitive primitive : currentSelection)
    123             selection.add(primitive);
     118        selection = new LinkedList<OsmPrimitive>();
     119        for (OsmPrimitive primitive : currentSelection)
     120            selection.add(primitive);
    124121
    125         do {
    126             if (!selection.add(way))
    127                 break;
     122        do {
     123            if (!selection.add(way))
     124                break;
    128125
    129             selectionChanged = true;
    130             ways.add(way);
    131             addNodes(way);
     126            selectionChanged = true;
     127            addNodes(way);
    132128
    133             way = findWay(selection);
    134         } while (way != null);
     129            way = findWay(selection);
     130        } while (way != null);
    135131
    136         if (selectionChanged)
    137             data.setSelected(selection, true);
     132        if (selectionChanged)
     133            data.setSelected(selection, true);
    138134    }
    139135}
Note: See TracChangeset for help on using the changeset viewer.