Changeset 32974 in osm for applications/editors


Ignore:
Timestamp:
2016-09-11T13:47:38+02:00 (8 years ago)
Author:
nokutu
Message:

Fixed a couple of FindBugs issues and substituted some code blocks for lambdas. Still needs testing

Location:
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryAbstractImage.java

    r32593 r32974  
    1717 *
    1818 */
    19 public class MapillaryAbstractImage implements Comparable<MapillaryAbstractImage> {
     19public abstract class MapillaryAbstractImage implements Comparable<MapillaryAbstractImage> {
    2020  /**
    2121   * If two values for field ca differ by less than EPSILON both values are considered equal.
     
    291291  }
    292292
    293   @Override
     293  /*@Override
    294294  public int compareTo(MapillaryAbstractImage mapillaryAbstractImage) {
    295295    return hashCode() - mapillaryAbstractImage.hashCode();
    296296  }
     297
     298  @Override
     299  public boolean equals(Object obj) {
     300    return obj instanceof MapillaryAbstractImage && hashCode() == obj.hashCode();
     301  }*/
    297302}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryImage.java

    r32972 r32974  
    129129      return this.key.compareTo(((MapillaryImage) image).getKey());
    130130    }
    131     return super.compareTo(image);
     131    return hashCode() - image.hashCode();
    132132  }
    133133
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryImportedImage.java

    r32033 r32974  
    9090  @Override
    9191  public int compareTo(MapillaryAbstractImage image) {
    92     if (image instanceof MapillaryImage)
     92    if (image instanceof MapillaryImportedImage)
    9393      return this.file.compareTo(((MapillaryImportedImage) image).getFile());
    94     return super.compareTo(image);
     94    return hashCode() - image.hashCode();
    9595  }
    9696
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryLayer.java

    r32717 r32974  
    247247  @Override
    248248  public boolean isModified() {
    249     for (MapillaryAbstractImage image : this.data.getImages()) {
    250       if (image.isModified())
    251         return true;
    252     }
    253     return false;
     249    return this.data.getImages().parallelStream().anyMatch(MapillaryAbstractImage::isModified);
    254250  }
    255251
     
    257253  public void setVisible(boolean visible) {
    258254    super.setVisible(visible);
    259     for (MapillaryAbstractImage img : this.data.getImages()) {
    260       img.setVisible(visible);
    261     }
     255    this.data.getImages().parallelStream().forEach(img -> img.setVisible(visible));
    262256    if (Main.map != null) {
    263257      MapillaryFilterDialog.getInstance().refresh();
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryPlugin.java

    r32740 r32974  
    6363
    6464  /** Cache that stores the pictures the downloaded pictures. */
    65   public static CacheAccess<String, BufferedImageCacheEntry> cache;
     65  private static CacheAccess<String, BufferedImageCacheEntry> cache;
    6666
    6767  private static final MapillaryDownloadAction downloadAction = new MapillaryDownloadAction();
     
    137137   *          Required information of the plugin. Obtained from the jar file.
    138138   */
    139   public MapillaryPlugin(PluginInformation info) {
     139  public MapillaryPlugin(PluginInformation info) throws IOException {
    140140    super(info);
    141141
    142     try {
     142    if (cache == null) {
    143143      cache = JCSCacheManager.getCache("mapillary", 10, 10000, this.getPluginDir() + "/cache/");
    144     } catch (IOException e) {
    145       Main.error(e);
    146144    }
    147145
     
    286284    return new ImageProvider(s);
    287285  }
     286
     287  public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
     288    return cache;
     289  }
    288290}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillarySequence.java

    r32373 r32974  
    3838   * Creates a sequence object with the given parameters.
    3939   *
    40    * @param key       The unique identifier of the sequence.
     40   * @param key The unique identifier of the sequence.
    4141   * @param createdAt The date the sequence was created.
     42   *
    4243   * @throws IllegalArgumentException if the key is invalid
    43    *           according to {@link ValidationUtil#validateSequenceKey(String)}
     44   * according to {@link ValidationUtil#validateSequenceKey(String)}
    4445   */
    4546  public MapillarySequence(String key, long createdAt) {
     
    6667   */
    6768  public synchronized void add(List<MapillaryAbstractImage> images) {
    68     for (MapillaryAbstractImage image : images) {
    69       add(image);
    70     }
     69    images.forEach(this::add);
    7170  }
    7271
     
    108107   *
    109108   * @param image The {@link MapillaryAbstractImage} object whose next image is
    110    *              going to be returned.
     109   * going to be returned.
     110   *
    111111   * @return The next {@link MapillaryAbstractImage} object in the sequence.
     112   *
    112113   * @throws IllegalArgumentException if the given {@link MapillaryAbstractImage} object doesn't belong
    113    *                                  the this sequence.
     114   * the this sequence.
    114115   */
    115116  public MapillaryAbstractImage next(MapillaryAbstractImage image) {
     
    129130   *
    130131   * @param image The {@link MapillaryAbstractImage} object whose previous image is
    131    *              going to be returned.
     132   * going to be returned.
     133   *
    132134   * @return The previous {@link MapillaryAbstractImage} object in the sequence.
     135   *
    133136   * @throws IllegalArgumentException if the given {@link MapillaryAbstractImage} object doesn't belong
    134    *                                  the this sequence.
     137   * the this sequence.
    135138   */
    136139  public MapillaryAbstractImage previous(MapillaryAbstractImage image) {
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillarySign.java

    r32973 r32974  
    66import java.io.IOException;
    77import java.io.InputStreamReader;
     8import java.nio.charset.Charset;
    89import java.util.HashMap;
    910import java.util.Map;
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/actions/MapillaryExportAction.java

    r32373 r32974  
    9898      }
    9999      try {
    100         Main.worker.submit(new Thread(new MapillaryExportManager(images)));
     100        Main.worker.submit(new MapillaryExportManager(images));
    101101      } catch (IOException e1) {
    102102        Main.error(e1);
     
    113113   */
    114114  public void export(Set<MapillaryAbstractImage> images) {
    115     Main.worker.submit(new Thread(new MapillaryExportManager(images,
    116         this.dialog.chooser.getSelectedFile().toString())));
     115    Main.worker.submit(new MapillaryExportManager(images,
     116        this.dialog.chooser.getSelectedFile().toString()));
    117117  }
    118118
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/actions/MapillaryImportIntoSequenceAction.java

    r32593 r32974  
    88import java.io.File;
    99import java.io.IOException;
     10import java.io.Serializable;
    1011import java.util.ArrayList;
    1112import java.util.Collections;
     
    103104   */
    104105  public static class MapillaryEpochComparator implements
    105       Comparator<MapillaryAbstractImage> {
     106      Comparator<MapillaryAbstractImage>, Serializable {
    106107
    107108    @Override
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/cache/MapillaryCache.java

    r31815 r32974  
    4444   */
    4545  public MapillaryCache(String key, Type type) {
    46     super(MapillaryPlugin.cache, 50000, 50000, new HashMap<String, String>());
     46    super(MapillaryPlugin.getCache(), 50000, 50000, new HashMap<String, String>());
    4747    String k = null;
    4848    URL u = null;
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/FinishedUploadDialog.java

    r32593 r32974  
    4444  }
    4545
    46   private class WebAction implements ActionListener {
     46  private static class WebAction implements ActionListener {
    4747    @Override
    4848    public void actionPerformed(ActionEvent e) {
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryChangesetDialog.java

    r32690 r32974  
    146146  }
    147147
    148   private class SubmitAction extends AbstractAction {
     148  private static class SubmitAction extends AbstractAction {
    149149
    150150    private static final long serialVersionUID = -2761935780353053512L;
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryFilterDialog.java

    r32972 r32974  
    1111import java.util.Arrays;
    1212import java.util.Calendar;
     13import java.util.function.Predicate;
    1314import java.util.regex.Pattern;
    1415
     
    7273   */
    7374  private static final String[] SIGN_TAGS = {"prohibitory--maximum-speed-limit",
    74           "regulatory|priority--stop", "regulatory|priority--give_way|yield", "warning|mandatory--roundabout",
    75           "prohibitory|regulatory--no-entry|no-traffic-both-ways",
    76           "crossroads|junction", "mandatory--turn|straight", "uneven|slippery",
    77           "no-parking", "no_overtaking",
    78           "danger_pedestrian_crossing", "no_*_turn"};
     75    "regulatory|priority--stop", "regulatory|priority--give_way|yield", "warning|mandatory--roundabout",
     76    "prohibitory|regulatory--no-entry|no-traffic-both-ways",
     77    "crossroads|junction", "mandatory--turn|straight", "uneven|slippery",
     78    "no-parking", "no_overtaking",
     79    "danger_pedestrian_crossing", "no_*_turn"};
    7980  /**
    8081   * The {@link JCheckBox} where the respective tag should be searched
    8182   */
    8283  private final JCheckBox[] SIGN_CHECKBOXES = {this.signFilter.maxSpeed,
    83           this.signFilter.stop, this.signFilter.giveWay,
    84           this.signFilter.roundabout, this.signFilter.access, this.signFilter.intersection,
    85           this.signFilter.direction, this.signFilter.uneven,
    86           this.signFilter.noParking, this.signFilter.noOvertaking,
    87           this.signFilter.crossing, this.signFilter.noTurn};
     84    this.signFilter.stop, this.signFilter.giveWay,
     85    this.signFilter.roundabout, this.signFilter.access, this.signFilter.intersection,
     86    this.signFilter.direction, this.signFilter.uneven,
     87    this.signFilter.noParking, this.signFilter.noOvertaking,
     88    this.signFilter.crossing, this.signFilter.noTurn};
    8889
    8990  private MapillaryFilterDialog() {
    9091    super(tr("Mapillary filter"), "mapillary-filter.svg",
    91             tr("Open Mapillary filter dialog"), Shortcut.registerShortcut(
    92                     tr("Mapillary filter"), tr("Open Mapillary filter dialog"),
    93                     KeyEvent.VK_M, Shortcut.NONE), 200);
     92      tr("Open Mapillary filter dialog"), Shortcut.registerShortcut(
     93        tr("Mapillary filter"), tr("Open Mapillary filter dialog"),
     94        KeyEvent.VK_M, Shortcut.NONE), 200);
    9495
    9596    this.signChooser.setEnabled(false);
     
    179180    boolean onlySigns = this.onlySigns.isSelected();
    180181
    181     for (MapillaryAbstractImage img : MapillaryLayer.getInstance().getData().getImages()) {
    182       img.setVisible(true);
    183       if (img instanceof MapillaryImportedImage) {
    184         if (!imported)
    185           img.setVisible(false);
    186         continue;
    187       } else if (img instanceof MapillaryImage) {
    188         if (!downloaded) {
    189           img.setVisible(false);
    190           continue;
    191         }
    192         if (onlySigns) {
    193           if (((MapillaryImage) img).getSigns().isEmpty()) {
    194             img.setVisible(false);
    195             continue;
    196           }
    197           if (!checkSigns((MapillaryImage) img)) {
    198             img.setVisible(false);
    199             continue;
    200           }
    201         }
    202         if (!"".equals(user.getText())
    203                 && !this.user.getText().equals(((MapillaryImage) img).getUser())) {
    204           img.setVisible(false);
    205           continue;
    206         }
     182    // This predicate returns true is the image should be made invisible
     183    Predicate<MapillaryAbstractImage> p =
     184      img ->
     185        (img instanceof MapillaryImportedImage && !imported) ||
     186          (img instanceof MapillaryImage &&
     187            (!downloaded ||
     188              (onlySigns && (((MapillaryImage) img).getSigns().isEmpty() || !checkSigns((MapillaryImage) img))) ||
     189              (!user.getText().equals("") && !this.user.getText().equals(((MapillaryImage) img).getUser())))) ||
     190          checkValidTime(img);
     191
     192    MapillaryLayer.getInstance().getData().getImages().parallelStream().forEach(img -> img.setVisible(!p.test(img)));
     193
     194    Main.map.repaint();
     195  }
     196
     197  private boolean checkValidTime(MapillaryAbstractImage img) {
     198    Long currentTime = currentTime();
     199    long[] timeFactor = new long[]{
     200      31_536_000_000L, // = 365 * 24 * 60 * 60 * 1000 = number of ms in a year
     201      2_592_000_000L, // = 30 * 24 * 60 * 60 * 1000 = number of ms in a month
     202      86_400_000 // = 24 * 60 * 60 * 1000 = number of ms in a day
     203    };
     204    for (int i = 1; i <= 3; i++) {
     205      if (TIME_LIST[i].equals(time.getSelectedItem())
     206        && img.getCapturedAt() < currentTime - ((Integer) spinner.getValue()).longValue() * timeFactor[i - 1]
     207        ) {
     208        return true;
    207209      }
    208       // Calculates the amount of days since the image was taken
    209       Long currentTime = currentTime();
    210       long[] timeFactor = new long[]{
    211               31_536_000_000L, // = 365 * 24 * 60 * 60 * 1000 = number of ms in a year
    212               2_592_000_000L, // = 30 * 24 * 60 * 60 * 1000 = number of ms in a month
    213               86_400_000 // = 24 * 60 * 60 * 1000 = number of ms in a day
    214       };
    215       for (int i = 1; i <= 3; i++) {
    216         if (TIME_LIST[i].equals(time.getSelectedItem())
    217                 && img.getCapturedAt() < currentTime - ((Integer) spinner.getValue()).longValue() * timeFactor[i - 1]
    218                 ) {
    219           img.setVisible(false);
    220         }
    221       }
    222     }
    223     Main.map.repaint();
     210    }
     211    return false;
    224212  }
    225213
     
    228216   *
    229217   * @param img The {@link MapillaryAbstractImage} object that is going to be
    230    *            checked.
     218   * checked.
     219   *
    231220   * @return {@code true} if it fulfills the conditions; {@code false}
    232221   * otherwise.
     
    245234      String[] parts = signTag.split("--");
    246235      if (Pattern.compile(parts[0]).matcher(sign.getCategory()).find() &&
    247               Pattern.compile(parts[1]).matcher(sign.getType()).find()) {
     236        Pattern.compile(parts[1]).matcher(sign.getType()).find()) {
    248237        contains = true;
    249238      }
     
    341330      JPanel dialog = MapillaryFilterChooseSigns.getInstance();
    342331      JOptionPane pane = new JOptionPane(dialog, JOptionPane.PLAIN_MESSAGE,
    343               JOptionPane.OK_CANCEL_OPTION);
     332        JOptionPane.OK_CANCEL_OPTION);
    344333      JDialog dlg = pane.createDialog(Main.parent, tr("Choose signs"));
    345334      dlg.setVisible(true);
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryPreferenceSetting.java

    r32690 r32974  
    185185   *
    186186   */
    187   private class LoginAction extends AbstractAction {
     187  private static class LoginAction extends AbstractAction {
    188188    private static final long serialVersionUID = -3908477563072057344L;
    189189    private final transient MapillaryLoginListener callback;
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/mode/SelectMode.java

    r32740 r32974  
    5353    MapillaryAbstractImage closest = getClosest(e.getPoint());
    5454    if (!(Main.getLayerManager().getActiveLayer() instanceof MapillaryLayer)
    55             && closest != null && Main.map.mapMode == Main.map.mapModeSelect) {
     55      && closest != null && Main.map.mapMode == Main.map.mapModeSelect) {
    5656      this.lastClicked = this.closest;
    5757      this.data.setSelectedImage(closest);
     
    6262    // Double click
    6363    if (e.getClickCount() == 2 && this.data.getSelectedImage() != null && closest != null) {
    64       for (MapillaryAbstractImage img : closest.getSequence().getImages()) {
    65         this.data.addMultiSelectedImage(img);
    66       }
     64      closest.getSequence().getImages().forEach(data::addMultiSelectedImage);
    6765    }
    6866    this.lastClicked = this.closest;
     
    7674      // shift + click
    7775    } else if (
    78         e.getModifiers() == (InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK)
     76      e.getModifiers() == (InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK)
    7977        && this.lastClicked instanceof MapillaryImage
    80     ) {
    81       if (this.closest != null && this.lastClicked != null
    82               && this.closest.getSequence() == (this.lastClicked).getSequence()) {
     78      ) {
     79      if (this.closest != null
     80        && this.closest.getSequence() == (this.lastClicked).getSequence()) {
    8381        int i = this.closest.getSequence().getImages().indexOf(this.closest);
    8482        int j = this.lastClicked.getSequence().getImages().indexOf(this.lastClicked);
    8583        this.data.addMultiSelectedImage(
    86             i < j
     84          i < j
    8785            ? new ConcurrentSkipListSet<>(this.closest.getSequence().getImages().subList(i, j + 1))
    8886            : new ConcurrentSkipListSet<>(this.closest.getSequence().getImages().subList(j, i + 1))
     
    10098    if (
    10199      Main.getLayerManager().getActiveLayer() == MapillaryLayer.getInstance()
    102       && SwingUtilities.isLeftMouseButton(e)
    103       && highlightImg != null && highlightImg.getLatLon() != null
    104     ) {
     100        && SwingUtilities.isLeftMouseButton(e)
     101        && highlightImg != null && highlightImg.getLatLon() != null
     102      ) {
    105103      Point highlightImgPoint = Main.map.mapView.getPoint(highlightImg.getTempLatLon());
    106104      if (e.isShiftDown()) { // turn
    107         for (MapillaryAbstractImage img : data.getMultiSelectedImages()) {
    108           if (!(img instanceof MapillaryImage)) {
    109             img.turn(Math.toDegrees(Math.atan2(e.getX() - highlightImgPoint.getX(), -e.getY() + highlightImgPoint.getY())) - highlightImg.getTempCa());
    110           }
    111         }
     105        this.data.getMultiSelectedImages().parallelStream().filter(img -> !(img instanceof MapillaryImage))
     106          .forEach(img -> img.turn(Math.toDegrees(Math.atan2(e.getX() - highlightImgPoint.getX(), -e.getY() + highlightImgPoint.getY())) - highlightImg.getTempCa()));
    112107      } else { // move
    113         for (MapillaryAbstractImage img : this.data.getMultiSelectedImages()) {
    114           if (!(img instanceof MapillaryImage)) {
    115             LatLon eventLatLon = Main.map.mapView.getLatLon(e.getX(), e.getY());
    116             LatLon imgLatLon = Main.map.mapView.getLatLon(highlightImgPoint.getX(), highlightImgPoint.getY());
    117             img.move(eventLatLon.getX() - imgLatLon.getX(), eventLatLon.getY() - imgLatLon.getY());
    118           }
    119         }
     108        LatLon eventLatLon = Main.map.mapView.getLatLon(e.getX(), e.getY());
     109        LatLon imgLatLon = Main.map.mapView.getLatLon(highlightImgPoint.getX(), highlightImgPoint.getY());
     110        this.data.getMultiSelectedImages().parallelStream().filter(img -> !(img instanceof MapillaryImage))
     111          .forEach(img -> img.move(eventLatLon.getX() - imgLatLon.getX(), eventLatLon.getY() - imgLatLon.getY()));
    120112      }
    121113      Main.map.repaint();
     
    131123      double to = this.data.getSelectedImage().getMovingCa();
    132124      this.record.addCommand(new CommandTurn(this.data.getMultiSelectedImages(), to
    133               - from));
     125        - from));
    134126    } else if (this.data.getSelectedImage().getTempLatLon() != this.data
    135             .getSelectedImage().getMovingLatLon()) {
     127      .getSelectedImage().getMovingLatLon()) {
    136128      LatLon from = this.data.getSelectedImage().getTempLatLon();
    137129      LatLon to = this.data.getSelectedImage().getMovingLatLon();
    138130      this.record.addCommand(new CommandMove(this.data.getMultiSelectedImages(), to
    139               .getX() - from.getX(), to.getY() - from.getY()));
     131        .getX() - from.getX(), to.getY() - from.getY()));
    140132    }
    141     for (MapillaryAbstractImage img : this.data.getMultiSelectedImages()) {
    142       if (img != null)
    143         img.stopMoving();
    144     }
     133    this.data.getMultiSelectedImages().parallelStream().filter(img -> img != null).forEach(MapillaryAbstractImage::stopMoving);
    145134  }
    146135
     
    151140  public void mouseMoved(MouseEvent e) {
    152141    if (Main.getLayerManager().getActiveLayer() instanceof OsmDataLayer
    153             && Main.map.mapMode != Main.map.mapModeSelect) {
     142      && Main.map.mapMode != Main.map.mapModeSelect) {
    154143      return;
    155144    }
     
    161150
    162151    if (closestTemp != null
    163             && Main.getLayerManager().getActiveLayer() instanceof OsmDataLayer
    164             && !this.imageHighlighted) {
     152      && Main.getLayerManager().getActiveLayer() instanceof OsmDataLayer
     153      && !this.imageHighlighted) {
    165154      Main.map.mapMode.putValue("active", Boolean.FALSE);
    166155      this.imageHighlighted = true;
    167156
    168157    } else if (closestTemp == null
    169             && Main.getLayerManager().getActiveLayer() instanceof OsmDataLayer
    170             && this.imageHighlighted && this.nothingHighlighted) {
     158      && Main.getLayerManager().getActiveLayer() instanceof OsmDataLayer
     159      && this.imageHighlighted && this.nothingHighlighted) {
    171160      this.nothingHighlighted = false;
    172161      Main.map.mapMode.putValue("active", Boolean.TRUE);
    173162
    174163    } else if (this.imageHighlighted && !this.nothingHighlighted
    175             && Main.getLayerManager().getEditLayer().data != null
    176             && Main.getLayerManager().getActiveLayer() instanceof OsmDataLayer) {
     164      && Main.getLayerManager().getEditLayer().data != null
     165      && Main.getLayerManager().getActiveLayer() instanceof OsmDataLayer) {
    177166
    178167      for (OsmPrimitive primivitive : Main.getLayerManager().getEditLayer().data
    179               .allPrimitives()) {
     168        .allPrimitives()) {
    180169        primivitive.setHighlighted(false);
    181170      }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/UploadUtils.java

    r32593 r32974  
    273273      }
    274274    }
    275     file.delete();
     275    if (!file.delete()) {
     276      Main.error("MapillaryPlugin: File could not be deleted during upload");
     277    }
    276278    MapillaryUtils.updateHelpText();
    277279  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/utils/ImageUtil.java

    r32642 r32974  
    4848      for (File child : f.listFiles()) {
    4949        try {
    50           images.addAll(readImagesFrom(child, defaultLL));
     50          assert images.addAll(readImagesFrom(child, defaultLL));
    5151        } catch (IOException e) {
    5252          // Don't throw an exception here to allow other files that might be readable to be read.
Note: See TracChangeset for help on using the changeset viewer.