All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.virtdata.api.DataMapperLibrary Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
package io.virtdata.api;

import io.virtdata.core.DataMapperFunctionMapper;
import io.virtdata.core.ResolvedFunction;

import java.util.List;
import java.util.Optional;

/**
 * 

* A DataMapperLibrary is an independently loadable library of data mapping functions. *

*/ public interface DataMapperLibrary { /** *

Return the name for this data mapper implementation, as it can be used in spec strings, etc.

* * @return Simple lower-case canonical library name */ String getLibraryName(); /** *

Find the implementation for and construct an instance of a data mapper function, as described.

* * @param spec A specifier that describes the type and or parameterization of a data mapping function instance. * @param The result type produced by the data mapping function. * @return An optional data mapping function instance */ default Optional> getDataMapper(String spec) { if (canParseSpec(spec)) { Optional resolvedFunction = resolveFunction(spec); return resolvedFunction .map(ResolvedFunction::getFunctionObject) .map(DataMapperFunctionMapper::map); } return Optional.empty(); } default Optional> getOptionalDataMapper(String spec, Class clazz) { return Optional.ofNullable(getDataMapper(spec, clazz)); } @SuppressWarnings("unchecked") default DataMapper getDataMapper(String spec, Class clazz) { if (!canParseSpec(spec)) { return null; } Optional resolvedFunction = resolveFunction(spec); if (!resolvedFunction.isPresent()) { return null; } ResolvedFunction rf = resolvedFunction.get(); DataMapper dm = DataMapperFunctionMapper.map(rf.getFunctionObject()); return (DataMapper) dm; } /** * DataMapper Libraries are required to test specifier strings in order to determine * whether or not the library could possibly find matching functions. * This allows varying types of specifiers to be used that are library specific, * allowing an ad-hoc form of syntax layering. * * @param spec a data mapping function spec * @return a tagged Specifier option if successful */ boolean canParseSpec(String spec); Optional resolveFunction(String spec); /** * @param specifier A specifier that describes the type and parameterization of a data mapping function instance. * The type of specifier will be specific to your library implementation. You can use SpecData by default. * @return a list of function instances */ List resolveFunctions(String specifier); /** *

Get the list of known data mapping function names.

* * @return list of data mapping function names that can be used in specifiers */ List getDataMapperNames(); // default Optional> getDataMapper(String spec) { default Optional> getLongDataMapper(String spec) { if (!canParseSpec(spec)) { return Optional.empty(); } Optional resolvedFunction = resolveFunction(spec); Optional> mapper = resolvedFunction .map(ResolvedFunction::getFunctionObject) .map(DataMapperFunctionMapper::map); return mapper; } default Optional> getDoubleDataMapper(String spec) { if (!canParseSpec(spec)) { return Optional.empty(); } Optional resolvedFunction = resolveFunction(spec); Optional> mapper = resolvedFunction .map(ResolvedFunction::getFunctionObject) .map(DataMapperFunctionMapper::map); return mapper; } default Optional> getIntegerDataMapper(String spec) { if (!canParseSpec(spec)) { return Optional.empty(); } Optional resolvedFunction = resolveFunction(spec); Optional> mapper = resolvedFunction .map(ResolvedFunction::getFunctionObject) .map(DataMapperFunctionMapper::map); return mapper; } default Optional> getStringDataMapper(String spec) { if (!canParseSpec(spec)) { return Optional.empty(); } Optional resolvedFunction = resolveFunction(spec); Optional> mapper = resolvedFunction .map(ResolvedFunction::getFunctionObject) .map(DataMapperFunctionMapper::map); return mapper; } }