use of org.projectnessie.cel.common.types.ref.TypeProvider in project cel-java by projectnessie.
the class Env method extend.
/**
* Extend the current environment with additional options to produce a new Env.
*
* <p>Note, the extended Env value should not share memory with the original. It is possible,
* however, that a CustomTypeAdapter or CustomTypeProvider options could provide values which are
* mutable. To ensure separation of state between extended environments either make sure the
* TypeAdapter and TypeProvider are immutable, or that their underlying implementations are based
* on the ref.TypeRegistry which provides a Copy method which will be invoked by this method.
*/
public Env extend(List<EnvOption> opts) {
if (chkErr != null) {
throw chkErr;
}
// Copy slices.
List<Decl> decsCopy = new ArrayList<>(declarations);
List<Macro> macsCopy = new ArrayList<>(macros);
List<ProgramOption> progOptsCopy = new ArrayList<>(progOpts);
// Copy the adapter / provider if they appear to be mutable.
TypeAdapter adapter = this.adapter;
TypeProvider provider = this.provider;
// TypeRegistry as the base implementation are captured below.
if (this.adapter instanceof TypeRegistry && this.provider instanceof TypeRegistry) {
TypeRegistry adapterReg = (TypeRegistry) this.adapter;
TypeRegistry providerReg = (TypeRegistry) this.provider;
TypeRegistry reg = providerReg.copy();
provider = reg;
// to the same ref.TypeRegistry as the provider.
if (adapterReg.equals(providerReg)) {
adapter = reg;
} else {
// Otherwise, make a copy of the adapter.
adapter = adapterReg.copy();
}
} else if (this.provider instanceof TypeRegistry) {
provider = ((TypeRegistry) this.provider).copy();
} else if (this.adapter instanceof TypeRegistry) {
adapter = ((TypeRegistry) this.adapter).copy();
}
Set<EnvFeature> featuresCopy = EnumSet.copyOf(this.features);
Env ext = new Env(this.container, decsCopy, macsCopy, adapter, provider, featuresCopy, progOptsCopy);
return ext.configure(opts);
}
Aggregations