Class FilterMatcher


  • public class FilterMatcher
    extends java.lang.Object
    Class that encapsulates the filter logic, i.e. applies a list of filters to a primitive. Uses SearchCompiler.Match.match(org.openstreetmap.josm.data.osm.OsmPrimitive) to see if the filter expression matches, cares for "inverted-flag" of the filters and combines the results of all active filters. There are two major use cases: (1) Hide features that you don't like to edit but get in the way, e.g. landuse or power lines. It is expected, that the inverted flag if false for these kind of filters. (2) Highlight certain features, that are currently interesting and hide everything else. This can be thought of as an improved search (Ctrl-F), where you can continue editing and don't loose the current selection. It is expected that the inverted flag of the filter is true in this case. In addition to the formal application of filter rules, some magic is applied to (hopefully) match the expectations of the user: (1) non-inverted: When hiding a way, all its untagged nodes are hidden as well. This avoids a "cloud of nodes", that normally isn't useful without the corresponding way. (2) inverted: When displaying a way, we show all its nodes, although the individual nodes do not match the filter expression. The reason is, that a way without its nodes cannot be edited properly. Multipolygons and (untagged) member ways are handled in a similar way.
    • Method Detail

      • update

        public void update​(java.util.Collection<Filter> filters)
                    throws SearchParseError
        Clears the current filters, and adds the given filters
        Parameters:
        filters - the filters to add
        Throws:
        SearchParseError - if the search expression in one of the filters cannot be parsed
      • reset

        public void reset()
        Clears the filters in use.
      • hasFilters

        public boolean hasFilters()
        Determines if at least one filter is enabled.
        Returns:
        true if at least one filter is enabled
        Since:
        14206
      • add

        public void add​(Filter filter)
                 throws SearchParseError
        Adds a filter to the currently used filters
        Parameters:
        filter - the filter to add
        Throws:
        SearchParseError - if the search expression in the filter cannot be parsed
      • isFiltered

        private static boolean isFiltered​(IPrimitive primitive,
                                          boolean hidden)
        Check if primitive is filtered.
        Parameters:
        primitive - the primitive to check
        hidden - the minimum level required for the primitive to count as filtered
        Returns:
        when hidden is true, returns whether the primitive is hidden when hidden is false, returns whether the primitive is disabled or hidden
      • isFilterExplicit

        private static <T extends IFilterablePrimitive> boolean isFilterExplicit​(T primitive,
                                                                                 boolean hidden)
        Check if primitive is hidden explicitly. Only used for ways and relations.
        Type Parameters:
        T - The primitive type
        Parameters:
        primitive - the primitive to check
        hidden - the level where the check is performed
        Returns:
        true, if at least one non-inverted filter applies to the primitive
      • allParentWaysFiltered

        private static <T extends IPrimitive & IFilterablePrimitive> boolean allParentWaysFiltered​(T primitive,
                                                                                                   boolean hidden)
        Check if all parent ways are filtered.
        Type Parameters:
        T - The primitive type
        Parameters:
        primitive - the primitive to check
        hidden - parameter that indicates the minimum level of filtering: true when objects need to be hidden to count as filtered and false when it suffices to be disabled to count as filtered
        Returns:
        true if (a) there is at least one parent way (b) all parent ways are filtered at least at the level indicated by the parameter hidden and (c) at least one of the parent ways is explicitly filtered
      • isHidden

        public <T extends IPrimitive & IFilterablePrimitiveFilterMatcher.FilterType isHidden​(T primitive)
        Check if primitive is hidden. The filter flags for all parent objects must be set correctly, when calling this method.
        Type Parameters:
        T - The primitive type
        Parameters:
        primitive - the primitive
        Returns:
        FilterType.NOT_FILTERED when primitive is not hidden; FilterType.EXPLICIT when primitive is hidden and there is a non-inverted filter that applies; FilterType.PASSIV when primitive is hidden and all filters that apply are inverted
      • isDisabled

        public <T extends IPrimitive & IFilterablePrimitiveFilterMatcher.FilterType isDisabled​(T primitive)
        Check if primitive is disabled. The filter flags for all parent objects must be set correctly, when calling this method.
        Type Parameters:
        T - The primitive type
        Parameters:
        primitive - the primitive
        Returns:
        FilterType.NOT_FILTERED when primitive is not disabled; FilterType.EXPLICIT when primitive is disabled and there is a non-inverted filter that applies; FilterType.PASSIV when primitive is disabled and all filters that apply are inverted
      • of

        public static FilterMatcher of​(Filter... filters)
                                throws SearchParseError
        Returns a new FilterMatcher containing the given filters.
        Parameters:
        filters - filters to add to the resulting filter matcher
        Returns:
        a new FilterMatcher containing the given filters
        Throws:
        SearchParseError - if the search expression in a filter cannot be parsed
        Since:
        12383