Changeset 17374 in josm for trunk/src


Ignore:
Timestamp:
2020-11-29T10:33:58+01:00 (3 years ago)
Author:
GerdP
Message:

see #20167: [patch] Improve code readability by replacing indexed loops with foreach
Patch by gaben, slightly modified
I removed the changes for

  • GpxImageCorrelation.java, they introduce a TODO
  • ConnectivityRelations.java (no improvement in readability)
Location:
trunk/src/org/openstreetmap/josm
Files:
13 edited

Legend:

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

    r17358 r17374  
    708708        //  see #9599
    709709        if (discardedWays.stream().anyMatch(w -> !w.isNew())) {
    710             for (int i = 0; i < boundaries.size(); i++) {
    711                 AssembledPolygon ring = boundaries.get(i);
     710            for (AssembledPolygon ring : boundaries) {
    712711                for (int k = 0; k < ring.ways.size(); k++) {
    713712                    WayInPolygon ringWay = ring.ways.get(k);
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r17164 r17374  
    11241124
    11251125            if (cyclePrims && shift) {
    1126                 for (Iterator<OsmPrimitive> i = cycleList.iterator(); i.hasNext();) {
    1127                     nxt = i.next();
     1126                for (OsmPrimitive osmPrimitive : cycleList) {
     1127                    nxt = osmPrimitive;
    11281128                    if (!nxt.isSelected()) {
    11291129                        break; // take first primitive in cycleList not in sel
  • trunk/src/org/openstreetmap/josm/data/cache/HostLimitQueue.java

    r15525 r17374  
    44import java.io.IOException;
    55import java.net.URL;
    6 import java.util.Iterator;
    76import java.util.Map;
    87import java.util.concurrent.ConcurrentHashMap;
     
    5352
    5453    private JCSCachedTileLoaderJob<?, ?> findJob() {
    55         for (Iterator<Runnable> it = iterator(); it.hasNext();) {
    56             Runnable r = it.next();
     54        for (Runnable r : this) {
    5755            if (r instanceof JCSCachedTileLoaderJob) {
    5856                JCSCachedTileLoaderJob<?, ?> job = (JCSCachedTileLoaderJob<?, ?>) r;
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFile.java

    r17333 r17374  
    263263    private static NTV2SubGrid getSubGrid(NTV2SubGrid[] topLevelSubGrid, double lon, double lat) {
    264264        NTV2SubGrid sub = null;
    265         for (int i = 0; i < topLevelSubGrid.length; i++) {
    266             sub = topLevelSubGrid[i].getSubGridForCoord(lon, lat);
     265        for (NTV2SubGrid topLevel : topLevelSubGrid) {
     266            sub = topLevel.getSubGridForCoord(lon, lat);
    267267            if (sub != null) {
    268268                break;
     
    274274    @Override
    275275    public String toString() {
    276         return new StringBuilder(256)
    277             .append("Headers  : ")
    278             .append(overviewHeaderCount)
    279             .append("\nSub Hdrs : ")
    280             .append(subGridHeaderCount)
    281             .append("\nSub Grids: ")
    282             .append(subGridCount)
    283             .append("\nType     : ")
    284             .append(shiftType)
    285             .append("\nVersion  : ")
    286             .append(version)
    287             .append("\nFr Ellpsd: ")
    288             .append(fromEllipsoid)
    289             .append("\nTo Ellpsd: ")
    290             .append(toEllipsoid)
    291             .append("\nFr Maj Ax: ")
    292             .append(fromSemiMajorAxis)
    293             .append("\nFr Min Ax: ")
    294             .append(fromSemiMinorAxis)
    295             .append("\nTo Maj Ax: ")
    296             .append(toSemiMajorAxis)
    297             .append("\nTo Min Ax: ")
    298             .append(toSemiMinorAxis)
    299             .toString();
     276        char endl = '\n';
     277        return "Headers  : " + overviewHeaderCount + endl +
     278                "Sub Hdrs : " + subGridHeaderCount + endl +
     279                "Sub Grids: " + subGridCount + endl +
     280                "Type     : " + shiftType + endl +
     281                "Version  : " + version + endl +
     282                "Fr Ellpsd: " + fromEllipsoid + endl +
     283                "To Ellpsd: " + toEllipsoid + endl +
     284                "Fr Maj Ax: " + fromSemiMajorAxis + endl +
     285                "Fr Min Ax: " + fromSemiMinorAxis + endl +
     286                "To Maj Ax: " + toSemiMajorAxis + endl +
     287                "To Min Ax: " + toSemiMinorAxis;
    300288    }
    301289
  • trunk/src/org/openstreetmap/josm/data/validation/routines/UrlValidator.java

    r13590 r17374  
    276276            }
    277277            allowedSchemes = new HashSet<>(schemes.length);
    278             for (int i = 0; i < schemes.length; i++) {
    279                 allowedSchemes.add(schemes[i].toLowerCase(Locale.ENGLISH));
     278            for (String scheme : schemes) {
     279                allowedSchemes.add(scheme.toLowerCase(Locale.ENGLISH));
    280280            }
    281281        }
  • trunk/src/org/openstreetmap/josm/data/validation/tests/ConnectivityRelations.java

    r16643 r17374  
    8080        final String joined = cnTag.replace("bw", Integer.toString(BW));
    8181
    82 
    8382        final Map<Integer, Map<Integer, Boolean>> result = new HashMap<>();
    8483        String[] lanes = joined.split("\\|", -1);
    8584        for (int i = 0; i < lanes.length; i++) {
    86             String[] lane = lanes[i].split(":", -1);
     85            final String[] lane = lanes[i].split(":", -1);
    8786            int laneNumber;
    8887            //Ignore connections from bw, since we cannot derive a lane number from bw
  • trunk/src/org/openstreetmap/josm/data/validation/tests/DuplicateNode.java

    r17096 r17374  
    156156     * Returns the list of "duplicate nodes" errors for the given selection of node and parent test
    157157     * @param parentTest The parent test of returned errors
    158      * @param nodes The nodes selction to look into
     158     * @param nodes The nodes selection to look into
    159159     * @return the list of "duplicate nodes" errors
    160160     */
     
    187187                                Way w = (Way) sp;
    188188                                Map<String, String> keys = w.getKeys();
    189                                 for (Iterator<Entry<String, Boolean>> itt = typeMap.entrySet().iterator(); itt.hasNext();) {
    190                                     Entry<String, Boolean> e = itt.next();
     189                                for (Entry<String, Boolean> e : typeMap.entrySet()) {
    191190                                    if (keys.containsKey(e.getKey())) {
    192191                                        e.setValue(Boolean.TRUE);
  • trunk/src/org/openstreetmap/josm/data/validation/tests/SimilarNamedWays.java

    r15079 r17374  
    220220            StringBuilder expression = new StringBuilder();
    221221            int maxLength = 0;
    222             for (int i = 0; i < words.length; i++) {
    223                 if (words[i].length() > maxLength) {
    224                     maxLength = words[i].length();
     222            for (String word : words) {
     223                if (word.length() > maxLength) {
     224                    maxLength = word.length();
    225225                }
    226226                if (expression.length() > 0) {
    227227                    expression.append('|');
    228228                }
    229                 expression.append(Pattern.quote(words[i]));
     229                expression.append(Pattern.quote(word));
    230230            }
    231231            this.regExpr = Pattern.compile(expression.toString(), CASE_INSENSITIVE + UNICODE_CASE);
     
    243243            // which word matches?
    244244            String part = "";
    245             for (int i = 0; i < words.length; i++) {
    246                 String word = words[i];
     245            for (String word : words) {
    247246                if (start + word.length() <= name.length()) {
    248247                    part = name.substring(start, start + word.length());
  • trunk/src/org/openstreetmap/josm/gui/dialogs/CommandStackDialog.java

    r17347 r17374  
    277277        List<Command> undoCommands = UndoRedoHandler.getInstance().getUndoCommands();
    278278        undoRoot = new DefaultMutableTreeNode();
    279         for (int i = 0; i < undoCommands.size(); ++i) {
    280             undoRoot.add(getNodeForCommand(undoCommands.get(i)));
     279        for (Command undoCommand : undoCommands) {
     280            undoRoot.add(getNodeForCommand(undoCommand));
    281281        }
    282282        undoTreeModel.setRoot(undoRoot);
     
    286286        List<Command> redoCommands = UndoRedoHandler.getInstance().getRedoCommands();
    287287        redoRoot = new DefaultMutableTreeNode();
    288         for (int i = 0; i < redoCommands.size(); ++i) {
    289             redoRoot.add(getNodeForCommand(redoCommands.get(i)));
     288        for (Command redoCommand : redoCommands) {
     289            redoRoot.add(getNodeForCommand(redoCommand));
    290290        }
    291291        redoTreeModel.setRoot(redoRoot);
     
    341341        if (c.getChildren() != null) {
    342342            List<PseudoCommand> children = new ArrayList<>(c.getChildren());
    343             for (int i = 0; i < children.size(); ++i) {
    344                 node.add(getNodeForCommand(children.get(i)));
     343            for (PseudoCommand child : children) {
     344                node.add(getNodeForCommand(child));
    345345            }
    346346        }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/DialogsPanel.java

    r16438 r17374  
    194194         */
    195195        if (action == Action.RESTORE_SAVED || action == Action.ELEMENT_SHRINKS) {
    196             for (int i = 0; i < n; ++i) {
    197                 final ToggleDialog dlg = allDialogs.get(i);
     196            for (final ToggleDialog dlg : allDialogs) {
    198197                if (dlg.isDialogInDefaultView()) {
    199198                    final int ph = action == Action.RESTORE_SAVED ? dlg.getLastHeight() : dlg.getPreferredHeight();
     
    251250            int dm = 0;        // additional space needed by the small dialogs
    252251            int dp = 0;        // available space from the large dialogs
    253             for (int i = 0; i < n; ++i) {
    254                 final ToggleDialog dlg = allDialogs.get(i);
     252            for (final ToggleDialog dlg : allDialogs) {
    255253                if (dlg != triggeredBy && dlg.isDialogInDefaultView()) {
    256254                    final int ha = dlg.getSize().height;                              // current
     
    266264            }
    267265            /** adjust, without changing the sum */
    268             for (int i = 0; i < n; ++i) {
    269                 final ToggleDialog dlg = allDialogs.get(i);
     266            for (final ToggleDialog dlg : allDialogs) {
    270267                if (dlg != triggeredBy && dlg.isDialogInDefaultView()) {
    271268                    final int ha = dlg.getHeight();
     
    276273                        dlg.setPreferredSize(new Dimension(Integer.MAX_VALUE, hn));
    277274                    } else {
    278                         int d = dp == 0 ? 0 : ((h0-he) * dm / dp);
     275                        int d = dp == 0 ? 0 : ((h0 - he) * dm / dp);
    279276                        dlg.setPreferredSize(new Dimension(Integer.MAX_VALUE, h0 - d));
    280277                    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/placement/CompletelyInsideAreaStrategy.java

    r16488 r17374  
    4040        // Centroids are not optimal either, just imagine a U-shaped house.
    4141
    42         Rectangle pb = path.getBounds();
     42        final Rectangle pb = path.getBounds();
    4343
    4444        // quick check to see if label box is smaller than primitive box
     
    5656        final int nbh = (int) nb.getHeight();
    5757
    58         Rectangle centeredNBounds = new Rectangle(x2, y2, nbw, nbh);
     58        final Rectangle centeredNBounds = new Rectangle(x2, y2, nbw, nbh);
    5959
    6060        // slower check to see if label is displayed inside primitive shape
     
    8585        // Dumb algorithm to find a better placement. We could surely find a smarter one but it should
    8686        // solve most of building issues with only few calculations (8 at most)
    87         for (int i = 0; i < candidates.length; i++) {
    88             centeredNBounds = candidates[i];
    89             if (path.contains(centeredNBounds)) {
    90                 return centerOf(path.getMapViewState(), centeredNBounds);
     87        for (Rectangle candidate : candidates) {
     88            if (path.contains(candidate)) {
     89                return centerOf(path.getMapViewState(), candidate);
    9190            }
    9291        }
  • trunk/src/org/openstreetmap/josm/tools/ListenerList.java

    r15726 r17374  
    55import java.text.MessageFormat;
    66import java.util.HashMap;
    7 import java.util.Iterator;
    87import java.util.Objects;
    98import java.util.concurrent.CopyOnWriteArrayList;
     
    156155            eventFirerer.fire(l);
    157156        }
    158         for (Iterator<WeakListener<T>> iterator = weakListeners.iterator(); iterator.hasNext();) {
    159             WeakListener<T> weakLink = iterator.next();
     157        for (WeakListener<T> weakLink : weakListeners) {
    160158            T l = weakLink.listener.get();
    161159            if (l != null) {
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r16955 r17374  
    15101510        }
    15111511        Bidi.reorderVisually(levels, 0, dirStrings, 0, levels.length);
    1512         for (int i = 0; i < dirStrings.length; ++i) {
    1513             char[] chars = dirStrings[i].str.toCharArray();
    1514             gvs.add(font.layoutGlyphVector(frc, chars, 0, chars.length, dirStrings[i].direction));
     1512        for (DirectionString dirString : dirStrings) {
     1513            char[] chars = dirString.str.toCharArray();
     1514            gvs.add(font.layoutGlyphVector(frc, chars, 0, chars.length, dirString.direction));
    15151515        }
    15161516        return gvs;
Note: See TracChangeset for help on using the changeset viewer.