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

org.eclipse.jnosql.databases.cassandra.communication.CassandraConfiguration Maven / Gradle / Ivy

/*
 *  Copyright (c) 2022 Contributors to the Eclipse Foundation
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */

package org.eclipse.jnosql.databases.cassandra.communication;


import org.eclipse.jnosql.communication.Settings;
import org.eclipse.jnosql.communication.semistructured.DatabaseConfiguration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * A Cassandra-specific implementation of {@link DatabaseConfiguration}, which provides
 * {@link CassandraColumnManagerFactory} instances.
 *
 * @see CassandraConfigurations
 */
public final class CassandraConfiguration implements DatabaseConfiguration {

    /**
     * Retrieves the {@link CassandraColumnManagerFactory} based on the provided configurations.
     *
     * @param configurations the configurations for Cassandra
     * @return a {@link CassandraColumnManagerFactory} instance
     * @throws NullPointerException if configurations are null
     */
    private CassandraColumnManagerFactory getManagerFactory(Map configurations) {
        Objects.requireNonNull(configurations);
        CassandraProperties properties = CassandraProperties.of(configurations);
        return new CassandraColumnManagerFactory(properties.createCluster(), properties.getQueries());
    }

    /**
     * Applies the settings to create a {@link CassandraColumnManagerFactory}.
     *
     * @param settings the settings to apply
     * @return a {@link CassandraColumnManagerFactory} instance
     * @throws NullPointerException if settings are null
     */
    @Override
    public CassandraColumnManagerFactory apply(Settings settings) throws NullPointerException {
        Objects.requireNonNull(settings, "Settings is required");
        Map configurations = new HashMap<>();

        List keys = settings.keySet()
                .stream()
                .filter(k -> k.startsWith("jnosql."))
                .toList();

        for (String key : keys) {
            settings.get(key, String.class).ifPresent(v -> configurations.put(key, v));
        }
        return getManagerFactory(configurations);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy