io.streamthoughts.azkarra.api.config.Conf Maven / Gradle / Ivy
/*
* Copyright 2019-2020 StreamThoughts.
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 io.streamthoughts.azkarra.api.config;
import io.streamthoughts.azkarra.api.errors.MissingConfException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
/**
* A {@code Conf} represents an immutable object which can be used for configuring components.
*
* @see Configurable
* @see io.streamthoughts.azkarra.api.AzkarraContext
* @see io.streamthoughts.azkarra.api.StreamsExecutionEnvironment
* @see io.streamthoughts.azkarra.api.streams.TopologyProvider
*/
public interface Conf {
EmptyConf EMPTY = new EmptyConf();
/**
* Static helper that can be used to create a new empty
* {@link Conf} instance using the specified key-value pair.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final String k1, final Object v1) {
return new Property(k1, v1);
}
/**
* Static helper that can be used to create a new empty
* {@link Conf} instance using the specified key-value pair.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final String k1, final Object v1,
final String k2, final Object v2) {
return new MapConf(Map.of(k1, v1, k2, v2));
}
/**
* Static helper that can be used to create a new empty
* {@link Conf} instance using the specified key-value pair.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final String k1, final Object v1,
final String k2, final Object v2,
final String k3, final Object v3) {
return new MapConf(Map.of(k1, v1, k2, v2, k3, v3));
}
/**
* Static helper that can be used to create a new empty
* {@link Conf} instance using the specified key-value pair.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final String k1, final Object v1,
final String k2, final Object v2,
final String k3, final Object v3,
final String k4, final Object v4) {
return new MapConf(Map.of(k1, v1, k2, v2, k3, v3, k4, v4));
}
/**
* Static helper that can be used to create a new empty
* {@link Conf} instance using the specified key-value pair.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final String k1, final Object v1,
final String k2, final Object v2,
final String k3, final Object v3,
final String k4, final Object v4,
final String k5, final Object v5) {
return new MapConf(Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5));
}
/**
* Static helper that can be used to create a new empty
* {@link Conf} instance using the specified key-value pair.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final String k1, final Object v1,
final String k2, final Object v2,
final String k3, final Object v3,
final String k4, final Object v4,
final String k5, final Object v5,
final String k6, final Object v6) {
return new MapConf(Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6));
}
/**
* Static helper that can be used to create a new empty
* {@link Conf} instance using the specified {@link Map}.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final Map map) {
return new MapConf(map);
}
/**
* Static helper that can be used to merge multiple {@link Conf} instances.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final Conf...configurations) {
return of(Arrays.asList(configurations));
}
/**
* Static helper that can be used to merge multiple {@link Conf} instances.
*
* @return a new {@link Conf} instance.
*/
static Conf of(final Collection extends Conf> configurations) {
Conf config = empty();
for (Conf conf : configurations) {
if (conf != null) {
Conf copy = Conf.of(conf.getConfAsMap());
config = config.withFallback(copy);
}
}
// This will have the effect to flatten the configuration.
return Conf.of(config.getConfAsMap());
}
/**
* Static helper that can be used to creates a new empty {@link Conf} instance.
*
* @return a new {@link Conf} instance.
*/
static Conf empty() {
return EMPTY;
}
/**
* Gets a required parameter value.
*
* @param path the parameter path.
*
* @return the object value.
*/
Object getValue(final String path);
/**
* Returns a {@link Set} view of the keys contained in this conf.
* @return a set view of the keys contained in this conf
*/
Set keySet();
/**
* Gets a required parameter as a string.
*
* @param path the parameter path.
* @return the parameter value as a string.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*/
String getString(final String path);
/**
* Gets an optional parameter as a string.
*
* @param path the parameter path.
* @return the {@link Optional} value.
*/
default Optional getOptionalString(final String path) {
return Optional.ofNullable(hasPath(path) ? getString(path) : null);
}
/**
* Gets a required parameter as a long.
*
* @param path the parameter path.
* @return the parameter value as a long.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*/
long getLong(final String path);
/**
* Gets an optional parameter as a long.
*
* @param path the parameter path.
* @return the {@link Optional} value.
*/
default Optional getOptionalLong(final String path) {
return Optional.ofNullable(hasPath(path) ? getLong(path) : null);
}
/**
* Gets a required parameter as an integer.
*
* @param path the parameter path.
* @return the parameter value as a int.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*/
int getInt(final String path);
/**
* Gets an optional parameter as an integer.
*
* @param path the parameter path.
* @return the {@link Optional} value.
*/
default Optional getOptionalInt(final String path) {
return Optional.ofNullable(hasPath(path) ? getInt(path) : null);
}
/**
* Gets a required parameter as an boolean.
*
* @param path the parameter path.
* @return the parameter value as a boolean.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*/
boolean getBoolean(final String path);
/**
* Gets an optional parameter as an boolean.
*
* @param path the parameter path.
* @return the {@link Optional} value.
*/
default Optional getOptionalBoolean(final String path) {
return Optional.ofNullable(hasPath(path) ? getBoolean(path) : null);
}
/**
* Gets a required parameter as a double.
*
* @param path the parameter path.
* @return the parameter value as a double.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*/
double getDouble(final String path);
/**
* Gets an optional parameter as an double.
*
* @param path the parameter path.
* @return the {@link Optional} value.
*/
default Optional getOptionalDouble(final String path) {
return Optional.ofNullable(hasPath(path) ? getDouble(path) : null);
}
/**
* Gets a required parameter as a list.
*
* @param path the parameter path.
*
* @throws MissingConfException if no parameter can be found for the specified path.
* @return a string list value.
*/
List getStringList(final String path);
/**
* Gets a required parameter as a {@link Conf}.
*
* @param path the parameter path.
*
* @throws MissingConfException if no parameter can be found for the specified path.
* @return a new {@link Conf} instance.
*/
Conf getSubConf(final String path);
/**
* Gets a required parameter as a list of {@link Conf}.
*
* @param path the parameter path.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*
* @return a new list of {@link Conf} instances.
*/
List getSubConfList(final String path);
/**
* Checks whether the specified path exists into this {@link Conf}.
*
* @param path the path to be checked.
* @return {@code true} if the path exists, {@code false} otherwise.
*/
boolean hasPath(final String path);
Conf withFallback(final Conf fallback);
/**
* Gets a required parameter as a list of instances of type {@link T}.
*
* @param path the parameter path.
* @param type the class of the .
* @param the expected type.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*
* @return a new {@link Collection} of {@link T}.
*/
Collection getClasses(final String path, final Class type);
/**
* Gets a required parameter as an instances of type {@link T}.
*
* @param path the parameter path.
* @param type the class of the .
* @param the expected type.
*
* @throws MissingConfException if no parameter can be found for the specified path.
*
* @return a new {@link Collection} of {@link T}.
*/
T getClass(final String path, final Class type);
/**
* Converts this {@link Conf} into a path-value map.
*
* @return a new {@link Map} instance containing the configuration values.
*/
Map getConfAsMap() ;
/**
* Converts this {@link Conf} into a properties.
*
* @return a new {@link Properties} instance containing the configuration values.
*/
Properties getConfAsProperties();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy