Ticket #24949: 24949.patch

File 24949.patch, 4.0 KB (added by DanProgs <Abenteurer.2901@…>, 21 hours ago)
  • src/org/openstreetmap/josm/actions/AlignGridRotationAction.java

    ### Eclipse Workspace Patch 1.0
    #P josm
    diff --git src/org/openstreetmap/josm/actions/AlignGridRotationAction.java src/org/openstreetmap/josm/actions/AlignGridRotationAction.java
    index 5f7459a..e279c86 100644
     
    2525 * @since 19640
    2626 */
    2727public class AlignGridRotationAction extends JosmAction {
     28   
     29    private static final EastNorth[] EMPTY_DIRECTION = new EastNorth[0];
    2830
    2931    /**
    3032     * Constructs a new {@code AlignGridRotationAction}.
    … …  
    4345     */
    4446    static EastNorth[] getSelectedDirection(DataSet ds) {
    4547        if (ds == null) {
    46             return null;
     48            return EMPTY_DIRECTION;
    4749        }
    48         Collection<Way> ways = ds.getSelectedWays();
    49         Collection<Node> nodes = ds.getSelectedNodes();
     50        final Collection<Way> ways = ds.getSelectedWays();
     51        final Collection<Node> nodes = ds.getSelectedNodes();
     52       
     53        final int waysSize = ways.size();
     54        final int nodesSize = nodes.size();
     55       
    5056        Node a = null;
    5157        Node b = null;
    52         if (ways.size() == 1 && nodes.isEmpty()) {
    53             Way w = ways.iterator().next();
     58        if (waysSize == 1 && nodesSize ==0) {
     59            final Way w = ways.iterator().next();
    5460            if (w.getNodesCount() >= 2) {
    5561                a = w.firstNode();
    5662                b = w.isClosed() ? w.getNode(1) : w.lastNode();
    5763            }
    58         } else if (nodes.size() == 2 && ways.isEmpty()) {
    59             Iterator<Node> it = nodes.iterator();
     64        } else if (nodesSize == 2 && waysSize ==0) {
     65            final Iterator<Node> it = nodes.iterator();
    6066            a = it.next();
    6167            b = it.next();
    6268        }
    6369        if (a == null || b == null || !a.isLatLonKnown() || !b.isLatLonKnown()) {
    64             return null;
     70            return EMPTY_DIRECTION;
    6571        }
    66         EastNorth[] result = {a.getEastNorth(), b.getEastNorth()};
    67         return result[0].equalsEpsilon(result[1], 1e-9) ? null : result;
     72       
     73        final EastNorth[] result = {a.getEastNorth(), b.getEastNorth()};
     74        return result[0].equalsEpsilon(result[1], 1e-9) ? EMPTY_DIRECTION : result;
    6875    }
    6976
    7077    /**
    … …  
    8592
    8693    @Override
    8794    protected void updateEnabledState() {
    88         setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()) != null);
     95        setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()).length > 0);
    8996    }
    9097
    9198    @Override
    … …  
    95102
    96103    @Override
    97104    public void actionPerformed(ActionEvent e) {
    98         EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet());
    99         if (direction == null) {
     105        final EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet());
     106        if (direction.length == 0) {
    100107            return;
    101108        }
    102109        MapGridPaintable.ROTATION.put(rotationOf(direction[0], direction[1]));
  • test/unit/org/openstreetmap/josm/actions/GridActionsTest.java

    diff --git test/unit/org/openstreetmap/josm/actions/GridActionsTest.java test/unit/org/openstreetmap/josm/actions/GridActionsTest.java
    index 37e9d66..557abca 100644
     
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.actions;
    33
     4import static org.junit.Assert.assertArrayEquals;
    45import static org.junit.jupiter.api.Assertions.assertEquals;
    56import static org.junit.jupiter.api.Assertions.assertFalse;
    67import static org.junit.jupiter.api.Assertions.assertNull;
    … …  
    164165            ds.addPrimitive(w);
    165166
    166167            ds.setSelected();
    167             assertNull(AlignGridRotationAction.getSelectedDirection(ds));
     168            assertArrayEquals(new EastNorth[0], AlignGridRotationAction.getSelectedDirection(ds));
    168169            AlignGridRotationAction action = new AlignGridRotationAction();
    169170            assertFalse(action.isEnabled());
    170171            ds.setSelected(a);