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

org.seedstack.business.internal.migrate.MigrateModule Maven / Gradle / Ivy

There is a newer version: 4.5.0
Show newest version
/*
 * Copyright © 2013-2018, The SeedStack authors 
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package org.seedstack.business.internal.migrate;

import com.google.inject.AbstractModule;
import com.google.inject.Key;
import java.util.Map;
import javax.inject.Provider;
import org.seedstack.business.assembler.Assembler;
import org.seedstack.business.assembler.FluentAssembler;
import org.seedstack.business.domain.AggregateRoot;
import org.seedstack.business.domain.Factory;
import org.seedstack.business.domain.Producible;
import org.seedstack.business.domain.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class MigrateModule extends AbstractModule {
    private static final Logger LOGGER = LoggerFactory.getLogger(MigrateModule.class);
    private final Map, Key> repositoryBindings;
    private final Map, Key> factoryBindings;
    private final Map, Key> assemblerBindings;

    MigrateModule(Map, Key> repositoryBindings, Map, Key> factoryBindings,
            Map, Key> assemblerBindings) {
        this.repositoryBindings = repositoryBindings;
        this.factoryBindings = factoryBindings;
        this.assemblerBindings = assemblerBindings;
    }

    @Override
    protected void configure() {
        bind(FluentAssembler.class).to(FluentAssemblerAdapter.class);

        for (Map.Entry, Key> binding : repositoryBindings.entrySet()) {
            LOGGER.trace("Binding {} to {}", binding.getKey(), binding.getValue());
            bind(binding.getKey()).toProvider(createRepositoryProvider(binding.getValue()));
        }
        for (Map.Entry, Key> binding : factoryBindings.entrySet()) {
            LOGGER.trace("Binding {} to {}", binding.getKey(), binding.getValue());
            bind(binding.getKey()).toProvider(createFactoryProvider(binding.getValue()));
        }
        for (Map.Entry, Key> binding : assemblerBindings.entrySet()) {
            LOGGER.trace("Binding {} to {}", binding.getKey(), binding.getValue());
            bind(binding.getKey()).toProvider(createAssemblerProvider(binding.getValue()));
        }
    }

    @SuppressWarnings("unchecked")
    private , I, T extends Repository> Provider createRepositoryProvider(
            Key value) {
        return new LegacyRepositoryProvider<>((Key) value);
    }

    @SuppressWarnings("unchecked")
    private 

> Provider createFactoryProvider(Key value) { return new GenericFactoryProvider<>((Key) value); } @SuppressWarnings("unchecked") private > Provider createAssemblerProvider(Key value) { return new LegacyAssemblerProvider<>((Key) value); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy