Class Scope
- java.lang.Object
-
- org.inferred.freebuilder.processor.source.Scope
-
- Direct Known Subclasses:
CompilationUnitBuilder.InitialScope,Scope.FileScope,Scope.MethodScope
public abstract class Scope extends java.lang.ObjectAn append-only, hierarchical map with key-specific value typing.Scopes allow source generators to cooperate with each other—to avoid namespace clashes or reuse variables, for example—in a decoupled fashion using a shared key-value space. The key type dictates the type of the value that can be stored in the map, as well as at what level in the source the key should be unique at:
FILEkeys are unique within a compilation unit, whileMETHODkeys are scoped to the current method.While a Scope has to be mutable, it limits the potential for complex bugs by only permitting key-values pairs to be inserted, not modified or removed.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classScope.FileScopestatic interfaceScope.Key<V>static classScope.Level(package private) static classScope.MethodScope
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<Scope.Key<?>,java.lang.Object>entriesprivate Scopeparentprivate static java.lang.ObjectRECURSION_SENTINEL
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract booleancanStore(Scope.Key<?> key)<V> VcomputeIfAbsent(Scope.Key<V> key, java.util.function.Supplier<V> supplier)Ifkeyis not already associated with a value, computes its value usingsupplierand enters it into the scope.booleancontains(Scope.Key<?> key)<V> Vget(Scope.Key<V> key)booleanisEmpty()<V> java.util.Set<V>keysOfType(java.lang.Class<V> keyType)<V> VputIfAbsent(Scope.Key<V> key, V value)Ifkeyis not already associated with a value, associates it withvalue.
-
-
-
Constructor Detail
-
Scope
protected Scope()
-
Scope
protected Scope(Scope parent)
-
-
Method Detail
-
canStore
protected abstract boolean canStore(Scope.Key<?> key)
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(Scope.Key<?> key)
-
get
public <V> V get(Scope.Key<V> key)
-
computeIfAbsent
public <V> V computeIfAbsent(Scope.Key<V> key, java.util.function.Supplier<V> supplier)
Ifkeyis not already associated with a value, computes its value usingsupplierand enters it into the scope.- Returns:
- the current (existing or computed) value associated with
key
-
keysOfType
public <V> java.util.Set<V> keysOfType(java.lang.Class<V> keyType)
-
putIfAbsent
public <V> V putIfAbsent(Scope.Key<V> key, V value)
Ifkeyis not already associated with a value, associates it withvalue.- Returns:
- the original value, or
nullif there was no value associated
-
-