Changeset 19655 in josm


Ignore:
Timestamp:
2026-10-22T17:13:42+02:00 (3 hours ago)
Author:
stoecker
Message:

fix #24951 - Fix Sonar issues in recent Grid Layer and OAuth - patches by DanProgs & wangi

Location:
trunk
Files:
6 edited

Legend:

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

    r19640 r19655  
    2727public class AlignGridRotationAction extends JosmAction {
    2828
     29    /** returned when the selection does not define a direction */
     30    private static final EastNorth[] EMPTY_DIRECTION = new EastNorth[0];
     31
    2932    /**
    3033     * Constructs a new {@code AlignGridRotationAction}.
    … …  
    4043     * Determines the direction given by the selection: a single way with at least two nodes, or exactly two nodes.
    4144     * @param ds the data set, may be null
    42      * @return start and end point, or {@code null} if the selection does not define a direction
     45     * @return start and end point, or an empty array if the selection does not define a direction
    4346     */
    4447    static EastNorth[] getSelectedDirection(DataSet ds) {
    4548        if (ds == null) {
    46             return null;
     49            return EMPTY_DIRECTION;
    4750        }
    4851        Collection<Way> ways = ds.getSelectedWays();
    … …  
    6265        }
    6366        if (a == null || b == null || !a.isLatLonKnown() || !b.isLatLonKnown()) {
    64             return null;
     67            return EMPTY_DIRECTION;
    6568        }
    6669        EastNorth[] result = {a.getEastNorth(), b.getEastNorth()};
    67         return result[0].equalsEpsilon(result[1], 1e-9) ? null : result;
     70        return result[0].equalsEpsilon(result[1], 1e-9) ? EMPTY_DIRECTION : result;
    6871    }
    6972
    … …  
    8689    @Override
    8790    protected void updateEnabledState() {
    88         setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()) != null);
     91        setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()).length == 2);
    8992    }
    9093
    … …  
    97100    public void actionPerformed(ActionEvent e) {
    98101        EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet());
    99         if (direction == null) {
     102        if (direction.length != 2) {
    100103            return;
    101104        }
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r19640 r19655  
    125125    public final MapView mapView;
    126126    /** The grid drawn over the map view, see {@link MapGridPaintable} */
    127     private final MapGridPaintable gridOverlay = new MapGridPaintable();
     127    private final transient MapGridPaintable gridOverlay = new MapGridPaintable();
    128128
    129129    /**
  • trunk/src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java

    r19640 r19655  
    1313import java.util.ArrayList;
    1414import java.util.List;
     15import java.util.concurrent.atomic.AtomicReference;
    1516
    1617import org.openstreetmap.josm.data.Bounds;
    1718import org.openstreetmap.josm.data.ProjectionBounds;
    1819import org.openstreetmap.josm.data.coor.EastNorth;
    19 import org.openstreetmap.josm.data.coor.ILatLon;
    2020import org.openstreetmap.josm.data.coor.LatLon;
    2121import org.openstreetmap.josm.data.preferences.BooleanProperty;
    … …  
    138138
    139139    /** written by {@link #paint} and read by {@link #preferenceChanged}, which may run on another thread */
    140     private volatile MapView mapView;
     140    private final AtomicReference<MapView> mapView = new AtomicReference<>();
    141141
    142142    /**
    … …  
    149149    @Override
    150150    public void paint(Graphics2D g, MapView mv, Bounds bbox) {
    151         mapView = mv;
     151        mapView.set(mv);
    152152        if (!Boolean.TRUE.equals(ENABLED.get()) || mv.getWidth() <= 0 || mv.getHeight() <= 0) {
    153153            return;
    … …  
    319319                return 1;
    320320            }
    321             double metres = a.greatCircleDistance((ILatLon) b);
     321            double metres = a.greatCircleDistance(b);
    322322            return metres > 0 && Double.isFinite(metres) ? step / metres : 1;
    323323        } catch (IllegalArgumentException e) {
    … …  
    358358            double rotationDegrees, double originX, double originY) {
    359359        List<ProjectedGridLine> lines = new ArrayList<>();
    360         if (!(spacingX > 0) || !(spacingY > 0)) {
     360        if (!isPositive(spacingX) || !isPositive(spacingY)) {
    361361            return lines;
    362362        }
    … …  
    421421            double originLon, double originLat, int segments) {
    422422        List<LatLonGridLine> lines = new ArrayList<>();
    423         if (!(spacingLon > 0) || !(spacingLat > 0) || segments < 1) {
     423        if (!isPositive(spacingLon) || !isPositive(spacingLat) || segments < 1) {
    424424            return lines;
    425425        }
    … …  
    446446        for (long k = kLonMin; k <= kLonMax; k++) {
    447447            double lon = LatLon.toIntervalLon(originLon + k * spacingLon);
    448             if (limitedLon && (lon < world.getMinLon() || lon > world.getMaxLon())) {
    449                 continue;
    450             }
    451             List<LatLon> line = new ArrayList<>(segments + 1);
    452             for (int i = 0; i <= segments; i++) {
    453                 line.add(new LatLon(minLat + (maxLat - minLat) * i / segments, lon));
    454             }
    455             lines.add(new LatLonGridLine(line, true));
     448            if (!limitedLon || (lon >= world.getMinLon() && lon <= world.getMaxLon())) {
     449                addMeridian(lines, lon, minLat, maxLat, segments);
     450            }
    456451        }
    457452        for (long k = kLatMin; k <= kLatMax; k++) {
    … …  
    463458        }
    464459        return lines;
     460    }
     461
     462    /**
     463     * Adds a meridian running from one latitude to another.
     464     * @param lines the list to add to
     465     * @param lon the longitude of the meridian
     466     * @param minLat the latitude to start at
     467     * @param maxLat the latitude to end at
     468     * @param segments number of segments of the polyline
     469     */
     470    private static void addMeridian(List<LatLonGridLine> lines, double lon, double minLat, double maxLat, int segments) {
     471        List<LatLon> line = new ArrayList<>(segments + 1);
     472        for (int i = 0; i <= segments; i++) {
     473            line.add(new LatLon(minLat + (maxLat - minLat) * i / segments, lon));
     474        }
     475        lines.add(new LatLonGridLine(line, true));
    465476    }
    466477
    … …  
    481492            return;
    482493        }
    483         if (!(maxLon > minLon)) {
     494        if (maxLon <= minLon) {
    484495            return;
    485496        }
    … …  
    489500        }
    490501        lines.add(new LatLonGridLine(line, false));
     502    }
     503
     504    /**
     505     * Determines if a value is strictly positive. Unlike {@code value <= 0}, this rejects NaN.
     506     * @param value the value
     507     * @return {@code true} if the value is greater than 0
     508     */
     509    private static boolean isPositive(double value) {
     510        return value > 0;
    491511    }
    492512
    … …  
    495515        if (e.getKey().startsWith(PREFIX) || e.getKey().equals(COLOR_EAST.getKey()) || e.getKey().equals(COLOR_NORTH.getKey())) {
    496516            invalidate();
    497             if (mapView != null) {
    498                 mapView.repaint();
     517            MapView mv = mapView.get();
     518            if (mv != null) {
     519                mv.repaint();
    499520            }
    500521        }
    … …  
    504525    public void destroy() {
    505526        Config.getPref().removePreferenceChangeListener(this);
    506         mapView = null;
     527        mapView.set(null);
    507528    }
    508529}
  • trunk/src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java

    r19640 r19655  
    2121public class ShowTileBordersAction extends AbstractAction implements LayerAction {
    2222
    23     private final AbstractTileSourceLayer<?> layer;
     23    private final transient AbstractTileSourceLayer<?> layer;
    2424
    2525    /**
  • trunk/src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java

    r19640 r19655  
    55
    66import java.awt.Component;
     7import java.awt.GridBagConstraints;
    78import java.awt.GridBagLayout;
    89import java.text.DecimalFormat;
    … …  
    101102        panel.add(enabled, GBC.eol().insets(0, 0, 0, 10));
    102103        panel.add(new JLabel(tr("Grid type")), GBC.std().insets(5, 0, 5, 5));
    103         panel.add(type, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 5));
     104        panel.add(type, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(0, 0, 0, 5));
    104105        panel.add(spacingXLabel, GBC.std().insets(5, 0, 5, 5));
    105106        panel.add(spacingX, GBC.eol().insets(0, 0, 0, 5));
    … …  
    117118        panel.add(lineWidth, GBC.eol().insets(0, 0, 0, 5));
    118119        panel.add(new JLabel(tr("Blend mode")), GBC.std().insets(5, 0, 5, 5));
    119         panel.add(blendMode, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 5));
    120         panel.add(GBC.glue(0, 0), GBC.eol().fill(GBC.BOTH));
     120        panel.add(blendMode, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(0, 0, 0, 5));
     121        panel.add(GBC.glue(0, 0), GBC.eol().fill(GridBagConstraints.BOTH));
    121122
    122123        createPreferenceTabWithScrollPane(gui, panel);
    … …  
    143144    /** Renders enum values with a translated name */
    144145    private static final class TranslatedRenderer<T> extends DefaultListCellRenderer {
    145         private final Function<T, String> name;
     146        private final transient Function<T, String> name;
    146147
    147148        TranslatedRenderer(Function<T, String> name) {
    … …  
    179180        try {
    180181            double value = FORMAT.parse(field.getText().trim()).doubleValue();
    181             if (positive && !(value > 0)) {
     182            if (positive && (Double.isNaN(value) || value <= 0)) {
    182183                Logging.warn("Ignoring invalid grid setting {0}: {1}", property.getKey(), field.getText());
    183184                return;
  • trunk/test/unit/org/openstreetmap/josm/actions/GridActionsTest.java

    r19640 r19655  
    165165
    166166            ds.setSelected();
    167             assertNull(AlignGridRotationAction.getSelectedDirection(ds));
     167            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
    168168            AlignGridRotationAction action = new AlignGridRotationAction();
    169169            assertFalse(action.isEnabled());
    170170            ds.setSelected(a);
    171             assertNull(AlignGridRotationAction.getSelectedDirection(ds));
     171            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
    172172            assertFalse(action.isEnabled());
    173173            ds.setSelected(a, b, c);
    174             assertNull(AlignGridRotationAction.getSelectedDirection(ds));
     174            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
    175175            assertFalse(action.isEnabled());
    176176
Note: See TracChangeset for help on using the changeset viewer.