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

io.ultreia.java4all.classmapping.ClassMapping Maven / Gradle / Ivy

package io.ultreia.java4all.classmapping;

/*-
 * #%L
 * Class Mapping tools
 * %%
 * Copyright (C) 2017 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by tchemit on 16/07/17.
 *
 * @author Tony Chemit - [email protected]
 */
public class ClassMapping {

    /** Logger. */
    private static final Log log = LogFactory.getLog(ClassMapping.class);

    private final Map mapping = new LinkedHashMap<>();

    private final String keyPackage;
    private final String implementationType;
    private final String classSuffix;

    public ClassMapping(String keyPackage, String implementationType, String classSuffix) {
        this.keyPackage = keyPackage;
        this.implementationType = implementationType;
        this.classSuffix = classSuffix;
    }

    protected ClassMapping(Package keyPackage, Package implementationType, String classSuffix) {
        this(keyPackage.getName(), implementationType.getName(), classSuffix);
    }

//    public String getLocation(Class implementationType, boolean removeClassSuffix) {
//        String result = implementationType.getCanonicalName().replace(this.implementationType.getName(), "");
//        if (removeClassSuffix) {
//            result = result.substring(0, result.length() - classSuffix.length());
//        }
//        return result;
//    }

    @SuppressWarnings("unchecked")
    public  Class get(Class type) {
        Objects.requireNonNull(type);
        return mapping.computeIfAbsent(type, t -> {
            try {
                return getClass0(type);
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException("Can't find target class for " + type.getName());
            }
        });
    }

    @SuppressWarnings("WeakerAccess")
    protected Class getClass0(Class type) throws ClassNotFoundException {
        String packageSuffix = type.getPackage().getName().substring(keyPackage.length());
        String className = implementationType + packageSuffix + "." + type.getSimpleName() + classSuffix;
        log.debug(String.format("Loading  %s (%s)", type.getName(), className));
        return Class.forName(className);
    }


    protected int size() {
        return mapping.size();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy