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

com.speedment.runtime.join.internal.component.stream.SqlJoinStreamSupplierComponent Maven / Gradle / Ivy

/**
 *
 * Copyright (c) 2006-2018, Speedment, Inc. All Rights Reserved.
 *
 * 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 com.speedment.runtime.join.internal.component.stream;

import com.speedment.common.function.Function5;
import com.speedment.common.function.Function6;
import com.speedment.common.function.QuadFunction;
import com.speedment.common.function.TriFunction;
import com.speedment.common.injector.Injector;
import com.speedment.common.injector.annotation.Execute;
import com.speedment.runtime.config.Project;
import com.speedment.runtime.config.identifier.TableIdentifier;
import com.speedment.runtime.core.component.DbmsHandlerComponent;
import com.speedment.runtime.core.component.ProjectComponent;
import com.speedment.runtime.core.component.SqlAdapter;
import com.speedment.runtime.join.Join;
import com.speedment.runtime.join.JoinStreamSupplierComponent;
import com.speedment.runtime.join.internal.component.stream.sql.SqlHasCreateJoin2;
import com.speedment.runtime.join.internal.component.stream.sql.SqlHasCreateJoin3;
import com.speedment.runtime.join.internal.component.stream.sql.SqlHasCreateJoin4;
import com.speedment.runtime.join.internal.component.stream.sql.SqlHasCreateJoin5;
import com.speedment.runtime.join.internal.component.stream.sql.SqlHasCreateJoin6;
import com.speedment.runtime.join.stage.Stage;
import com.speedment.runtime.join.trait.HasCreateJoin2;
import com.speedment.runtime.join.trait.HasCreateJoin3;
import com.speedment.runtime.join.trait.HasCreateJoin4;
import com.speedment.runtime.join.trait.HasCreateJoin5;
import com.speedment.runtime.join.trait.HasCreateJoin6;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;

/**
 *
 * @author Per Minborg
 */
public class SqlJoinStreamSupplierComponent implements JoinStreamSupplierComponent {

    private Map, SqlAdapter> sqlAdapterMap;
    private HasCreateJoin2 join2Creator;
    private HasCreateJoin3 join3Creator;
    private HasCreateJoin4 join4Creator;
    private HasCreateJoin5 join5Creator;
    private HasCreateJoin6 join6Creator;

    @Execute
    void init(
        final Injector injector,
        final ProjectComponent projectComponent,
        final DbmsHandlerComponent dbmsHandlerComponent
    ) {
        final Project project = projectComponent.getProject();
        sqlAdapterMap = injector.stream(SqlAdapter.class)
            .map(sa -> (SqlAdapter) sa)
            .collect(
                toMap(
                    sa -> sa.identifier(),
                    sa -> sa
                )
            );

        join2Creator = new SqlHasCreateJoin2(dbmsHandlerComponent, project, this::sqlAdapterMapper);
        join3Creator = new SqlHasCreateJoin3(dbmsHandlerComponent, project, this::sqlAdapterMapper);
        join4Creator = new SqlHasCreateJoin4(dbmsHandlerComponent, project, this::sqlAdapterMapper);
        join5Creator = new SqlHasCreateJoin5(dbmsHandlerComponent, project, this::sqlAdapterMapper);
        join6Creator = new SqlHasCreateJoin6(dbmsHandlerComponent, project, this::sqlAdapterMapper);
    }

    @Override
    public  Join createJoin(
        final List> stages,
        final BiFunction constructor,
        final TableIdentifier t0,
        final TableIdentifier t1
    ) {
        return join2Creator.createJoin(stages, constructor, t0, t1);
    }

    @Override
    @SuppressWarnings("unchecked")
    public  Join createJoin(
        final List> stages,
        final TriFunction constructor,
        final TableIdentifier t0,
        final TableIdentifier t1,
        final TableIdentifier t2
    ) {
        return join3Creator.createJoin(stages, constructor, t0, t1, t2);
    }

    @Override
    @SuppressWarnings("unchecked")
    public  Join createJoin(
        final List> stages,
        final QuadFunction constructor,
        final TableIdentifier t0,
        final TableIdentifier t1,
        final TableIdentifier t2,
        final TableIdentifier t3
    ) {
        return join4Creator.createJoin(stages, constructor, t0, t1, t2, t3);
    }

    @Override
    @SuppressWarnings("unchecked")
    public  Join createJoin(
        final List> stages,
        final Function5 constructor,
        final TableIdentifier t0,
        final TableIdentifier t1,
        final TableIdentifier t2,
        final TableIdentifier t3,
        final TableIdentifier t4
    ) {
        return join5Creator.createJoin(stages, constructor, t0, t1, t2, t3, t4);
    }

    @Override
    @SuppressWarnings("unchecked")
    public  Join createJoin(
        final List> stages,
        final Function6 constructor,
        final TableIdentifier t0,
        final TableIdentifier t1,
        final TableIdentifier t2,
        final TableIdentifier t3,
        final TableIdentifier t4,
        final TableIdentifier t5
    ) {
        return join6Creator.createJoin(stages, constructor, t0, t1, t2, t3, t4, t5);
    }

    private  SqlAdapter sqlAdapterMapper(TableIdentifier identifier) {
        @SuppressWarnings("unchecked")
        final SqlAdapter result = (SqlAdapter) sqlAdapterMap.get(identifier);
        if (result == null) {
            throw new IllegalArgumentException(
                "There is no mapping for " + identifier + " "
                + "The following " + sqlAdapterMap.size() + " mappings are available: "
                + sqlAdapterMap.keySet().stream().map(Object::toString).collect(joining(", "))
            );
        }
        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy