Changeset 10040 in josm


Ignore:
Timestamp:
2016-03-26T11:30:54+01:00 (4 years ago)
Author:
Don-vip
Message:

fix #12680 - Documentation and cleanup on predicates (patch by michael2402, modified)

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r9941 r10040  
    4848import org.openstreetmap.josm.tools.FilteredCollection;
    4949import org.openstreetmap.josm.tools.Predicate;
     50import org.openstreetmap.josm.tools.Predicates;
    5051import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    5152import org.openstreetmap.josm.tools.Utils;
     
    264265    private final QuadBuckets<Node> nodes = new QuadBuckets<>();
    265266
    266     private <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<OsmPrimitive> predicate) {
     267    private <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<? super OsmPrimitive> predicate) {
    267268        return new SubclassFilteredCollection<>(allPrimitives, predicate);
    268269    }
     
    402403     */
    403404    public Collection<OsmPrimitive> allPrimitives() {
    404         return getPrimitives(OsmPrimitive.allPredicate);
     405        return getPrimitives(Predicates.alwaysTrue());
    405406    }
    406407
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r9979 r10040  
    2828import org.openstreetmap.josm.tools.CheckParameterUtil;
    2929import org.openstreetmap.josm.tools.Predicate;
     30import org.openstreetmap.josm.tools.Predicates;
    3031import org.openstreetmap.josm.tools.Utils;
    3132import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
     
    171172
    172173    /**
    173      * Some predicates, that describe conditions on primitives.
     174     * A predicate that filters primitives that are usable.
     175     * @see OsmPrimitive#isUsable()
    174176     */
    175177    public static final Predicate<OsmPrimitive> isUsablePredicate = new Predicate<OsmPrimitive>() {
     
    180182    };
    181183
     184    /**
     185     * A predicate filtering primitives that are selectable.
     186     */
    182187    public static final Predicate<OsmPrimitive> isSelectablePredicate = new Predicate<OsmPrimitive>() {
    183188        @Override
     
    187192    };
    188193
     194    /**
     195     * A predicate filtering primitives that are not deleted.
     196     */
    189197    public static final Predicate<OsmPrimitive> nonDeletedPredicate = new Predicate<OsmPrimitive>() {
    190198        @Override public boolean evaluate(OsmPrimitive primitive) {
     
    193201    };
    194202
     203    /**
     204     * A predicate filtering primitives that are not deleted and not incomplete.
     205     */
    195206    public static final Predicate<OsmPrimitive> nonDeletedCompletePredicate = new Predicate<OsmPrimitive>() {
    196207        @Override public boolean evaluate(OsmPrimitive primitive) {
     
    199210    };
    200211
     212    /**
     213     * A predicate filtering primitives that are not deleted and not incomplete and that are not a relation.
     214     */
    201215    public static final Predicate<OsmPrimitive> nonDeletedPhysicalPredicate = new Predicate<OsmPrimitive>() {
    202216        @Override public boolean evaluate(OsmPrimitive primitive) {
     
    205219    };
    206220
     221    /**
     222     * A predicate filtering primitives that are modified
     223     */
    207224    public static final Predicate<OsmPrimitive> modifiedPredicate = new Predicate<OsmPrimitive>() {
    208225        @Override public boolean evaluate(OsmPrimitive primitive) {
     
    211228    };
    212229
    213     public static final Predicate<OsmPrimitive> nodePredicate = new Predicate<OsmPrimitive>() {
    214         @Override public boolean evaluate(OsmPrimitive primitive) {
    215             return primitive.getClass() == Node.class;
    216         }
    217     };
    218 
    219     public static final Predicate<OsmPrimitive> wayPredicate = new Predicate<OsmPrimitive>() {
    220         @Override public boolean evaluate(OsmPrimitive primitive) {
    221             return primitive.getClass() == Way.class;
    222         }
    223     };
    224 
    225     public static final Predicate<OsmPrimitive> relationPredicate = new Predicate<OsmPrimitive>() {
    226         @Override public boolean evaluate(OsmPrimitive primitive) {
    227             return primitive.getClass() == Relation.class;
    228         }
    229     };
    230 
     230    /**
     231     * A predicate filtering nodes.
     232     */
     233    public static final Predicate<OsmPrimitive> nodePredicate = Predicates.<OsmPrimitive>isOfClass(Node.class);
     234
     235    /**
     236     * A predicate filtering ways.
     237     */
     238    public static final Predicate<OsmPrimitive> wayPredicate = Predicates.<OsmPrimitive>isOfClass(Way.class);
     239
     240    /**
     241     * A predicate filtering relations.
     242     */
     243    public static final Predicate<OsmPrimitive> relationPredicate = Predicates.<OsmPrimitive>isOfClass(Relation.class);
     244
     245    /**
     246     * A predicate filtering multipolygon relations.
     247     */
    231248    public static final Predicate<OsmPrimitive> multipolygonPredicate = new Predicate<OsmPrimitive>() {
    232249        @Override public boolean evaluate(OsmPrimitive primitive) {
     
    235252    };
    236253
    237     public static final Predicate<OsmPrimitive> allPredicate = new Predicate<OsmPrimitive>() {
    238         @Override public boolean evaluate(OsmPrimitive primitive) {
    239             return true;
    240         }
    241     };
    242 
     254    /**
     255     * This matches all ways that have a direction
     256     *
     257     * @see #FLAG_HAS_DIRECTIONS
     258     */
    243259    public static final Predicate<Tag> directionalKeyPredicate = new Predicate<Tag>() {
    244260        @Override
     
    677693    }
    678694
     695    /**
     696     * Updates the highlight flag for this primitive.
     697     * @param highlighted The new highlight flag.
     698     */
    679699    public void setHighlighted(boolean highlighted) {
    680700        if (isHighlighted() != highlighted) {
     
    686706    }
    687707
     708    /**
     709     * Checks if the highlight flag for this primitive was set
     710     * @return The highlight flag.
     711     */
    688712    public boolean isHighlighted() {
    689713        return (flags & FLAG_HIGHLIGHTED) != 0;
     
    820844    }
    821845
     846    /**
     847     * A tagged way that matches this pattern has a direction.
     848     * @see #FLAG_HAS_DIRECTIONS
     849     */
    822850    private static volatile Match directionKeys;
     851
     852    /**
     853     * A tagged way that matches this pattern has a direction that is reversed.
     854     * <p>
     855     * This pattern should be a subset of {@link #directionKeys}
     856     * @see #FLAG_DIRECTION_REVERSED
     857     */
    823858    private static volatile Match reversedDirectionKeys;
    824859
    825     /**
    826      * Contains a list of direction-dependent keys that make an object
    827      * direction dependent.
    828      * Initialized by checkDirectionTagged()
    829      */
    830860    static {
    831861        String reversedDirectionDefault = "oneway=\"-1\"";
     
    837867                "(highway=motorway_link & -oneway=no & -oneway=reversible)";
    838868
    839         try {
    840             reversedDirectionKeys = SearchCompiler.compile(Main.pref.get("tags.reversed_direction", reversedDirectionDefault));
     869        reversedDirectionKeys = compileDirectionKeys("tags.reversed_direction", reversedDirectionDefault);
     870        directionKeys = compileDirectionKeys("tags.direction", directionDefault);
     871    }
     872
     873    private static Match compileDirectionKeys(String prefName, String defaultValue) throws AssertionError {
     874        try {
     875            return SearchCompiler.compile(Main.pref.get(prefName, defaultValue));
    841876        } catch (ParseError e) {
    842             Main.error("Unable to compile pattern for tags.reversed_direction, trying default pattern: " + e.getMessage());
    843 
    844             try {
    845                 reversedDirectionKeys = SearchCompiler.compile(reversedDirectionDefault);
    846             } catch (ParseError e2) {
    847                 throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage(), e2);
    848             }
    849         }
    850         try {
    851             directionKeys = SearchCompiler.compile(Main.pref.get("tags.direction", directionDefault));
    852         } catch (ParseError e) {
    853             Main.error("Unable to compile pattern for tags.direction, trying default pattern: " + e.getMessage());
    854 
    855             try {
    856                 directionKeys = SearchCompiler.compile(directionDefault);
    857             } catch (ParseError e2) {
    858                 throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage(), e2);
    859             }
     877            Main.error("Unable to compile pattern for " + prefName + ", trying default pattern: " + e.getMessage());
     878        }
     879
     880        try {
     881            return SearchCompiler.compile(defaultValue);
     882        } catch (ParseError e2) {
     883            throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage(), e2);
    860884        }
    861885    }
  • trunk/src/org/openstreetmap/josm/tools/Predicates.java

    r9246 r10040  
    1414
    1515    private Predicates() {
     16    }
     17
     18    /**
     19     * Creates a predicate that returns true every time.
     20     * @param <T> The type of the predicate.
     21     * @return A predicate returning <code>true</code>
     22     * @since 10040
     23     */
     24    public static <T> Predicate<T> alwaysTrue() {
     25        return new Predicate<T>() {
     26            @Override
     27            public boolean evaluate(T object) {
     28                return true;
     29            }
     30        };
     31    }
     32
     33    /**
     34     * Creates a predicate that returns false every time.
     35     * @param <T> The type of the predicate.
     36     * @return A predicate returning <code>false</code>
     37     * @since 10040
     38     */
     39    public static <T> Predicate<T> alwaysFalse() {
     40        return new Predicate<T>() {
     41            @Override
     42            public boolean evaluate(T object) {
     43                return false;
     44            }
     45        };
    1646    }
    1747
     
    4272            public boolean evaluate(T obj) {
    4373                return Objects.equals(obj, ref);
     74            }
     75        };
     76    }
     77
     78    /**
     79     * Creates a new predicate that checks if elements are exactly of that class.
     80     * @param <T> The predicate type.
     81     * @param clazz The class the elements must have.
     82     * @return A predicate.
     83     */
     84    public static <T> Predicate<T> isOfClass(final Class<? extends T> clazz) {
     85        return new Predicate<T>() {
     86            @Override
     87            public boolean evaluate(T obj) {
     88                return obj != null && obj.getClass() == clazz;
    4489            }
    4590        };
Note: See TracChangeset for help on using the changeset viewer.