Interface Context

    • Method Detail

      • get

        Object get​(String key)
        Parameters:
        key - Object key (property name).
        Returns:
        Property by a key.
      • getString

        default String getString​(String key)
        Convenience method for retrieving string properties.
        Parameters:
        key -
        Returns:
      • getString

        default String getString​(String key,
                                 String defaultValue)
        Convenience method for retrieving string properties.
        Parameters:
        key -
        defaultValue -
        Returns:
      • get

        default Object get​(String key,
                           Object defaultValue)
        Parameters:
        key - Object key (property name).
        defaultValue - value to return if key is not present.
        Returns:
        Property by key or default value if property is not present.
      • get

        default Object get​(String key,
                           Function<String,​Object> computer)
        Parameters:
        key - Object key (property name).
        computer - FunctionFactory to compute value if key is not present.
        Returns:
        Property by key of a value returned by the computer if property is not present.
      • get

        <T> T get​(Class<T> type)
        Type Parameters:
        T - Service type.
        Parameters:
        type - Service type class.
        Returns:
        Service of a particular type. Multiple service instances maybe obtained by passing an array type.
      • get

        default <T> T get​(Class<T> type,
                          T defaultService)
        Type Parameters:
        T - Service type.
        Parameters:
        type - Service type class.
        defaultService - Default service implementation to return if no service is found.
        Returns:
        Service of a particular type or default service if no service of requested type is found in the context. Multiple service instances maybe obtained by passing an array type.
      • get

        default <T> T get​(Class<T> type,
                          Function<Class<T>,​T> computer)
        Type Parameters:
        T - Service type.
        Parameters:
        type - Service type class.
        computer - FunctionFactory to compute a service instance if none is found in the context.
        Returns:
        Service of a particular type or a service returned by the computer if not service of requested type is found in the context. Multiple service instances maybe obtained by passing an array type.
      • get

        default <T> T get​(Class<T> type,
                          Predicate<? super T> predicate)
        Type Parameters:
        T - Service type.
        Parameters:
        type - Service type class.
        predicate - Service predicate (filter).
        Returns:
        Service of a particular type. Multiple service instances maybe obtained by passing an array type. To filter multiple instances use Predicate<Object> for compatibility with both array and component types.
      • get

        default <T> T get​(Class<T> type,
                          Predicate<? super T> predicate,
                          T defaultService)
        Type Parameters:
        T - Service type.
        Parameters:
        type - Service type class.
        predicate - Service predicate (filter).
        defaultService - Default service implementation to return if no service is found.
        Returns:
        Service of a particular type. Multiple service instances maybe obtained by passing an array type. To filter multiple instances use Predicate<Object> for compatibility with both array and component types.
      • get

        default <T> T get​(Class<T> type,
                          Predicate<? super T> predicate,
                          Function<Class<T>,​T> computer)
        Type Parameters:
        T - Service type.
        Parameters:
        type - Service type class.
        predicate - Service predicate (filter).
        computer - FunctionFactory to compute a service instance if none is found in the context.
        Returns:
        Service of a particular type. Multiple service instances maybe obtained by passing an array type. To filter multiple instances use Predicate<Object> for compatibility with both array and component types.
      • get

        default <T> T get​(String key,
                          Class<T> type)
        Returns a property converted to the requested type. E.g. property which is a File or URL may be converted to InputStream, Reader or String. The default implementation looks up Converter service to convert the value returned by get(String) to requested type and falls-back to DefaultConverter.INSTANCE if the converter service is not present in the context.
        Type Parameters:
        T - Property type.
        Parameters:
        key - Object key (property/service name).
        type - Property type class.
        Returns:
        Property by key converted to requested type.
      • get

        default <T> T get​(String key,
                          Class<T> type,
                          T defaultValue)
        Returns a property converted to the requested type. E.g. property which is a File or URL may be converted to InputStream, Reader or String. The default implementation looks up Converter service to convert the value returned by get(String) to requested type and falls-back to DefaultConverter.INSTANCE if the converter service is not present in the context.
        Type Parameters:
        T - Property/service type.
        Parameters:
        key - Object key (property/service name).
        type - Property/service type class.
        defaultValue - Default property value.
        Returns:
        Property by key converted to requested type or default value if property is not found.
      • get

        default <T> T get​(String key,
                          Class<T> type,
                          BiFunction<String,​Class<T>,​T> computer)
        Returns a property converted to the requested type. E.g. property which is a File or URL may be converted to InputStream, Reader or String. The default implementation looks up Converter service to convert the value returned by get(String) to requested type and falls-back to DefaultConverter.INSTANCE if the converter service is not present in the context.
        Type Parameters:
        T - Property/service type.
        Parameters:
        key - Object key (property/service name).
        type - Property/service type class.
        computer - FunctionFactory to compute property value if none is found in the context.
        Returns:
        Property by key converted to requested type or computed value if property is not present.
      • mount

        default Context mount​(Context context,
                              String prefix)
        Mounts a context under a specified prefix. get(String) and get(String,Class) methods look up property/source in the mounted context first if the key start with the prefix, removing the prefix. E.g. if the prefix is resources/ then get("resources/myResource") will call get("myResource") in the mounted resource first and will return a value if it is not null. If the value returned by the mounted context is null, then the value from get("resources/myResource") call to this context will be returned. In other words, the mounted value shadows this context value, but doesn't hide it. Typed service calls are always made against this context.
        Parameters:
        context - Context to mount.
        prefix - Mount prefix.
        Returns:
        This context if argument context or argument prefix are null null or a context which delegates get(String) and get(String,Class) to the mounted context if the key start with the prefix.
      • map

        default Context map​(Function<String,​String> mapper)
        Parameters:
        mapper - Key mapping function.
        Returns:
        A context which maps the key using the mapper function in get(String> and get(String,Class) methods. Returns this context if mapper is null.
      • compose

        default Context compose​(Context other)
        Returns this context if other is null, or a new context which delegates to this context and falls back to the other if property/service is not found in this context.
        Specified by:
        compose in interface Composeable<Context>
        Parameters:
        chain -
        Returns:
      • fork

        default MutableContext fork()
        Creates a new mutable context backed by this context. This call is equivalent to ``fork(null,null)``.
        Returns:
      • fork

        default MutableContext fork​(Function<String,​String> mapper,
                                    Predicate<Object> servicePredicate)
        Creates a new mutable context with a given prefix and service predicate.
        Parameters:
        mapper - Mapper functions, the same as in map();
        servicePredicate - Predicate to use during look up of parent services. One possible scenario is to filter-out Composeable services from the parent.
        Returns:
      • interpolate

        default Object interpolate​(String input)
        Expands tokens in the form of ``${token name|default value}`` to their values. If a token is not found the default value after the pipe is used for expansion. If there is no default value then the expansion is not processed. Token names shall not contain pipes. If replacement is not null and there is a Converter service then the converter is used to convert the replacement to String. If no such conversion can be done then toString() is used as default conversion. Add extra open/closing curly brackets to escape the token, e.g. in documentation. For example ${{xyz}} will be "interpolated" as ${xyz}. There may be multiple levels of curly brackets. This behavior may be leveraged in multi-stage interpolation where early stage interpolators "peel" the token until it comes at the interpolation level where it is supposed to be expanded.
        Parameters:
        input -
        Returns:
        If the string contains just a single token then the token value is returned as-is. Otherwise tokens in the input are replaced with their values converted to string using context Converter service if it is present or toString() method.
      • interpolate

        default <K> Map<K,​Objectinterpolate​(Map<K,​?> input)
        Creates a copy of this map using LinkedHashMap with String values interpolated and Map and Collection values passed through interpolate methods with respective parameter types.
        Parameters:
        input -
        Returns:
        A deep copy of the map with string values interpolated.
      • interpolate

        default List<Objectinterpolate​(Collection<?> input)
        Creates a copy of this map using ArrayList with String values interpolated and Map and Collection values passed through interpolate methods with respective parameter types.
        Parameters:
        input -
        Returns:
        A deep copy of the collection with string values interpolated.
      • cachingContext

        default Context cachingContext()
        Returns:
        A context backed by this context which caches values of propeties by key and of services by type.
      • computingContext

        default Context computingContext()
        Computing context retrieves properties from this context. If a property is found and is of PropertyComputer type, then the property computer is invoked to produce value otherwise the value is returned as is. If a property for a given key is not found, the computer traverses the key hierarchy up. Slash (/) is a key separator. When it finds a parent property value then is computes the property value in the following way - for a property computer its compute() method is invoked, for Context the path from the parent property is used to retrieve value, for Map its get method is used, for List an element is retrived if key path is parseable to int, for Function the path is passed to it as an argument and the value is returned as property value.
        Returns:
      • singleton

        static <T> Context singleton​(Class<T> type,
                                     T service)
        Creates a context with a single service entry.
        Parameters:
        key -
        value -
        Returns:
      • singleton

        static Context singleton​(String key,
                                 Object value)
        Creates a context with a single property entry.
        Parameters:
        key -
        value -
        Returns:
      • wrap

        static Context wrap​(Function<String,​Object> source)
        Wraps a source function, e.g. Map::get into a context. Performs resolution of hierarchical properties.
        Parameters:
        source - Source function.
        Returns: