<commit_before>/*
 *
 *  * Copyright 2016 http://www.hswebframework.org
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *     http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *  
 */

package org.hswebframework.web.commons.entity.factory;

import org.apache.commons.beanutils.BeanUtils;
import org.hswebframework.web.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author zhouhao
 * @since 3.0
 */
public class MapperEntityFactory implements EntityFactory {
    private Map<Class, Mapper> realTypeMapper = new HashMap<>();
    private Logger             logger         = LoggerFactory.getLogger(this.getClass());

    public MapperEntityFactory() {
    }

    public <T> MapperEntityFactory(Map<Class<T>, Mapper> realTypeMapper) {
        this.realTypeMapper.putAll(realTypeMapper);
    }

    public <T> MapperEntityFactory addMapping(Class<T> target, Mapper<T> mapper) {
        realTypeMapper.put(target, mapper);
        return this;
    }

    @Override
    public <S, T> T copyProperties(S source, T target) {
        try {
            // TODO: 17-3-30 应该设计为可自定义
            BeanUtils.copyProperties(target, source);
        } catch (Exception e) {
            logger.warn("copy properties error", e);
        }
        return target;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T newInstance(Class<T> beanClass) {
        if (beanClass == null) return null;
        Mapper<T> mapper = realTypeMapper.get(beanClass);
        if (mapper != null) return mapper.getInstanceGetter().get();
        synchronized (beanClass) {
            mapper = realTypeMapper.get(beanClass);
            if (mapper != null) return mapper.getInstanceGetter().get();
            Class<T> realType = null;
            if (!Modifier.isInterface(beanClass.getModifiers()) && !Modifier.isAbstract(beanClass.getModifiers())) {
                realType = beanClass;
            }
            //尝试使用 Simple类，如: package.SimpleUserBean
            if (realType == null) {
                String simpleClassName = beanClass.getPackage().getName().concat(".Simple").concat(beanClass.getSimpleName());
                try {
                    realType = (Class<T>) Class.forName(simpleClassName);
                    mapper = new Mapper<>(realType, new DefaultInstanceGetter(realType));
                    realTypeMapper.put(beanClass, mapper);
                    return mapper.getInstanceGetter().get();
                } catch (ClassNotFoundException e) {
                    throw new NotFoundException(e.getMessage());
                }
            }<commit_msg>优化类型获取逻辑<commit_after>/*
 *
 *  * Copyright 2016 http://www.hswebframework.org
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *     http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *  
 */

package org.hswebframework.web.commons.entity.factory;

import org.apache.commons.beanutils.BeanUtils;
import org.hswebframework.web.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author zhouhao
 * @since 3.0
 */
public class MapperEntityFactory implements EntityFactory {
    private Map<Class, Mapper> realTypeMapper = new HashMap<>();
    private Logger             logger         = LoggerFactory.getLogger(this.getClass());

    public MapperEntityFactory() {
    }

    public <T> MapperEntityFactory(Map<Class<T>, Mapper> realTypeMapper) {
        this.realTypeMapper.putAll(realTypeMapper);
    }

    public <T> MapperEntityFactory addMapping(Class<T> target, Mapper<T> mapper) {
        realTypeMapper.put(target, mapper);
        return this;
    }

    @Override
    public <S, T> T copyProperties(S source, T target) {
        try {
            // TODO: 17-3-30 应该设计为可自定义
            BeanUtils.copyProperties(target, source);
        } catch (Exception e) {
            logger.warn("copy properties error", e);
        }
        return target;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T newInstance(Class<T> beanClass) {
        if (beanClass == null) return null;
        Mapper<T> mapper = realTypeMapper.get(beanClass);
        if (mapper != null) return mapper.getInstanceGetter().get();
        synchronized (beanClass) {
            mapper = realTypeMapper.get(beanClass);
            if (mapper != null) return mapper.getInstanceGetter().get();
            Class<T> realType = null;
            if (!Modifier.isInterface(beanClass.getModifiers()) && !Modifier.isAbstract(beanClass.getModifiers())) {
                realType = beanClass;
            }
            //尝试使用 Simple类，如: package.SimpleUserBean
            if (realType == null) {
                String simpleClassName = beanClass.getPackage().getName().concat(".Simple").concat(beanClass.getSimpleName());
                try {
                    realType = (Class<T>) Class.forName(simpleClassName);
                } catch (ClassNotFoundException e) {
                    throw new NotFoundException(e.getMessage());
                }
            }
            if (realType != null) {
                mapper = new Mapper<>(realType, new DefaultInstanceGetter(realType));
                realTypeMapper.put(beanClass, mapper);
                return mapper.getInstanceGetter().get();
            }
{'fraction_non_alphanumeric': 0.10944504560210233, 'fraction_numerical': 0.005255835523264802, 'mean_word_length': 2.549094898518925, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776371', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1809, 'n_words': 457}