org.eclipse.kapua.kura.simulator.generator.GeneratorFactories Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2017 Red Hat Inc and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.kura.simulator.generator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
public final class GeneratorFactories {
private GeneratorFactories() {
}
/**
* Construct a generator from a configuration map
*
* The method will try to create a {@link Generator} instance from the configuration map
* by iterating through all {@link GeneratorFactory} instances registered with the {@link ServiceLoader}
* framework.
*
*
* If no generator could be created an empty Optional is being returned
*
*
* @param configuration
* the configuration map
* @return the optional result, never {@code null}
*/
public static Optional create(final Map configuration) {
return create(configuration, ServiceLoader.load(GeneratorFactory.class));
}
/**
* Construct a generator from a configuration map
*
* The method will try to create a {@link Generator} instance from the configuration map
* by iterating through all {@link GeneratorFactory} instances provided as argument
*
*
* If no generator could be created an empty Optional is being returned
*
*
* @param configuration
* the configuration map
* @param factories
* the available factories
* @return the optional result, never {@code null}
*/
public static Optional create(final Map configuration, final Iterable factories) {
Objects.requireNonNull(configuration);
Objects.requireNonNull(factories);
for (final GeneratorFactory factory : factories) {
final Optional generator = factory.create(configuration);
if (generator.isPresent()) {
return generator;
}
}
return Optional.empty();
}
}