Package org.apache.sis.internal.filter
Class Visitor<R,A>
java.lang.Object
org.apache.sis.internal.filter.Visitor<R,A>
- Type Parameters:
R- the type of resources (e.g.Feature) used as inputs.A- type of the accumulator object where actions will write their results.
- Direct Known Subclasses:
SelectionClauseWriter
An executor of different actions depending on filter or expression type.
Action are defined by
BiConsumer where the first parameter is the filter or expression,
and the second parameter is an arbitrary object used as accumulator. For example, the accumulator
may be a StringBuilder where the filter is written as a SQL or CQL statement.
Relationship with the visitor pattern
This class provides similar functionalities than the "visitor pattern". The actions are defined by lambda functions in a
This class provides similar functionalities than the "visitor pattern". The actions are defined by lambda functions in a
HashMap instead of by overriding methods,
but the results are similar.Thread-safety
Visitor instances are thread-safe if protected methods are invoked at construction time only.- Since:
- 1.1
- Version:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<String,BiConsumer<Expression<R, ?>, A>> All expressions known to this visitor.All filters known to this visitor. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedVisitor()Creates a new visitor.protectedVisitor(boolean hasFilters, boolean hasExpressions) Creates a new visitor which will accept only the specified type of objects.protectedCreates a new visitor initialized to the same handlers than the specified visitor. -
Method Summary
Modifier and TypeMethodDescriptionprotected final BiConsumer<Expression<R,?>, A> getExpressionHandler(String type) Returns the action to execute for the given type of expression.protected final BiConsumer<Filter<R>,A> getFilterHandler(Enum<?> type) Returns the action to execute for the given type of filter.protected final voidremoveFilterHandlers(Collection<? extends Enum<?>> types) Removes all filters of the given types.protected final voidsetBinaryComparisonHandlers(BiConsumer<Filter<R>, A> action) Sets the same action to execute for the <, >, ≤, ≥, = and ≠ types of filter.protected final voidsetBinaryTemporalHandlers(BiConsumer<Filter<R>, A> action) Sets the same action to execute for the temporal comparison operators.protected final voidsetExpressionHandler(String type, BiConsumer<Expression<R, ?>, A> action) Sets the action to execute for the given type of expression.private voidsetExpressionHandlers(BiConsumer<Expression<R, ?>, A> action, String... types) Sets the same action to execute for the given types of expression.private voidsetFamilyHandlers(Enum<?> lastType, BiConsumer<Filter<R>, A> action) Sets the same action for all member of the same family of filters.protected final voidsetFilterHandler(Enum<?> type, BiConsumer<Filter<R>, A> action) Sets the action to execute for the given type of filter.protected final voidsetLogicalHandlers(BiConsumer<Filter<R>, A> action) Sets the same action to execute for theAND,ORandNOTtypes filter.protected final voidsetMathHandlers(BiConsumer<Expression<R, ?>, A> action) Sets the same action to execute for the +, −, × and ÷ expressions.protected final voidsetNullAndNilHandlers(BiConsumer<Filter<R>, A> action) Sets the same action for both theIsNullandIsNiltypes of filter.protected final voidsetSpatialHandlers(BiConsumer<Filter<R>, A> action) Sets the same action to execute for the spatial comparison operators, including the ones with a distance parameter.protected voidtypeNotFound(Enum<?> type, Filter<R> filter, A accumulator) Adds the value to use or throws an exception when there is no action registered for a given filter type.protected voidtypeNotFound(String type, Expression<R, ?> expression, A accumulator) Adds the value to use or throws an exception when there is no action registered for a given expression type.voidvisit(Expression<R, ?> expression, A accumulator) Executes the registered action for the given expression.voidExecutes the registered action for the given filter.
-
Field Details
-
filters
All filters known to this visitor. May contain an entry associated to thenullkey.- See Also:
-
expressions
All expressions known to this visitor. May contain an entry associated to thenullkey.- See Also:
-
-
Constructor Details
-
Visitor
protected Visitor()Creates a new visitor. -
Visitor
protected Visitor(boolean hasFilters, boolean hasExpressions) Creates a new visitor which will accept only the specified type of objects.- Parameters:
hasFilters- whether this filter will accepts filters.hasExpressions- whether this filter will accepts expressions.
-
Visitor
Creates a new visitor initialized to the same handlers than the specified visitor. This constructor can be used for creating a copy from a template before to add or remove handlers. If any of thecopyargument isfalse, it is caller's responsibility to not modify the corresponding map of handlers because it will be an instance shared with thesource.- Parameters:
source- the visitor from which to copy the handlers.copyFilters- whether to copy the map of filter handlers.copyExpressions- whether to copy the map of expression handlers.- See Also:
-
-
Method Details
-
getFilterHandler
Returns the action to execute for the given type of filter. Thenulltype is legal and identifies the action to execute when theFilterinstance is null or has a null type.- Parameters:
type- identification of the filter type (can benull).- Returns:
- the action to execute when the identified filter is found, or
nullif none.
-
getExpressionHandler
Returns the action to execute for the given type of expression. Thenulltype is legal and identifies the action to execute when theExpressioninstance is null.- Parameters:
type- identification of the expression type (can benull).- Returns:
- the action to execute when the identified expression is found, or
nullif none.
-
setFilterHandler
Sets the action to execute for the given type of filter. Thenulltype is legal and identifies the action to execute when theFilterinstance is null or has a null type.- Parameters:
type- identification of the filter type (can benull).action- the action to execute when the identified filter is found.
-
setFamilyHandlers
Sets the same action for all member of the same family of filters. The action is set in enumeration declaration order up to the last type inclusive.- Parameters:
lastType- identification of the last filter type (inclusive).action- the action to execute when an identified filter is found.
-
setExpressionHandler
Sets the action to execute for the given type of expression. Thenulltype is legal and identifies the action to execute when theExpressioninstance is null.- Parameters:
type- identification of the expression type (can benull).action- the action to execute when the identified expression is found.
-
setExpressionHandlers
Sets the same action to execute for the given types of expression.- Parameters:
types- identification of the expression types.action- the action to execute when the identified expression is found.
-
setLogicalHandlers
Sets the same action to execute for theAND,ORandNOTtypes filter.- Parameters:
action- the action to execute when one of the enumerated filters is found.
-
setNullAndNilHandlers
Sets the same action for both theIsNullandIsNiltypes of filter.- Parameters:
action- the action to execute when one of the enumerated filters is found.
-
setBinaryComparisonHandlers
Sets the same action to execute for the <, >, ≤, ≥, = and ≠ types of filter.- Parameters:
action- the action to execute when one of the enumerated filters is found.
-
setBinaryTemporalHandlers
Sets the same action to execute for the temporal comparison operators. The operators areAFTER,BEFORE,BEGINS,BEGUN_BY,CONTAINS,DURING,EQUALS,OVERLAPS,MEETS,ENDS,OVERLAPPED_BY,MET_BY,ENDED_BYandANY_INTERACTStypes filter.- Parameters:
action- the action to execute when one of the enumerated filters is found.
-
setSpatialHandlers
Sets the same action to execute for the spatial comparison operators, including the ones with a distance parameter. The operators areBBOX,EQUALS,DISJOINT,INTERSECTS,TOUCHES,CROSSES,WITHIN,CONTAINS,OVERLAPS,DWITHINandBEYONDtypes filter.- Parameters:
action- the action to execute when one of the enumerated filters is found.
-
setMathHandlers
Sets the same action to execute for the +, −, × and ÷ expressions.- Parameters:
action- the action to execute when one of the enumerated expressions is found.
-
removeFilterHandlers
Removes all filters of the given types. Types that have no registered handlers are ignored.- Parameters:
types- types of filters to remove.
-
visit
Executes the registered action for the given filter. Actions are registered by calls tosetFooHandler(…)before the call to thisvisit(…)method.Note on parameterized type
This method often needs to be invoked with instances ofFilter<? super R>, because this is the type of filters returned by GeoAPI methods such asLogicalOperator.getOperands(). But the parameterized type expected by this method matches the parameterized type of handlers registered bysetFilterHandler(Enum, BiConsumer)and similar methods, which use the exact<R>type. This restriction exists because when doing otherwise, parameterized types become hard to express in Java (we get a cascade ofsuperkeywords, something like<? super ? super R>). However, doing the(Filter<R>) filtercast is actually safe if the handlers do not invoke anyfiltermethod having a return value (directly or indirectly as list elements) restricted to the exact<R>type. Such methods do not exist in the GeoAPI interfaces, so the cast is safe if theBiConsumerhandlers do not invoke implementation-specific methods. Since only subclasses known the details of registered handlers, the decision to cast or not is left to those subclasses.- Parameters:
filter- the filter for which to execute an action based on its type.accumulator- where to write the result of all actions.- Throws:
UnsupportedOperationException- if there is no action registered for the given filter.
-
visit
Executes the registered action for the given expression. Actions are registered by calls tosetFooHandler(…)before the call to thisvisit(…)method.Note on parameterized type
This method often needs to be invoked with instances ofExpression<? super R, ?>, because this is the type of filters returned by GeoAPI methods such asExpression.getParameters(). But the parameterized type expected by this method matches the parameterized type of handlers registered bysetExpressionHandler(String, BiConsumer)and similar methods, which use the exact<R>type. This restriction exists because when doing otherwise, parameterized types become hard to express in Java (we get a cascade ofsuperkeywords, something like<? super ? super R>). However, doing the(Expression<R>,?) expressioncast is actually safe if the handlers do not invoke anyexpressionmethod having a return value (directly or indirectly as list elements) restricted to the exact<R>type. Such methods do not exist in the GeoAPI interfaces, so the cast is safe if theBiConsumerhandlers do not invoke implementation-specific methods. Since only subclasses known the details of registered handlers, the decision to cast or not is left to those subclasses.- Parameters:
expression- the expression for which to execute an action based on its type.accumulator- where to write the result of all actions.- Throws:
UnsupportedOperationException- if there is no action registered for the given expression.
-
typeNotFound
Adds the value to use or throws an exception when there is no action registered for a given filter type. The default implementation throwsUnsupportedOperationException.- Parameters:
type- the filter type which has not been found, ornullif is null.filter- the filter (may benull).accumulator- where to write the result of all actions.- Throws:
UnsupportedOperationException- if there is no default action.
-
typeNotFound
Adds the value to use or throws an exception when there is no action registered for a given expression type. The default implementation throwsUnsupportedOperationException.- Parameters:
type- the expression type which has not been found, ornullif is null.expression- the expression (may benull).accumulator- where to write the result of all actions.- Throws:
UnsupportedOperationException- if there is no default value.
-