Ignore:
Timestamp:
2015-12-16T17:17:00+01:00 (9 years ago)
Author:
floscher
Message:

[mapillary] Performance improvements by e.g. using StringBuilder and avoiding initializations to defaults

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

Legend:

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

    r31787 r31838  
    1313   * Fired when any image is added to the database.
    1414   */
    15   public void imagesAdded();
     15  void imagesAdded();
    1616
    1717  /**
     
    2424   *          New selected {@link MapillaryAbstractImage}
    2525   */
    26   public void selectedImageChanged(MapillaryAbstractImage oldImage,
     26  void selectedImageChanged(MapillaryAbstractImage oldImage,
    2727      MapillaryAbstractImage newImage);
    2828}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/actions/MapillaryWalkAction.java

    r31811 r31838  
    9797
    9898  @Override
    99   public void selectedImageChanged(MapillaryAbstractImage oldImage,
    100       MapillaryAbstractImage newImage) {
    101     if (newImage != null)
    102       MapillaryPlugin.setMenuEnabled(MapillaryPlugin.getWalkMenu(), true);
    103     else
    104       MapillaryPlugin.setMenuEnabled(MapillaryPlugin.getWalkMenu(), false);
     99  public void selectedImageChanged(MapillaryAbstractImage oldImage, MapillaryAbstractImage newImage) {
     100      MapillaryPlugin.setMenuEnabled(MapillaryPlugin.getWalkMenu(), newImage != null);
    105101  }
    106102
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/actions/WalkListener.java

    r31787 r31838  
    1616   * @param thread The thread executing the walk.
    1717   */
    18   public void walkStarted(WalkThread thread);
     18  void walkStarted(WalkThread thread);
    1919}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/actions/WalkThread.java

    r31797 r31838  
    2323  private final int interval;
    2424  private final MapillaryData data;
    25   private boolean end = false;
     25  private boolean end;
    2626  private final boolean waitForFullQuality;
    2727  private final boolean followSelected;
    2828  private final boolean goForward;
    2929  private BufferedImage lastImage;
    30   private volatile boolean paused = false;
     30  private volatile boolean paused;
    3131
    3232  /**
     
    6161          // Predownload next 10 thumbnails.
    6262          for (int i = 0; i < 10; i++) {
    63             if (image.next() == null)
     63            if (image.next() == null) {
    6464              break;
     65            }
    6566            image = image.next();
    66             CacheUtils.downloadPicture((MapillaryImage) image,
    67                 CacheUtils.PICTURE.THUMBNAIL);
     67            CacheUtils.downloadPicture((MapillaryImage) image, CacheUtils.PICTURE.THUMBNAIL);
    6868          }
    6969          if (this.waitForFullQuality)
    7070            // Start downloading 3 next full images.
    7171            for (int i = 0; i < 3; i++) {
    72               if (image.next() == null)
     72              if (image.next() == null) {
    7373                break;
     74              }
    7475              image = image.next();
    75               CacheUtils.downloadPicture((MapillaryImage) image,
    76                   CacheUtils.PICTURE.FULL_IMAGE);
     76              CacheUtils.downloadPicture((MapillaryImage) image, CacheUtils.PICTURE.FULL_IMAGE);
    7777            }
    7878        }
     
    8080          synchronized (this) {
    8181            // Waits for full quality picture.
     82            final BufferedImage displayImage = MapillaryMainDialog.getInstance().mapillaryImageDisplay.getImage();
    8283            if (this.waitForFullQuality && image instanceof MapillaryImage) {
    83               while (MapillaryMainDialog.getInstance().mapillaryImageDisplay
    84                   .getImage() == this.lastImage
    85                   || MapillaryMainDialog.getInstance().mapillaryImageDisplay
    86                       .getImage() == null
    87                   || MapillaryMainDialog.getInstance().mapillaryImageDisplay
    88                       .getImage().getWidth() < 2048)
     84              while ( displayImage == this.lastImage || displayImage == null || displayImage.getWidth() < 2048) {
    8985                wait(100);
     86              }
    9087            }
    9188            // Waits for thumbnail.
    9289            else {
    93               while (MapillaryMainDialog.getInstance().mapillaryImageDisplay
    94                   .getImage() == this.lastImage
    95                   || MapillaryMainDialog.getInstance().mapillaryImageDisplay
    96                       .getImage() == null
    97                   || MapillaryMainDialog.getInstance().mapillaryImageDisplay
    98                       .getImage().getWidth() < 320)
     90              while (displayImage == this.lastImage || displayImage == null || displayImage.getWidth() < 320) {
    9991                wait(100);
     92              }
    10093            }
    101             while (this.paused)
     94            while (this.paused) {
    10295              wait(100);
     96            }
    10397            wait(this.interval);
    104             while (this.paused)
     98            while (this.paused) {
    10599              wait(100);
     100            }
    106101          }
    107           this.lastImage = MapillaryMainDialog.getInstance().mapillaryImageDisplay
    108               .getImage();
     102          this.lastImage = MapillaryMainDialog.getInstance().mapillaryImageDisplay.getImage();
    109103          synchronized (this) {
    110             if (this.goForward)
     104            if (this.goForward) {
    111105              this.data.selectNext(this.followSelected);
    112             else
     106            } else {
    113107              this.data.selectPrevious(this.followSelected);
     108            }
    114109          }
    115110        } catch (InterruptedException e) {
     
    118113      }
    119114    } catch (NullPointerException e) {
     115      // TODO: Avoid NPEs instead of waiting until they are thrown and then catching them
    120116      return;
    121117    }
     
    129125
    130126  @Override
    131   public void selectedImageChanged(MapillaryAbstractImage oldImage,
    132       MapillaryAbstractImage newImage) {
     127  public void selectedImageChanged(MapillaryAbstractImage oldImage, MapillaryAbstractImage newImage) {
    133128    if (newImage != oldImage.next()) {
    134129      end();
     
    155150   */
    156151  public void stopWalk() {
    157     if (!SwingUtilities.isEventDispatchThread()) {
     152    if (SwingUtilities.isEventDispatchThread()) {
     153      end();
     154      this.interrupt();
     155    } else {
    158156      SwingUtilities.invokeLater(new Runnable() {
    159157        @Override
     
    162160        }
    163161      });
    164     } else {
    165       end();
    166       this.interrupt();
    167162    }
    168163  }
     
    172167   */
    173168  public void end() {
    174     if (!SwingUtilities.isEventDispatchThread()) {
     169    if (SwingUtilities.isEventDispatchThread()) {
     170      this.end = true;
     171      this.data.removeListener(this);
     172      MapillaryMainDialog.getInstance().setMode(MapillaryMainDialog.MODE.NORMAL);
     173    } else {
    175174      SwingUtilities.invokeLater(new Runnable() {
    176175        @Override
     
    179178        }
    180179      });
    181     } else {
    182       this.end = true;
    183       this.data.removeListener(this);
    184       MapillaryMainDialog.getInstance()
    185           .setMode(MapillaryMainDialog.MODE.NORMAL);
    186180    }
    187181  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryMainDialog.java

    r31833 r31838  
    308308      });
    309309    } else if (this.image != null) {
     310      StringBuilder title = new StringBuilder(tr(BASE_TITLE));
    310311      if (this.image instanceof MapillaryImage) {
    311312        MapillaryImage mapillaryImage = (MapillaryImage) this.image;
    312         String title = tr(BASE_TITLE);
    313313        if (mapillaryImage.getUser() != null)
    314           title += " -- " + mapillaryImage.getUser();
     314          title.append(" — ").append(mapillaryImage.getUser());
    315315        if (mapillaryImage.getCapturedAt() != 0)
    316           title += " -- " + mapillaryImage.getDate();
    317         setTitle(title);
     316          title.append(" — ").append(mapillaryImage.getDate());
     317        setTitle(title.toString());
    318318      } else if (this.image instanceof MapillaryImportedImage) {
    319319        MapillaryImportedImage mapillaryImportedImage = (MapillaryImportedImage) this.image;
    320         String title = tr(BASE_TITLE);
    321         title += " -- " + mapillaryImportedImage.getFile().getName();
    322         title += " -- " + mapillaryImportedImage.getDate();
    323         setTitle(title);
     320        title.append(" — ").append(mapillaryImportedImage.getFile().getName());
     321        title.append(" — ").append(mapillaryImportedImage.getDate());
     322        setTitle(title.toString());
    324323      }
    325324    }
     
    518517    } else if (data != null && result == LoadResult.SUCCESS) {
    519518      try {
    520         BufferedImage img = ImageIO.read(new ByteArrayInputStream(data
    521             .getContent()));
    522         if (img == null)
     519        BufferedImage img = ImageIO.read(new ByteArrayInputStream(data.getContent()));
     520        if (img == null) {
    523521          return;
    524         if (this.mapillaryImageDisplay.getImage() == null)
     522        }
     523        if (this.mapillaryImageDisplay.getImage() == null) {
    525524          this.mapillaryImageDisplay.setImage(img);
    526         else if (img.getHeight() > this.mapillaryImageDisplay.getImage()
    527             .getHeight()) {
     525        } else if (img.getHeight() > this.mapillaryImageDisplay.getImage().getHeight()) {
    528526          this.mapillaryImageDisplay.setImage(img);
    529527        }
     
    563561      panel.add(this.buttonsPanel, BorderLayout.NORTH);
    564562      createLayout(panel, true, null);
    565     } else
     563    } else {
    566564      createLayout(panel, true, buttons);
     565    }
    567566    this.add(this.titleBar, BorderLayout.NORTH);
    568567  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryUtils.java

    r31836 r31838  
    1515import java.util.Date;
    1616import java.util.List;
     17import java.util.Locale;
    1718
    1819import javax.swing.SwingUtilities;
     
    163164   * @throws ParseException
    164165   */
    165   public static long getEpoch(String date, String format)
    166       throws ParseException {
    167     SimpleDateFormat formatter = new SimpleDateFormat(format);
     166  public static long getEpoch(String date, String format) throws ParseException {
     167    SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.UK);
    168168    Date dateTime = formatter.parse(date);
    169169    return dateTime.getTime();
     
    460460   */
    461461  public static void updateHelpText() {
    462     String ret = "";
    463     if (PluginState.isDownloading())
    464       ret += tr("Downloading Mapillary images");
    465     else if (MapillaryLayer.getInstance().getData().size() > 0)
    466       ret += tr("Total Mapillary images: {0}",
    467           MapillaryLayer.getInstance().getData().size());
    468     else
    469       ret += tr("No images found");
    470     if (MapillaryLayer.getInstance().mode != null)
    471       ret += " -- " + tr(MapillaryLayer.getInstance().mode.toString());
    472     if (PluginState.isUploading())
    473       ret += " -- " + PluginState.getUploadString();
     462    StringBuilder ret = new StringBuilder();
     463    if (PluginState.isDownloading()) {
     464      ret .append(tr("Downloading Mapillary images"));
     465    } else if (MapillaryLayer.getInstance().getData().size() > 0) {
     466      ret.append(tr("Total Mapillary images: {0}", MapillaryLayer.getInstance().getData().size()));
     467    } else {
     468      ret.append(tr("No images found"));
     469    }
     470    if (MapillaryLayer.getInstance().mode != null) {
     471      ret.append(" — ").append(tr(MapillaryLayer.getInstance().mode.toString()));
     472    }
     473    if (PluginState.isUploading()) {
     474      ret.append(" — ").append(PluginState.getUploadString());
     475    }
    474476    synchronized (MapillaryUtils.class) {
    475       Main.map.statusLine.setHelpText(ret);
     477      Main.map.statusLine.setHelpText(ret.toString());
    476478    }
    477479  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/utils/PluginState.java

    r31787 r31838  
    1717public class PluginState {
    1818
    19   private static int runningDownloads = 0;
     19  private static int runningDownloads;
    2020  /** Images that have to be uploaded. */
    21   protected static int imagesToUpload = 0;
     21  protected static int imagesToUpload;
    2222  /** Images that have been uploaded. */
    23   public static int imagesUploaded = 0;
     23  public static int imagesUploaded;
     24
     25  private PluginState() {
     26    // Empty constructor to avoid instantiation
     27  }
    2428
    2529  /**
     
    7781  public static void imageUploaded() {
    7882    imagesUploaded++;
    79     if (imagesToUpload == imagesUploaded) {
    80       if (Main.main != null)
     83    if (imagesToUpload == imagesUploaded && Main.main != null) {
    8184        finishedUploadDialog();
    8285    }
     
    8588  private static void finishedUploadDialog() {
    8689    if (!SwingUtilities.isEventDispatchThread()) {
     90      JOptionPane pane = new JOptionPane();
     91      pane.setMessage(new FinishedUploadDialog());
     92      JDialog dlg = pane.createDialog(Main.parent, tr("Finished upload"));
     93      dlg.setVisible(true);
     94    } else {
    8795      SwingUtilities.invokeLater(new Runnable() {
    8896        @Override
     
    9199        }
    92100      });
    93     } else {
    94       JOptionPane pane = new JOptionPane();
    95       pane.setMessage(new FinishedUploadDialog());
    96       JDialog dlg = pane.createDialog(Main.parent, tr("Finished upload"));
    97       dlg.setVisible(true);
    98101    }
    99102  }
     
    105108   */
    106109  public static String getUploadString() {
    107     return tr("Uploading: {0}", "(" + imagesUploaded + "/" + imagesToUpload
    108         + ")");
     110    return tr("Uploading: {0}", "(" + imagesUploaded + "/" + imagesToUpload + ")");
    109111  }
    110112}
Note: See TracChangeset for help on using the changeset viewer.