IndexAccessor class abstract interface

A plug-in that makes target[index] work for your own indexable types.

Lists, maps and strings are indexed without any accessor. For any other target the context's index accessors are asked in order before the fallbacks (other iterables by position, then obj['name'] as a property read). Implement it for a type that has its own notion of "element at", such as a matrix row or a sparse vector. Spring added the same interface in 6.2.

// `#root[1]` reads row 1 of a Matrix.
final class MatrixRowAccessor implements IndexAccessor {
  @override
  bool canRead(EvaluationContext c, Object target, Object? index) =>
      target is Matrix && index is int;

  @override
  Object? read(EvaluationContext c, Object target, Object? index) =>
      (target as Matrix).row(index! as int);

  @override
  bool canWrite(EvaluationContext c, Object t, Object? i) => false;

  @override
  void write(EvaluationContext c, Object t, Object? i, Object? v) {}
}

final context = StandardEvaluationContext(matrix)
  ..addIndexAccessor(MatrixRowAccessor());
SpelExpressionParser().parseExpression('#root[1]').getValue(context);

Spring (Java):

StandardEvaluationContext context = new StandardEvaluationContext(matrix);
context.addIndexAccessor(new MatrixRowAccessor());

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

canRead(EvaluationContext context, Object target, Object? index) → bool
Whether this accessor handles reading index (the evaluated index value) of target. An accessor that answers true to canRead or canWrite takes over the index for both reads and writes.
canWrite(EvaluationContext context, Object target, Object? index) → bool
Whether this accessor handles writing index of target.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read(EvaluationContext context, Object target, Object? index) → Object?
The element at index of target; only called after canRead returned true. Other exceptions than a SpelEvaluationException are reported as exceptionDuringIndexRead (EL1083E).
toString() → String
A string representation of this object.
inherited
write(EvaluationContext context, Object target, Object? index, Object? newValue) → void
Stores newValue at index of target; only called after canWrite returned true. Other exceptions than a SpelEvaluationException are reported as exceptionDuringIndexWrite (EL1084E).

Operators

operator ==(Object other) → bool
The equality operator.
inherited