Changeset 10592 in josm for trunk


Ignore:
Timestamp:
2016-07-22T23:39:36+02:00 (4 years ago)
Author:
Don-vip
Message:

see #11390, fix #13192 - Update LayerPositionStrategy to be a java 8 functional interface (patch by michael2402, modified) - gsoc-core

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/LayerPositionStrategy.java

    r10271 r10592  
    33
    44import java.util.List;
     5import java.util.function.Predicate;
    56
    6 import org.openstreetmap.josm.tools.Predicate;
    77import org.openstreetmap.josm.tools.Predicates;
    88
     
    1111 * @author Michael Zangl
    1212 * @since 10008
     13 * @since 10592 functional interface
    1314 */
    14 public abstract class LayerPositionStrategy {
     15@FunctionalInterface
     16public interface LayerPositionStrategy {
    1517
    1618    /**
    1719     * always inserts at the front of the stack.
    1820     */
    19     public static final LayerPositionStrategy IN_FRONT = new LayerPositionStrategy() {
    20         @Override
    21         public int getPosition(LayerManager manager) {
    22             return 0;
    23         }
    24     };
     21    LayerPositionStrategy IN_FRONT = manager -> 0;
    2522
    2623    /**
    2724     * A GPX layer is added below the lowest data layer.
    2825     */
    29     public static final LayerPositionStrategy AFTER_LAST_DATA_LAYER = afterLast(new Predicate<Layer>() {
    30         @Override
    31         public boolean evaluate(Layer object) {
    32             return object instanceof OsmDataLayer || object instanceof ValidatorLayer;
    33         }
    34     });
     26    LayerPositionStrategy AFTER_LAST_DATA_LAYER = afterLast(
     27            layer -> layer instanceof OsmDataLayer || layer instanceof ValidatorLayer);
    3528
    3629    /**
    3730     * A normal layer is added after all validation layers.
    3831     */
    39     public static final LayerPositionStrategy AFTER_LAST_VALIDATION_LAYER = afterLast(new Predicate<Layer>() {
    40         @Override
    41         public boolean evaluate(Layer object) {
    42             return object instanceof ValidatorLayer;
    43         }
    44     });
     32    LayerPositionStrategy AFTER_LAST_VALIDATION_LAYER = afterLast(
     33            layer -> layer instanceof ValidatorLayer);
    4534
    4635    /**
     
    4837     * If there is none, they are added at the end of the list.
    4938     */
    50     public static final LayerPositionStrategy BEFORE_FIRST_BACKGROUND_LAYER = inFrontOfFirst(new Predicate<Layer>() {
    51         @Override
    52         public boolean evaluate(Layer object) {
    53             return object.isBackgroundLayer();
    54         }
    55     });
     39    LayerPositionStrategy BEFORE_FIRST_BACKGROUND_LAYER = inFrontOfFirst(
     40            layer -> layer.isBackgroundLayer());
    5641
    5742    /**
     
    6045     * @return The strategy
    6146     */
    62     public static LayerPositionStrategy inFrontOf(Layer other) {
     47    static LayerPositionStrategy inFrontOf(Layer other) {
    6348        return inFrontOfFirst(Predicates.equalTo(other));
    6449    }
     
    6954     * @return The strategy.
    7055     */
    71     public static LayerPositionStrategy inFrontOfFirst(final Predicate<Layer> what) {
    72         return new LayerPositionStrategy() {
    73             @Override
    74             public int getPosition(LayerManager manager) {
    75                 List<Layer> layers = manager.getLayers();
    76                 for (int i = 0; i < layers.size(); i++) {
    77                     if (what.evaluate(layers.get(i))) {
    78                         return i;
    79                     }
     56    static LayerPositionStrategy inFrontOfFirst(final Predicate<Layer> what) {
     57        return manager -> {
     58            List<Layer> layers = manager.getLayers();
     59            for (int i = 0; i < layers.size(); i++) {
     60                if (what.test(layers.get(i))) {
     61                    return i;
    8062                }
    81                 return layers.size();
    8263            }
     64            return layers.size();
    8365        };
    8466    }
     
    8971     * @return The strategy.
    9072     */
    91     public static LayerPositionStrategy afterLast(final Predicate<Layer> what) {
    92         return new LayerPositionStrategy() {
    93             @Override
    94             public int getPosition(LayerManager manager) {
    95                 List<Layer> layers = manager.getLayers();
    96                 for (int i = layers.size() - 1; i >= 0; i--) {
    97                     if (what.evaluate(layers.get(i))) {
    98                         return i + 1;
    99                     }
     73    static LayerPositionStrategy afterLast(final Predicate<Layer> what) {
     74        return manager -> {
     75            List<Layer> layers = manager.getLayers();
     76            for (int i = layers.size() - 1; i >= 0; i--) {
     77                if (what.test(layers.get(i))) {
     78                    return i + 1;
    10079                }
    101                 return 0;
    10280            }
     81            return 0;
    10382        };
    10483    }
     
    10988     * @return The position in the range 0...layers.size
    11089     */
    111     public abstract int getPosition(LayerManager manager);
     90    int getPosition(LayerManager manager);
    11291}
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/LayerManagerTest.java

    r10467 r10592  
    239239            @Override
    240240            public LayerPositionStrategy getDefaultLayerPosition() {
    241                 return new LayerPositionStrategy() {
    242                     @Override
    243                     public int getPosition(LayerManager manager) {
    244                         return 42;
    245                     }
    246                 };
     241                return manager -> 42;
    247242            }
    248243        };
Note: See TracChangeset for help on using the changeset viewer.