Skip to content

Commit

Permalink
docs: update database configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Mar 2, 2024
1 parent c1646f8 commit ef05d03
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
Expand Up @@ -11,6 +11,7 @@
* Otavio Santana
*/
module org.eclipse.jnosql.communication.column {
uses org.eclipse.jnosql.communication.semistructured.DatabaseConfiguration;
requires org.eclipse.jnosql.communication.core;
requires org.eclipse.jnosql.communication.query;
requires jakarta.json.bind;
Expand Down
Expand Up @@ -25,41 +25,67 @@


/**
* It is a function that reads from {@link Settings} and then creates a manager factory instance.
* It should return a {@link NullPointerException} when the {@link Settings} parameter is null.
* This interface represents a function that reads from {@link Settings} and then creates a manager factory instance.
* It should throw a {@link NullPointerException} when the {@link Settings} parameter is null.
*
* <p>This interface extends {@link java.util.function.Function}, providing a way to create database manager factory instances
* based on settings. Implementations of this interface are expected to provide the necessary logic to read settings and
* instantiate appropriate database manager factories.</p>
*
* <p>Two static factory methods are provided for obtaining instances of {@link DatabaseConfiguration}: {@link #getConfiguration()}
* and {@link #getConfiguration(Class)}. These methods utilize Java's {@link ServiceLoader} mechanism to discover available
* implementations of {@link DatabaseConfiguration}.</p>
*
* <p>Implementations of this interface should be aware of the underlying provider implementation they represent,
* and ensure that the correct configuration is returned based on the provided type.</p>
*
* @param <R> the {@link DatabaseManagerFactory} type
* @see DatabaseManagerFactory
* @see DatabaseManager
*/
public interface DatabaseConfiguration extends Function<Settings, DatabaseManagerFactory> {
public interface DatabaseConfiguration<R extends DatabaseManagerFactory> extends Function<Settings, R> {


/**
* creates and returns a {@link DatabaseConfiguration} instance from {@link ServiceLoader}
* Creates and returns a {@link DatabaseConfiguration} instance using Java's {@link ServiceLoader} mechanism.
*
* <p>This method discovers and loads an implementation of {@link DatabaseConfiguration} using the {@link ServiceLoader}.
* It returns the first implementation found. If no implementation is found, it throws a {@link CommunicationException}.</p>
*
* @param <R> the {@link DatabaseManagerFactory} type
* @param <T> the configuration type
* @return {@link DatabaseConfiguration} instance
* @return a {@link DatabaseConfiguration} instance
* @throws CommunicationException if no implementation of {@link DatabaseConfiguration} is found
*/
static <T extends DatabaseConfiguration> T getConfiguration() {
@SuppressWarnings("unchecked")
static <R extends DatabaseManagerFactory, T extends DatabaseConfiguration<R>> T getConfiguration() {
return (T) ServiceLoader.load(DatabaseConfiguration.class)
.stream()
.map(ServiceLoader.Provider::get)
.findFirst().orElseThrow(() -> new CommunicationException("No ColumnConfiguration implementation found!"));
.findFirst().orElseThrow(() -> new CommunicationException("No DatabaseConfiguration implementation found!"));
}

/**
* creates and returns a {@link DatabaseConfiguration} instance from {@link ServiceLoader}
* for a particular provider implementation.
* Creates and returns a {@link DatabaseConfiguration} instance using Java's {@link ServiceLoader} mechanism,
* filtered by a particular provider implementation.
*
* <p>This method discovers and loads an implementation of {@link DatabaseConfiguration} using the {@link ServiceLoader},
* filtering by the provided type. It returns the first implementation found matching the specified type.
* If no implementation is found, it throws a {@link CommunicationException}.</p>
*
* @param <T> the configuration type
* @param type the particular provider
* @return {@link DatabaseConfiguration} instance
* @param <R> the {@link DatabaseManagerFactory} type
* @param <T> the configuration type
* @param type the particular provider type
* @return a {@link DatabaseConfiguration} instance
* @throws CommunicationException if no implementation of {@link DatabaseConfiguration} is found for the specified type
*/
static <T extends DatabaseConfiguration> T getConfiguration(Class<T> type) {
@SuppressWarnings("unchecked")
static <R extends DatabaseManagerFactory, T extends DatabaseConfiguration<R>> T getConfiguration(Class<T> type) {
return (T) ServiceLoader.load(DatabaseConfiguration.class)
.stream()
.map(ServiceLoader.Provider::get)
.filter(type::isInstance)
.findFirst().orElseThrow(() -> new CommunicationException("No ColumnConfiguration implementation found!"));
.findFirst()
.orElseThrow(() -> new CommunicationException("No DatabaseConfiguration implementation found for type: " + type.getName()));
}
}
}

0 comments on commit ef05d03

Please sign in to comment.