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

com.github.phantomthief.view.mapper.impl.DefaultViewMapperImpl Maven / Gradle / Ivy

/**
 * 
 */
package com.github.phantomthief.view.mapper.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;

import org.apache.commons.lang3.ClassUtils;

import com.github.phantomthief.model.builder.context.BuildContext;
import com.github.phantomthief.view.mapper.ViewMapper;

/**
 * 

DefaultViewMapperImpl class.

* * @author w.vela * @version $Id: $Id */ public class DefaultViewMapperImpl implements ViewMapper { private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(getClass()); private final Map, BiFunction> mappers = new HashMap<>(); /** {@inheritDoc} */ @SuppressWarnings({ "unchecked" }) @Override public V map(M model, B buildContext) { V view = (V) getMapper(model.getClass()).apply(buildContext, model); return view; } private final ConcurrentMap, BiFunction> modelTypeCache = new ConcurrentHashMap<>(); @SuppressWarnings("rawtypes") private BiFunction getMapper(Class modelType) { return modelTypeCache.computeIfAbsent(modelType, t -> { BiFunction result = mappers.get(t); if (result == null) { for (Class c : ClassUtils.getAllInterfaces(t)) { result = mappers.get(c); if (result != null) { return result; } } for (Class c : ClassUtils.getAllSuperclasses(t)) { result = mappers.get(c); if (result != null) { return result; } } } if (result == null) { logger.warn("cannot found model's view:{}", modelType); } return result; }); } /** *

addMapper.

* * @param modelType a {@link java.lang.Class} object. * @param viewFactory a {@link java.util.function.BiFunction} object. * @param a M object. * @param a V object. * @return a {@link com.github.phantomthief.view.mapper.impl.DefaultViewMapperImpl} object. */ public DefaultViewMapperImpl addMapper(Class modelType, BiFunction viewFactory) { mappers.put(modelType, viewFactory); return this; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy