- java.lang.Object
-
- swim.structure.Item
-
- swim.structure.Value
-
- swim.structure.Expression
-
- swim.structure.Selector
-
- Direct Known Subclasses:
ChildrenSelector
,DescendantsSelector
,FilterSelector
,GetAttrSelector
,GetItemSelector
,GetSelector
,IdentitySelector
,KeysSelector
,LiteralSelector
,ValuesSelector
public abstract class Selector extends Expression
AnExpression
that returns references toItems
when it isevaluated
. Because most application-levelItems
areRecords
, a way to only extract certain parts ofRecords
is often required. Technically, this can be accomplished withoutSelectors
to some extent because theRecord
class implementsjava.util.List<Item>
and (implicitly)java.util.Map<Value,Value>
; however,Selectors
additionally expose functional patterns that enhance composability, providing a foundation on top of which expression languages can be built.
-
-
Constructor Summary
Constructors Constructor Description Selector()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Selector
andThen(Selector then)
The means to chainSelectors
.Selector
children()
int
compareTo(Item other)
protected abstract int
compareTo(Selector that)
<T> Output<T>
debug(Output<T> output)
Writes a developer readable, debug-formatted string representation of this object tooutput
.abstract <T> Output<T>
debugThen(Output<T> output)
Selector
descendants()
Item
evaluate(Interpreter interpreter)
Evaluates thisSelector
against someInterpreter
.FilterSelector
filter()
Selector
filter(Item predicate)
abstract <T> T
forSelected(Interpreter interpreter, Selectee<T> callback)
Selector
get(String key)
Selector
get(Value key)
Selector
getAttr(String key)
Selector
getAttr(Text key)
Selector
getItem(int index)
Selector
getItem(Num index)
static Selector
identity()
Operator
invoke(Value args)
Creates, but does not evaluate, anInvokeOperator
where thisSelector
evaluates to the operator.boolean
isConstant()
Selector
keys()
static Selector
literal(Item item)
abstract Item
mapSelected(Interpreter interpreter, Selectee<Item> transform)
int
precedence()
abstract Selector
then()
Returns theSelector
that thisSelector
uses to match sub-selections.Selector
values()
-
Methods inherited from class swim.structure.Expression
and, and, bitwiseAnd, bitwiseAnd, bitwiseNot, bitwiseOr, bitwiseOr, bitwiseXor, bitwiseXor, conditional, conditional, divide, divide, eq, eq, ge, ge, gt, gt, inverse, le, le, lt, lt, minus, minus, modulo, modulo, ne, ne, negative, not, or, or, plus, plus, positive, times, times
-
Methods inherited from class swim.structure.Value
absent, alias, body, booleanValue, booleanValue, branch, builder, byteValue, byteValue, charValue, charValue, commit, contains, containsKey, containsKey, containsValue, doubleValue, doubleValue, empty, extant, flattened, floatValue, floatValue, fromObject, getField, getField, getSlot, getSlot, head, header, headers, integerValue, integerValue, intValue, intValue, isAliased, isDefined, isDefinite, isDistinct, isMutable, key, keyEquals, lambda, length, longValue, longValue, numberValue, numberValue, removed, removed, shortValue, shortValue, stringValue, stringValue, tag, tail, target, toValue, unflattened
-
Methods inherited from class swim.structure.Item
appended, appended, appended, appended, appended, appended, appended, appended, cast, cast, coerce, coerce, concat, display, equals, evaluate, globalScope, hashCode, iterator, max, min, prepended, prepended, prepended, prepended, prepended, prepended, prepended, prepended, substitute, substitute, toString, typeOrder, updated, updated, updated, updated, updated, updated, updated, updated, updated, updated, updated, updated, updated, updated, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedAttr, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot, updatedSlot
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
isConstant
public boolean isConstant()
Description copied from class:Item
- Specified by:
isConstant
in classItem
-
then
public abstract Selector then()
Returns theSelector
that thisSelector
uses to match sub-selections.
-
forSelected
public abstract <T> T forSelected(Interpreter interpreter, Selectee<T> callback)
Evaluatescallback.selected
against theItems
that match thisSelector's
selection criteria. That is, it pushes suchItems
tointerpreter
, then invokescallback
against it. To support chainedSelectors
, this is a recursive procedure that invokesforSelected
throughthis.then
wherever it exists (which it always does outside ofIdentitySelector
); we define "subselection" to be such an invocation.- Returns:
- the result of executing
callback
from the context of the lastSelector
in the chain formed bySelector then
fields.
-
mapSelected
public abstract Item mapSelected(Interpreter interpreter, Selectee<Item> transform)
-
evaluate
public final Item evaluate(Interpreter interpreter)
Evaluates thisSelector
against someInterpreter
. This is accomplished by creating a newSelecteeBuilder
and populating its internalRecord
with (recursive) calls toforSelected(swim.structure.Interpreter, swim.structure.Selectee<T>)
.
-
andThen
public abstract Selector andThen(Selector then)
The means to chainSelectors
. By intention, this is NOT a strict functional composition: for twoSelectors
s1
ands2
,s1.andThen(s2)
DOES NOT NECESSARILY return a newSelector
s3
such thats3.evaluate(args)
is equivalent tos2.evaluate(s1.evaluate(args))
.The reason for this is that for
Selectors
likeChildrenSelector
that yield (logical) collections, we wish to invoke the nextSelector
, say aGetSelector
, against every result. Under strict functional rules,ChildrenSelector.andThen(someGetSelector).evaluate(args)
would instead return at most one defined value regardless of the number of children.
-
getItem
public Selector getItem(Num index)
- Parameters:
index
- theindex
field in the composingGetItemSelector
.
-
getItem
public Selector getItem(int index)
-
keys
public Selector keys()
-
values
public Selector values()
-
children
public Selector children()
-
descendants
public Selector descendants()
-
filter
public FilterSelector filter()
-
invoke
public Operator invoke(Value args)
Creates, but does not evaluate, anInvokeOperator
where thisSelector
evaluates to the operator.
-
precedence
public int precedence()
- Overrides:
precedence
in classItem
-
debug
public <T> Output<T> debug(Output<T> output)
Description copied from interface:Debug
Writes a developer readable, debug-formatted string representation of this object tooutput
.
-
compareTo
public int compareTo(Item other)
- Specified by:
compareTo
in interfaceComparable<Item>
- Specified by:
compareTo
in classItem
-
compareTo
protected abstract int compareTo(Selector that)
-
identity
public static Selector identity()
-
-