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

org.coodex.concrete.apitools.APIHelper Maven / Gradle / Ivy

There is a newer version: 0.5.3-RC1
Show newest version
/*
 * Copyright (c) 2019 coodex.org ([email protected])
 *
 * 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.coodex.concrete.apitools;

import org.coodex.concrete.common.ErrorMessageFacade;
import org.coodex.concrete.common.modules.AbstractModule;
import org.coodex.concrete.common.modules.ModuleMaker;
import org.coodex.util.Common;
import org.coodex.util.LazySelectableServiceLoader;
import org.coodex.util.SelectableServiceLoader;

import java.util.*;
import java.util.function.Consumer;

import static org.coodex.concrete.common.ConcreteHelper.foreachClassInPackages;
import static org.coodex.concrete.common.ConcreteHelper.isConcreteService;

public class APIHelper {

    private final static SelectableServiceLoader> MODULE_MAKERS =
            new LazySelectableServiceLoader>() {
            };

    private static ModuleMaker getInstance(String desc) {
//        if (MODULE_MAKERS.getAllInstances().size() == 0)
//            throw new RuntimeException("No service provider for " + ModuleMaker.class.getName());
//
//        for (ModuleMaker moduleMaker : MODULE_MAKERS.getAllInstances()) {
//            if (moduleMaker.isAccept(desc)) {
//                return moduleMaker;
//            }
//        }
        ModuleMaker moduleMaker = MODULE_MAKERS.select(desc);
        if (moduleMaker == null)
            throw new RuntimeException("No module maker supported '" + desc + "' ");
        else
            return moduleMaker;
    }


    public static > List loadModules(
            String desc, String... packages) {

        ModuleMaker maker = Common.cast(getInstance(desc));
        return loadModules(maker, packages);
    }

    public static > List loadModules(
            String desc, Class... classes) {
        ModuleMaker maker = Common.cast(getInstance(desc));
        return loadModules(maker, classes);
    }

    private static > List loadModules(
            final ModuleMaker maker, Class... classes) {

//        final Map, MODULE> moduleMap = new HashMap<>();
        ForEachClassConsumer consumer = new ForEachClassConsumer<>(maker);
        Arrays.stream(classes).forEach(consumer.classConsumer);
        return consumer.toList();
//
//
//        List moduleList = new ArrayList<>(consumer.moduleMap.values());
//        Collections.sort(moduleList);
//        return moduleList;
    }

    private static > List loadModules(
            final ModuleMaker maker, String... packages) {

        ForEachClassConsumer consumer = new ForEachClassConsumer<>(maker);
        foreachClassInPackages(consumer.classConsumer, packages);
        return consumer.toList();
//        final Map, MODULE> moduleMap = new HashMap<>();
//        foreachClassInPackages((serviceClass) -> {
//            if (isConcreteService(serviceClass)) {
//                MODULE module = maker.make(serviceClass);
//
//                Class key = module.getInterfaceClass();//.getName();
//                MODULE exists = moduleMap.get(key);
//
//                if (exists != null) {
//                    throw new RuntimeException(
//                            String.format("Module %s duplicated. %s & %s",
//                                    key,
//                                    exists.getInterfaceClass().getName(),
//                                    module.getInterfaceClass().getName()));
//                }
//                moduleMap.put(key, module);
//            } else {
//                ErrorMessageFacade.register(serviceClass);
//            }
//        }, packages);
//
//        List moduleList = new ArrayList<>(moduleMap.values());
//        Collections.sort(moduleList);
//
//        return moduleList;
    }

    private static class ForEachClassConsumer> {
        private final ModuleMaker maker;
        private final Map, MODULE> moduleMap = new HashMap<>();
        private final Consumer> classConsumer = (serviceClass) -> {
            if (isConcreteService(serviceClass)) {
                if (moduleMap.containsKey(serviceClass)) {
                    throw new DuplicatedModuleException(String.format("Module %s duplicated.", serviceClass));
                }
                moduleMap.put(serviceClass, getMaker().make(serviceClass));
            } else {
                ErrorMessageFacade.register(serviceClass);
            }
        };

        private ForEachClassConsumer(ModuleMaker maker) {
            this.maker = maker;
        }

        private ModuleMaker getMaker() {
            return this.maker;
        }

        private List toList() {
            List moduleList = new ArrayList<>(moduleMap.values());
            Collections.sort(moduleList);
            return moduleList;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy