coherence.extractor

Extractors

class coherence.extractor.Extractors

A Utility class for creating extractors.

classmethod extract(expression: str, params: list[Any] | None = None) ValueExtractor[T, E]
If providing only an expression, the following rules apply:
  • if the expression contains multiple values separated by a period, the expression will be treated as a chained expression. E.g., the expression ‘a.b.c’ would be treated as extract the ‘a’ property, from that result, extract the ‘b’ property, and finally from that result, extract the ‘c’ property.

  • if the expression contains multiple values separated by a comma, the expression will be treated as a multi expression. E.g., the expression ‘a,b,c’ would be treated as extract the ‘a’, ‘b’, and ‘c’ properties from the same object.

  • for either case, the params argument is ignored.

It is also possible to invoke, and pass arguments to, arbitrary methods. For example, if the target object of the extraction is a String, it’s possible to call the length() function by passing an expression of “length()”. If the target method accepts arguments, provide a list of one or more arguments to be passed.

Parameters:
  • expression – the extractor expression

  • params – the params to pass to the method invocation

Returns:

a ValueExtractor based on the provided inputs

classmethod identity() IdentityExtractor[Any]

Returns an extractor that does not actually extract anything from the passed value, but returns the value itself.

Returns:

an extractor that does not actually extract anything from the passed value, but returns the value itself

ValueExtractor

class coherence.extractor.ValueExtractor

Bases: ABC, Generic[T, E]

__init__() None

ValueExtractor is used to both extract values (for example, for sorting or filtering) from an object, and to provide an identity for that extraction.

Construct a ValueExtractor.

and_then(after: ValueExtractor[T, E]) ValueExtractor[T, E]

Returns a composed extractor that first applies this extractor to its input, and then applies the after extractor to the result. If evaluation of either extractor throws an exception, it is relayed to the caller of the composed extractor.

Parameters:

after – the extractor to apply after this extractor is applied

Returns:

a composed extractor that first applies this extractor and then applies the after extractor

compose(before: ValueExtractor[T, E]) ValueExtractor[T, E]

Returns a composed extractor that first applies the before extractor to its input, and then applies this extractor to the result. If evaluation of either extractor throws an exception, it is relayed to the caller of the composed extractor.

Parameters:

before – the extractor to apply before this extractor is applied

Returns:

a composed extractor that first applies the before extractor and then applies this extractor

classmethod extract(from_field_or_method: str, params: list[Any] | None = None) ValueExtractor[T, E]

Returns an extractor that extracts the value of the specified field.

Parameters:
  • from_field_or_method – the name of the field or method to extract the value from.

  • params – the parameters to pass to the method.

Returns:

an instance of coherence.extractor.UniversalExtractor

UniversalExtractor

class coherence.extractor.UniversalExtractor(name: str, params: list[Any] | None = None)

Bases: ValueExtractor[T, Any]

__init__(name: str, params: list[Any] | None = None) None

Universal ValueExtractor implementation.

Either a property or method based extractor based on parameters passed to constructor. Generally, the name value passed to the UniversalExtractor constructor represents a property unless the name value ends in (), then this instance is a reflection based method extractor.

Construct a UniversalExtractor based on a name and optional parameters.

If name does not end in (), this extractor is a property extractor. If name is prefixed with one of set or get and ends in (), this extractor is a property extractor. If the name just ends in (), this extractor is considered a method extractor.

Parameters:
  • name – A method or property name.

  • params – the parameter array. Must be null or zero length for a property based extractor.

classmethod create(name: str, params: list[Any] | None = None) UniversalExtractor[T]

Class method to create an instance of coherence.extractor.UniversalExtractor

Parameters:
  • name – A method or property name.

  • params – the parameter array. Must be null or zero length for a property based extractor.

Returns:

an instance of coherence.extractor.UniversalExtractor

AbstractCompositeExtractor

class coherence.extractor.AbstractCompositeExtractor(extractors: Sequence[ValueExtractor[T, E]])

Bases: ValueExtractor[T, E]

__init__(extractors: Sequence[ValueExtractor[T, E]]) None

Abstract super class for coherence.extractor.ValueExtractor implementations that are based on an underlying array of coherence.extractor.ValueExtractor objects.

Parameters:

extractors – an array of extractors

ChainedExtractor

class coherence.extractor.ChainedExtractor(extractors_or_method: str | Sequence[ValueExtractor[T, Any]])

Bases: AbstractCompositeExtractor[T, Any]

__init__(extractors_or_method: str | Sequence[ValueExtractor[T, Any]]) None

Composite coherence.extractor.ValueExtractor implementation based on an array of extractors. The extractors in the array are applied sequentially left-to-right, so a result of a previous extractor serves as a target object for a next one.

Parameters:

extractors_or_method – an array of coherence.extractor.ValueExtractor, or a dot-delimited sequence of method names which results in a ChainedExtractor that is based on an array of corresponding coherence.extractor.UniversalExtractor objects

IdentityExtractor

class coherence.extractor.IdentityExtractor

Bases: ValueExtractor[T, Any]

__init__() None

A Trivial coherence.extractor.ValueExtractor implementation that does not actually extract anything from the passed value, but returns the value itself.

Constructs a new IdentityExtractor instance.

ValueUpdater

class coherence.extractor.ValueUpdater

Bases: ABC, Generic[T, R]

__init__() None

ValueUpdater is used to update an object’s state.

Constructs a new ValueUpdater.

ValueManipulator

class coherence.extractor.ValueManipulator(*args, **kwds)

Bases: Generic[T, R]

ValueManipulator represents a composition of coherence.extractor.ValueExtractor and coherence.extractor.ValueUpdater implementations.

__init__()
abstract get_extractor() ValueExtractor[T, R]

Retrieve the underlying ValueExtractor reference.

Return type:

the ValueExtractor

abstract get_updator() ValueUpdater[T, R]

Retrieve the underlying ValueUpdater reference.

Return type:

the ValueUpdater

CompositeUpdater

class coherence.extractor.CompositeUpdater(method_or_extractor: ValueExtractor[T, R] | str, updater: ValueUpdater[T, R] | None = None)

Bases: ValueManipulator[T, R], ValueUpdater[T, R]

A ValueUpdater implementation based on an extractor-updater pair that could also be used as a ValueManipulator.

__init__(method_or_extractor: ValueExtractor[T, R] | str, updater: ValueUpdater[T, R] | None = None) None

Constructs a new CompositeUpdater.

Parameters:
  • method_or_extractor – The ValueExtractor part.

  • updater – The ValueUpdater part.

get_extractor() ValueExtractor[T, R]

Retrieve the underlying ValueExtractor reference.

Return type:

the ValueExtractor

get_updator() ValueUpdater[T, R]

Retrieve the underlying ValueUpdater reference.

Return type:

the ValueUpdater

UniversalUpdater

class coherence.extractor.UniversalUpdater(method: str)

Bases: ValueUpdater[V, R]

Universal ValueUpdater implementation.

Either a property-based and method-based {@link ValueUpdater} based on whether constructor parameter name is evaluated to be a property or method.

__init__(method: str) None

Construct a UniversalUpdater for the provided name.

If method ends in a ‘()’, then the name is a method name. This implementation assumes that a target’s class will have one and only one method with the specified name and this method will have exactly one parameter; if the method is a property name, there should be a corresponding JavaBean property modifier method, or it will be used as a key in a Map.

Parameters:

method – a method or property name