source: josm/trunk/src/org/openstreetmap/josm/actions/SelectNonBranchingWaySequences.java@ 11590

Last change on this file since 11590 was 9999, checked in by Don-vip, 8 years ago

sonar - do not copy collection contents from one to another with a loop

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import java.util.Collection;
5import java.util.LinkedList;
6import java.util.Set;
7import java.util.TreeSet;
8
9import org.openstreetmap.josm.data.osm.DataSet;
10import org.openstreetmap.josm.data.osm.Node;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.Way;
13
14/**
15 * Auxiliary class for the {@link SelectNonBranchingWaySequencesAction}.
16 *
17 * @author Marko Mäkelä
18 */
19public class SelectNonBranchingWaySequences {
20 /**
21 * outer endpoints of selected ways
22 */
23 private Set<Node> outerNodes;
24 /**
25 * endpoints of selected ways
26 */
27 private Set<Node> nodes;
28
29 /**
30 * Creates a way selection
31 *
32 * @param ways selection a selection of ways
33 */
34 public SelectNonBranchingWaySequences(final Collection<Way> ways) {
35 if (ways.isEmpty()) {
36 // The selection cannot be extended.
37 outerNodes = null;
38 nodes = null;
39 } else {
40 nodes = new TreeSet<>();
41 outerNodes = new TreeSet<>();
42
43 for (Way way : ways) {
44 addNodes(way);
45 }
46 }
47 }
48
49 /**
50 * Add a way endpoint to nodes, outerNodes
51 *
52 * @param node a way endpoint
53 */
54 private void addNodes(Node node) {
55 if (node == null) return;
56 else if (!nodes.add(node))
57 outerNodes.remove(node);
58 else
59 outerNodes.add(node);
60 }
61
62 /**
63 * Add the endpoints of the way to nodes, outerNodes
64 *
65 * @param way a way whose endpoints are added
66 */
67 private void addNodes(Way way) {
68 addNodes(way.firstNode());
69 addNodes(way.lastNode());
70 }
71
72 /**
73 * Find out if the selection can be extended
74 *
75 * @return true if the selection can be extended
76 */
77 public boolean canExtend() {
78 return outerNodes != null && !outerNodes.isEmpty();
79 }
80
81 /**
82 * Finds out if the current selection can be extended.
83 *
84 * @param selection current selection (ways and others)
85 * @param node perimeter node from which to extend the selection
86 * @return a way by which to extend the selection, or null
87 */
88 private static Way findWay(Collection<OsmPrimitive> selection, Node node) {
89 Way foundWay = null;
90
91 for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(),
92 Way.class)) {
93 if (way.getNodesCount() < 2 || !way.isFirstLastNode(node)
94 || !way.isSelectable()
95 || selection.contains(way))
96 continue;
97
98 /* A previously unselected way was found that is connected
99 to the node. */
100 if (foundWay != null)
101 /* This is not the only qualifying way. There is a
102 branch at the node, and we cannot extend the selection. */
103 return null;
104
105 /* Remember the first found qualifying way. */
106 foundWay = way;
107 }
108
109 /* Return the only way found, or null if none was found. */
110 return foundWay;
111 }
112
113 /**
114 * Finds out if the current selection can be extended.
115 * <p>
116 * The members outerNodes, nodes must have been initialized.
117 * How to update these members when extending the selection, @see extend().
118 * </p>
119 * @param selection current selection
120 * @return a way by which to extend the selection, or null
121 */
122 private Way findWay(Collection<OsmPrimitive> selection) {
123 for (Node node : outerNodes) {
124 Way way = findWay(selection, node);
125 if (way != null)
126 return way;
127 }
128
129 return null;
130 }
131
132 /**
133 * Extend the current selection
134 *
135 * @param data the data set in which to extend the selection
136 */
137 public void extend(DataSet data) {
138 if (!canExtend())
139 return;
140
141 Collection<OsmPrimitive> currentSelection = data.getSelected();
142
143 Way way = findWay(currentSelection);
144
145 if (way == null)
146 return;
147
148 boolean selectionChanged = false;
149 Collection<OsmPrimitive> selection = new LinkedList<>(currentSelection);
150
151 do {
152 if (!selection.add(way))
153 break;
154
155 selectionChanged = true;
156 addNodes(way);
157
158 way = findWay(selection);
159 } while (way != null);
160
161 if (selectionChanged)
162 data.setSelected(selection, true);
163 }
164}
Note: See TracBrowser for help on using the repository browser.