com.yahoo.tensor.functions.ToStringContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vespajlib Show documentation
Show all versions of vespajlib Show documentation
Library for use in Java components of Vespa. Shared code which do
not fit anywhere else.
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.functions;
import com.yahoo.tensor.evaluation.Name;
import com.yahoo.tensor.evaluation.TypeContext;
import java.util.Optional;
/**
* A context which is passed down to all nested functions when returning a string representation.
*
* @author bratseth
*/
public interface ToStringContext {
static ToStringContext empty() { return new EmptyStringContext<>(); }
/** Returns the name an identifier is bound to, or null if not bound in this context */
String getBinding(String name);
/** Returns the name an identifier is bound to, or the input name if none */
default String resolveBinding(String name) {
String binding = getBinding(name);
return binding == null ? name : binding;
}
/**
* Returns the context used to resolve types in this, if present.
* In some functions serialization depends on type information.
*/
default Optional> typeContext() { return Optional.empty(); }
/**
* Returns the parent context of this (the context we're in scope of when this is created),
* or null if this is the root.
*/
ToStringContext parent();
class EmptyStringContext implements ToStringContext {
@Override
public String getBinding(String name) { return null; }
@Override
public ToStringContext parent() { return null; }
}
}