com.jparams.object.builder.provider.CachedDataProvider Maven / Gradle / Ivy
package com.jparams.object.builder.provider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.jparams.object.builder.Context;
import com.jparams.object.builder.path.Path;
public class CachedDataProvider implements Provider
{
private final Map cache = new HashMap<>();
private final List providers;
private final int cacheStart;
public CachedDataProvider(final List providers, final int cacheStart)
{
this.providers = new ArrayList<>(providers);
this.cacheStart = cacheStart;
}
@Override
public Object provide(final Context context)
{
if (!cache.containsKey(context.getPath()) || context.getPath().getDepth() < cacheStart)
{
final Optional provider = findSupportingProvider(context.getPath().getMemberType().getType());
if (provider.isPresent())
{
final Object obj = provider.get().provide(context);
cache.put(context.getPath(), obj);
return obj;
}
return null;
}
return cache.get(context.getPath());
}
@Override
public boolean supports(final Class> clazz)
{
return findSupportingProvider(clazz).isPresent();
}
private Optional findSupportingProvider(final Class> clazz)
{
return providers.stream()
.filter(provider -> provider.supports(clazz))
.findFirst();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy