package io.ebeaninternal.server.dto; import io.ebean.meta.MetricVisitor; import io.ebeaninternal.server.type.TypeManager; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Manages all the DTO bean descriptors. */ public class DtoBeanManager { private static final Map EMPTY_NAMED_QUERIES = new HashMap<>(); private final TypeManager typeManager; private final Map, DtoNamedQueries> namedQueries; private final Map descriptorMap = new ConcurrentHashMap<>(); public DtoBeanManager(TypeManager typeManager, Map, DtoNamedQueries> namedQueries) { this.typeManager = typeManager; this.namedQueries = namedQueries; } /** * Return the descriptor for the given DTO bean class. */ @SuppressWarnings("unchecked") public DtoBeanDescriptor getDescriptor(Class dtoType) { return descriptorMap.computeIfAbsent(dtoType, this::createDescriptor); } private DtoBeanDescriptor createDescriptor(Class dtoType) { try { DtoMeta meta = new DtoMetaBuilder(dtoType, typeManager).build(); return new DtoBeanDescriptor<>(dtoType, meta, namedQueries(dtoType)); } catch (Exception e) { throw new IllegalStateException(e); } } private Map namedQueries(Class dtoType) { DtoNamedQueries namedQueries = this.namedQueries.get(dtoType); return (namedQueries == null) ? EMPTY_NAMED_QUERIES : namedQueries.map(); } public void visitMetrics(MetricVisitor visitor) { for (DtoBeanDescriptor value : descriptorMap.values()) { value.visit(visitor); } } }