- 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
AnExpressionthat returns references toItemswhen it isevaluated. Because most application-levelItemsareRecords, a way to only extract certain parts ofRecordsis often required. Technically, this can be accomplished withoutSelectorsto some extent because theRecordclass implementsjava.util.List<Item>and (implicitly)java.util.Map<Value,Value>; however,Selectorsadditionally 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 SelectorandThen(Selector then)The means to chainSelectors.Selectorchildren()intcompareTo(Item other)protected abstract intcompareTo(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)Selectordescendants()Itemevaluate(Interpreter interpreter)Evaluates thisSelectoragainst someInterpreter.FilterSelectorfilter()Selectorfilter(Item predicate)abstract <T> TforSelected(Interpreter interpreter, Selectee<T> callback)Selectorget(String key)Selectorget(Value key)SelectorgetAttr(String key)SelectorgetAttr(Text key)SelectorgetItem(int index)SelectorgetItem(Num index)static Selectoridentity()Operatorinvoke(Value args)Creates, but does not evaluate, anInvokeOperatorwhere thisSelectorevaluates to the operator.booleanisConstant()Selectorkeys()static Selectorliteral(Item item)abstract ItemmapSelected(Interpreter interpreter, Selectee<Item> transform)intprecedence()abstract Selectorthen()Returns theSelectorthat thisSelectoruses to match sub-selections.Selectorvalues()-
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:
isConstantin classItem
-
then
public abstract Selector then()
Returns theSelectorthat thisSelectoruses to match sub-selections.
-
forSelected
public abstract <T> T forSelected(Interpreter interpreter, Selectee<T> callback)
Evaluatescallback.selectedagainst theItemsthat match thisSelector'sselection criteria. That is, it pushes suchItemstointerpreter, then invokescallbackagainst it. To support chainedSelectors, this is a recursive procedure that invokesforSelectedthroughthis.thenwherever it exists (which it always does outside ofIdentitySelector); we define "subselection" to be such an invocation.- Returns:
- the result of executing
callbackfrom the context of the lastSelectorin the chain formed bySelector thenfields.
-
mapSelected
public abstract Item mapSelected(Interpreter interpreter, Selectee<Item> transform)
-
evaluate
public final Item evaluate(Interpreter interpreter)
Evaluates thisSelectoragainst someInterpreter. This is accomplished by creating a newSelecteeBuilderand populating its internalRecordwith (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 twoSelectorss1ands2,s1.andThen(s2)DOES NOT NECESSARILY return a newSelectors3such thats3.evaluate(args)is equivalent tos2.evaluate(s1.evaluate(args)).The reason for this is that for
SelectorslikeChildrenSelectorthat 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- theindexfield 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, anInvokeOperatorwhere thisSelectorevaluates to the operator.
-
precedence
public int precedence()
- Overrides:
precedencein classItem
-
debug
public <T> Output<T> debug(Output<T> output)
Description copied from interface:DebugWrites a developer readable, debug-formatted string representation of this object tooutput.
-
compareTo
public int compareTo(Item other)
- Specified by:
compareToin interfaceComparable<Item>- Specified by:
compareToin classItem
-
compareTo
protected abstract int compareTo(Selector that)
-
identity
public static Selector identity()
-
-