Changeset 7090 in josm


Ignore:
Timestamp:
2014-05-09T15:47:48+02:00 (10 years ago)
Author:
Don-vip
Message:

code refactoring to avoid classes in methods (cause problems for code analysis tools)

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    r7081 r7090  
    3636import java.util.concurrent.Executors;
    3737import java.util.concurrent.Future;
     38
    3839import javax.swing.AbstractButton;
    3940import javax.swing.FocusManager;
     41
    4042import org.openstreetmap.josm.Main;
    4143import org.openstreetmap.josm.data.Bounds;
     
    8385    static {
    8486        noThreads = Main.pref.getInteger(
    85                 "mappaint.StyledMapRenderer.style_creation.numberOfThreads", 
     87                "mappaint.StyledMapRenderer.style_creation.numberOfThreads",
    8688                Runtime.getRuntime().availableProcessors());
    8789        styleCreatorPool = noThreads <= 1 ? null : Executors.newFixedThreadPool(noThreads);
     
    240242    /**
    241243     * Joined List build from two Lists (read-only).
    242      * 
     244     *
    243245     * Extremely simple single-purpose implementation.
    244      * @param <T> 
     246     * @param <T>
    245247     */
    246248    public static class CompositeList<T> extends AbstractList<T> {
    247         List<T> a,b;
    248 
    249         public CompositeList(List<T> a, List<T> b) {
     249        List<? extends T> a,b;
     250
     251        /**
     252         * Constructs a new {@code CompositeList} from two lists.
     253         * @param a First list
     254         * @param b Second list
     255         */
     256        public CompositeList(List<? extends T> a, List<? extends T> b) {
    250257            this.a = a;
    251258            this.b = b;
     
    293300    }
    294301
    295     private ElemStyles styles;
    296302    private double circum;
    297303
     
    13401346    }
    13411347
     1348    private class ComputeStyleListWorker implements Callable<List<StyleRecord>>, Visitor {
     1349        private final List<? extends OsmPrimitive> input;
     1350        private final int from;
     1351        private final int to;
     1352        private final List<StyleRecord> output;
     1353        private final DataSet data;
     1354
     1355        private final ElemStyles styles = MapPaintStyles.getStyles();
     1356
     1357        private final boolean drawArea = circum <= Main.pref.getInteger("mappaint.fillareas", 10000000);
     1358        private final boolean drawMultipolygon = drawArea && Main.pref.getBoolean("mappaint.multipolygon", true);
     1359        private final boolean drawRestriction = Main.pref.getBoolean("mappaint.restriction", true);
     1360
     1361        /**
     1362         * Constructor for CreateStyleRecordsWorker.
     1363         * @param input the primitives to process
     1364         * @param from first index of <code>input</code> to use
     1365         * @param to last index + 1
     1366         */
     1367        public ComputeStyleListWorker(final List<? extends OsmPrimitive> input, int from, int to, List<StyleRecord> output, DataSet data) {
     1368            this.input = input;
     1369            this.from = from;
     1370            this.to = to;
     1371            this.output = output;
     1372            this.data = data;
     1373            this.styles.setDrawMultipolygon(drawMultipolygon);
     1374        }
     1375
     1376        @Override
     1377        public List<StyleRecord> call() throws Exception {
     1378            for (int i = from; i<to; i++) {
     1379                OsmPrimitive osm = input.get(i);
     1380                if (osm.isDrawable()) {
     1381                    osm.accept(this);
     1382                }
     1383            }
     1384            return output;
     1385        }
     1386
     1387        @Override
     1388        public void visit(Node n) {
     1389            if (n.isDisabled()) {
     1390                add(n, FLAG_DISABLED);
     1391            } else if (data.isSelected(n)) {
     1392                add(n, FLAG_SELECTED);
     1393            } else if (n.isMemberOfSelected()) {
     1394                add(n, FLAG_MEMBER_OF_SELECTED);
     1395            } else {
     1396                add(n, FLAG_NORMAL);
     1397            }
     1398        }
     1399
     1400        @Override
     1401        public void visit(Way w) {
     1402            if (w.isDisabled()) {
     1403                add(w, FLAG_DISABLED);
     1404            } else if (data.isSelected(w)) {
     1405                add(w, FLAG_SELECTED);
     1406            } else if (w.isMemberOfSelected()) {
     1407                add(w, FLAG_MEMBER_OF_SELECTED);
     1408            } else {
     1409                add(w, FLAG_NORMAL);
     1410            }
     1411        }
     1412
     1413        @Override
     1414        public void visit(Relation r) {
     1415            if (r.isDisabled()) {
     1416                add(r, FLAG_DISABLED);
     1417            } else if (data.isSelected(r)) {
     1418                add(r, FLAG_SELECTED);
     1419            } else {
     1420                add(r, FLAG_NORMAL);
     1421            }
     1422        }
     1423
     1424        @Override
     1425        public void visit(Changeset cs) {
     1426            throw new UnsupportedOperationException();
     1427        }
     1428
     1429        public void add(Node osm, int flags) {
     1430            StyleList sl = styles.get(osm, circum, nc);
     1431            for (ElemStyle s : sl) {
     1432                output.add(new StyleRecord(s, osm, flags));
     1433            }
     1434        }
     1435
     1436        public void add(Relation osm, int flags) {
     1437            StyleList sl = styles.get(osm, circum, nc);
     1438            for (ElemStyle s : sl) {
     1439                if (drawMultipolygon && drawArea && s instanceof AreaElemStyle && (flags & FLAG_DISABLED) == 0) {
     1440                    output.add(new StyleRecord(s, osm, flags));
     1441                } else if (drawRestriction && s instanceof NodeElemStyle) {
     1442                    output.add(new StyleRecord(s, osm, flags));
     1443                }
     1444            }
     1445        }
     1446
     1447        public void add(Way osm, int flags) {
     1448            StyleList sl = styles.get(osm, circum, nc);
     1449            for (ElemStyle s : sl) {
     1450                if (!(drawArea && (flags & FLAG_DISABLED) == 0) && s instanceof AreaElemStyle) {
     1451                    continue;
     1452                }
     1453                output.add(new StyleRecord(s, osm, flags));
     1454            }
     1455        }
     1456    }
     1457
     1458    private class ConcurrentTasksHelper {
     1459
     1460        private final List<StyleRecord> allStyleElems;
     1461        private final DataSet data;
     1462
     1463        public ConcurrentTasksHelper(List<StyleRecord> allStyleElems, DataSet data) {
     1464            this.allStyleElems = allStyleElems;
     1465            this.data = data;
     1466        }
     1467
     1468        void process(List<? extends OsmPrimitive> prims) {
     1469            final List<ComputeStyleListWorker> tasks = new ArrayList<>();
     1470            final int bucketsize = Math.max(100, prims.size()/noThreads/3);
     1471            final int noBuckets = (prims.size() + bucketsize - 1) / bucketsize;
     1472            final boolean singleThread = noThreads == 1 || noBuckets == 1;
     1473            for (int i=0; i<noBuckets; i++) {
     1474                int from = i*bucketsize;
     1475                int to = Math.min((i+1)*bucketsize, prims.size());
     1476                List<StyleRecord> target = singleThread ? allStyleElems : new ArrayList<StyleRecord>(to - from);
     1477                tasks.add(new ComputeStyleListWorker(prims, from, to, target, data));
     1478            }
     1479            if (singleThread) {
     1480                try {
     1481                    for (ComputeStyleListWorker task : tasks) {
     1482                        task.call();
     1483                    }
     1484                } catch (Exception ex) {
     1485                    throw new RuntimeException(ex);
     1486                }
     1487            } else if (tasks.size() > 1) {
     1488                try {
     1489                    for (Future<List<StyleRecord>> future : styleCreatorPool.invokeAll(tasks)) {
     1490                            allStyleElems.addAll(future.get());
     1491                    }
     1492                } catch (InterruptedException | ExecutionException ex) {
     1493                    throw new RuntimeException(ex);
     1494                }
     1495            }
     1496        }
     1497    }
     1498
    13421499    @Override
    13431500    public void render(final DataSet data, boolean renderVirtualNodes, Bounds bounds) {
     
    13451502        getSettings(renderVirtualNodes);
    13461503
    1347         final boolean drawArea = circum <= Main.pref.getInteger("mappaint.fillareas", 10000000);
    1348         final boolean drawMultipolygon = drawArea && Main.pref.getBoolean("mappaint.multipolygon", true);
    1349         final boolean drawRestriction = Main.pref.getBoolean("mappaint.restriction", true);
    1350 
    1351         styles = MapPaintStyles.getStyles();
    1352         styles.setDrawMultipolygon(drawMultipolygon);
    1353        
    13541504        highlightWaySegments = data.getHighlightedWaySegments();
    13551505
     
    13611511        }
    13621512
    1363         class ComputeStyleListWorker implements Callable<List<StyleRecord>>, Visitor {
    1364             private final List<? extends OsmPrimitive> input;
    1365             private final int from;
    1366             private final int to;
    1367             private final List<StyleRecord> output;
    1368 
    1369             /**
    1370              * Constructor for CreateStyleRecordsWorker.
    1371              * @param input the primitives to process
    1372              * @param from first index of <code>input</code> to use
    1373              * @param to last index + 1
    1374              */
    1375             public ComputeStyleListWorker(final List<? extends OsmPrimitive> input, int from, int to, List<StyleRecord> output) {
    1376                 this.input = input;
    1377                 this.from = from;
    1378                 this.to = to;
    1379                 this.output = output;
    1380             }
    1381 
    1382             @Override
    1383             public List<StyleRecord> call() throws Exception {
    1384                 for (int i = from; i<to; i++) {
    1385                     OsmPrimitive osm = input.get(i);
    1386                     if (osm.isDrawable()) {
    1387                         osm.accept(this);
    1388                     }
    1389                 }
    1390                 return output;
    1391             }
    1392 
    1393             @Override
    1394             public void visit(Node n) {
    1395                 if (n.isDisabled()) {
    1396                     add(n, FLAG_DISABLED);
    1397                 } else if (data.isSelected(n)) {
    1398                     add(n, FLAG_SELECTED);
    1399                 } else if (n.isMemberOfSelected()) {
    1400                     add(n, FLAG_MEMBER_OF_SELECTED);
    1401                 } else {
    1402                     add(n, FLAG_NORMAL);
    1403                 }
    1404             }
    1405 
    1406             @Override
    1407             public void visit(Way w) {
    1408                 if (w.isDisabled()) {
    1409                     add(w, FLAG_DISABLED);
    1410                 } else if (data.isSelected(w)) {
    1411                     add(w, FLAG_SELECTED);
    1412                 } else if (w.isMemberOfSelected()) {
    1413                     add(w, FLAG_MEMBER_OF_SELECTED);
    1414                 } else {
    1415                     add(w, FLAG_NORMAL);
    1416                 }
    1417             }
    1418 
    1419             @Override
    1420             public void visit(Relation r) {
    1421                 if (r.isDisabled()) {
    1422                     add(r, FLAG_DISABLED);
    1423                 } else if (data.isSelected(r)) {
    1424                     add(r, FLAG_SELECTED);
    1425                 } else {
    1426                     add(r, FLAG_NORMAL);
    1427                 }
    1428             }
    1429 
    1430             @Override
    1431             public void visit(Changeset cs) {
    1432                 throw new UnsupportedOperationException();
    1433             }
    1434 
    1435             public void add(Node osm, int flags) {
    1436                 StyleList sl = styles.get(osm, circum, nc);
    1437                 for (ElemStyle s : sl) {
    1438                     output.add(new StyleRecord(s, osm, flags));
    1439                 }
    1440             }
    1441 
    1442             public void add(Relation osm, int flags) {
    1443                 StyleList sl = styles.get(osm, circum, nc);
    1444                 for (ElemStyle s : sl) {
    1445                     if (drawMultipolygon && drawArea && s instanceof AreaElemStyle && (flags & FLAG_DISABLED) == 0) {
    1446                         output.add(new StyleRecord(s, osm, flags));
    1447                     } else if (drawRestriction && s instanceof NodeElemStyle) {
    1448                         output.add(new StyleRecord(s, osm, flags));
    1449                     }
    1450                 }
    1451             }
    1452 
    1453             public void add(Way osm, int flags) {
    1454                 StyleList sl = styles.get(osm, circum, nc);
    1455                 for (ElemStyle s : sl) {
    1456                     if (!(drawArea && (flags & FLAG_DISABLED) == 0) && s instanceof AreaElemStyle) {
    1457                         continue;
    1458                     }
    1459                     output.add(new StyleRecord(s, osm, flags));
    1460                 }
    1461             }
    1462         }
    14631513        List<Node> nodes = data.searchNodes(bbox);
    14641514        List<Way> ways = data.searchWays(bbox);
    14651515        List<Relation> relations = data.searchRelations(bbox);
    1466        
     1516
    14671517        final List<StyleRecord> allStyleElems = new ArrayList<>(nodes.size()+ways.size()+relations.size());
    14681518
    1469         class ConcurrentTasksHelper {
    1470 
    1471             void process(List<? extends OsmPrimitive> prims) {
    1472                 final List<ComputeStyleListWorker> tasks = new ArrayList<>();
    1473                 final int bucketsize = Math.max(100, prims.size()/noThreads/3);
    1474                 final int noBuckets = (prims.size() + bucketsize - 1) / bucketsize;
    1475                 final boolean singleThread = noThreads == 1 || noBuckets == 1;
    1476                 for (int i=0; i<noBuckets; i++) {
    1477                     int from = i*bucketsize;
    1478                     int to = Math.min((i+1)*bucketsize, prims.size());
    1479                     List<StyleRecord> target = singleThread ? allStyleElems : new ArrayList<StyleRecord>(to - from);
    1480                     tasks.add(new ComputeStyleListWorker(prims, from, to, target));
    1481                 }
    1482                 if (singleThread) {
    1483                     try {
    1484                         for (ComputeStyleListWorker task : tasks) {
    1485                             task.call();
    1486                         }
    1487                     } catch (Exception ex) {
    1488                         throw new RuntimeException(ex);
    1489                     }
    1490                 } else if (tasks.size() > 1) {
    1491                     try {
    1492                         for (Future<List<StyleRecord>> future : styleCreatorPool.invokeAll(tasks)) {
    1493                                 allStyleElems.addAll(future.get());
    1494                         }
    1495                     } catch (InterruptedException | ExecutionException ex) {
    1496                         throw new RuntimeException(ex);
    1497                     }
    1498                 }
    1499             }
    1500         }
    1501         ConcurrentTasksHelper helper = new ConcurrentTasksHelper();
     1519        ConcurrentTasksHelper helper = new ConcurrentTasksHelper(allStyleElems, data);
    15021520
    15031521        // Need to process all relations first.
    15041522        // Reason: Make sure, ElemStyles.getStyleCacheWithRange is
    1505         // not called for the same primtive in parallel threads.
     1523        // not called for the same primitive in parallel threads.
    15061524        // (Could be synchronized, but try to avoid this for
    15071525        // performance reasons.)
    15081526        helper.process(relations);
    1509         @SuppressWarnings("unchecked")
    1510         List<OsmPrimitive> nodesAndWays = (List) new CompositeList(nodes, ways);
    1511         helper.process(nodesAndWays);
    1512        
     1527        helper.process(new CompositeList<>(nodes, ways));
     1528
    15131529        if (Main.isTraceEnabled()) {
    15141530            timePhase1 = System.currentTimeMillis();
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r7005 r7090  
    126126        return group != null ? group.getRawName() + "/" + name : name;
    127127    }
    128    
     128
    129129    /**
    130130     * Returns the preset icon.
     
    254254            link.addToPanel(p, selected, presetInitiallyMatches);
    255255        }
    256        
     256
    257257        // "Add toolbar button"
    258258        JToggleButton tb = new JToggleButton(new ToolbarButtonAction());
     
    320320    }
    321321
    322     public int showDialog(Collection<OsmPrimitive> sel, final boolean showNewRelation) {
     322    private static class PresetDialog extends ExtendedDialog {
     323        public PresetDialog(Component content, String title, ImageIcon icon, boolean disableApply, boolean showNewRelation) {
     324            super(Main.parent, title,
     325                    showNewRelation?
     326                            new String[] { tr("Apply Preset"), tr("New relation"), tr("Cancel") }:
     327                                new String[] { tr("Apply Preset"), tr("Cancel") },
     328                                true);
     329            if (icon != null)
     330                setIconImage(icon.getImage());
     331            contentInsets = new Insets(10,5,0,5);
     332            if (showNewRelation) {
     333                setButtonIcons(new String[] {"ok.png", "dialogs/addrelation.png", "cancel.png" });
     334            } else {
     335                setButtonIcons(new String[] {"ok.png", "cancel.png" });
     336            }
     337            setContent(content);
     338            setDefaultButton(1);
     339            setupDialog();
     340            buttons.get(0).setEnabled(!disableApply);
     341            buttons.get(0).setToolTipText(title);
     342            // Prevent dialogs of being too narrow (fix #6261)
     343            Dimension d = getSize();
     344            if (d.width < 350) {
     345                d.width = 350;
     346                setSize(d);
     347            }
     348            showDialog();
     349        }
     350    }
     351
     352    public int showDialog(Collection<OsmPrimitive> sel, boolean showNewRelation) {
    323353        PresetPanel p = createPanel(sel);
    324354        if (p == null)
     
    336366            }
    337367
    338             class PresetDialog extends ExtendedDialog {
    339                 public PresetDialog(Component content, String title, ImageIcon icon, boolean disableApply) {
    340                     super(Main.parent,
    341                             title,
    342                             showNewRelation?
    343                                     new String[] { tr("Apply Preset"), tr("New relation"), tr("Cancel") }:
    344                                         new String[] { tr("Apply Preset"), tr("Cancel") },
    345                                         true);
    346                     if (icon != null)
    347                         setIconImage(icon.getImage());
    348                     contentInsets = new Insets(10,5,0,5);
    349                     if (showNewRelation) {
    350                         setButtonIcons(new String[] {"ok.png", "dialogs/addrelation.png", "cancel.png" });
    351                     } else {
    352                         setButtonIcons(new String[] {"ok.png", "cancel.png" });
    353                     }
    354                     setContent(content);
    355                     setDefaultButton(1);
    356                     setupDialog();
    357                     buttons.get(0).setEnabled(!disableApply);
    358                     buttons.get(0).setToolTipText(title);
    359                     // Prevent dialogs of being too narrow (fix #6261)
    360                     Dimension d = getSize();
    361                     if (d.width < 350) {
    362                         d.width = 350;
    363                         setSize(d);
    364                     }
    365                     showDialog();
    366                 }
    367             }
    368 
    369             answer = new PresetDialog(p, title, (ImageIcon) getValue(Action.SMALL_ICON), sel.isEmpty()).getValue();
     368            answer = new PresetDialog(p, title, (ImageIcon) getValue(Action.SMALL_ICON),
     369                    sel.isEmpty(), showNewRelation).getValue();
    370370        }
    371371        if (!showNewRelation && answer == 2)
     
    502502        });
    503503    }
    504    
     504
    505505    /**
    506506     * Action that adds or removes the button on main toolbar
     
    522522        }
    523523    }
    524    
     524
    525525    public String getToolbarString() {
    526526        ToolbarPreferences.ActionDefinition aDef
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r7089 r7090  
    733733    }
    734734
     735    /** Quit parsing, when a certain condition is met */
     736    private static class SAXReturnException extends SAXException {
     737        private final String result;
     738
     739        public SAXReturnException(String result) {
     740            this.result = result;
     741        }
     742
     743        public String getResult() {
     744            return result;
     745        }
     746    }
     747
    735748    /**
    736749     * Reads the wiki page on a certain file in html format in order to find the real image URL.
    737750     */
    738751    private static String getImgUrlFromWikiInfoPage(final String base, final String fn) {
    739 
    740         /** Quit parsing, when a certain condition is met */
    741         class SAXReturnException extends SAXException {
    742             private final String result;
    743 
    744             public SAXReturnException(String result) {
    745                 this.result = result;
    746             }
    747 
    748             public String getResult() {
    749                 return result;
    750             }
    751         }
    752 
    753752        try {
    754753            final XMLReader parser = XMLReaderFactory.createXMLReader();
Note: See TracChangeset for help on using the changeset viewer.