Ignore:
Timestamp:
2016-08-03T14:37:38+02:00 (9 years ago)
Author:
simon04
Message:

see #11390 - Use CompletableFuture for async image loading

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r10680 r10714  
    4040import java.util.Map;
    4141import java.util.TreeSet;
     42import java.util.concurrent.CompletableFuture;
    4243import java.util.concurrent.ExecutorService;
    4344import java.util.concurrent.Executors;
     
    308309
    309310    /**
    310      * Callback interface for asynchronous image loading.
    311      * @since 10600 (functional interface)
    312      */
    313     @FunctionalInterface
    314     public interface ImageCallback {
    315         /**
    316          * Called when image loading has finished.
    317          * @param result the loaded image icon
    318          */
    319         void finished(ImageIcon result);
    320     }
    321 
    322     /**
    323      * Callback interface for asynchronous image loading (with delayed scaling possibility).
    324      * @since 7693
    325      * @since 10600 (functional interface)
    326      */
    327     @FunctionalInterface
    328     public interface ImageResourceCallback {
    329         /**
    330          * Called when image loading has finished.
    331          * @param result the loaded image resource
    332          */
    333         void finished(ImageResource result);
    334     }
    335 
    336     /**
    337311     * Constructs a new {@code ImageProvider} from a filename in a given directory.
    338312     * @param subdir subdirectory the image lies in
     
    653627
    654628    /**
     629     * Load the image in a background thread.
     630     *
     631     * This method returns immediately and runs the image request asynchronously.
     632     *
     633     * @return the future of the requested image
     634     * @since 10714
     635     */
     636    public CompletableFuture<ImageIcon> getAsync() {
     637        return CompletableFuture.supplyAsync(this::get, IMAGE_FETCHER);
     638    }
     639
     640    /**
    655641     * Execute the image request.
    656642     *
     
    687673     * This method returns immediately and runs the image request asynchronously.
    688674     *
    689      * @param callback a callback. It is called, when the image is ready.
    690      * This can happen before the call to this method returns or it may be
    691      * invoked some time (seconds) later. If no image is available, a null
    692      * value is returned to callback (just like {@link #get}).
    693      */
    694     public void getInBackground(final ImageCallback callback) {
    695         if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(WIKI_PROTOCOL)) {
    696             IMAGE_FETCHER.submit(() -> callback.finished(get()));
    697         } else {
    698             callback.finished(get());
    699         }
    700     }
    701 
    702     /**
    703      * Load the image in a background thread.
    704      *
    705      * This method returns immediately and runs the image request asynchronously.
    706      *
    707      * @param callback a callback. It is called, when the image is ready.
    708      * This can happen before the call to this method returns or it may be
    709      * invoked some time (seconds) later. If no image is available, a null
    710      * value is returned to callback (just like {@link #get}).
    711      * @since 7693
    712      */
    713     public void getInBackground(final ImageResourceCallback callback) {
    714         if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(WIKI_PROTOCOL)) {
    715             IMAGE_FETCHER.submit(() -> callback.finished(getResource()));
    716         } else {
    717             callback.finished(getResource());
    718         }
     675     * @return the future of the requested image
     676     * @since 10714
     677     */
     678     public CompletableFuture<ImageResource> getResourceAsync() {
     679        return CompletableFuture.supplyAsync(this::getResource, IMAGE_FETCHER);
    719680    }
    720681
Note: See TracChangeset for help on using the changeset viewer.