io.wcm.config.api.ParameterBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of io.wcm.config.api Show documentation
Show all versions of io.wcm.config.api Show documentation
API and SPI for context-specific configuration.
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2014 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.config.api;
import io.wcm.config.spi.ApplicationProvider;
import io.wcm.sling.commons.resource.ImmutableValueMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.osgi.annotation.versioning.ProviderType;
import com.google.common.collect.ImmutableSet;
/**
* Fluent API for building configuration parameter definitions.
* @param Parameter value type
*/
@ProviderType
public final class ParameterBuilder {
private static final Pattern PARAMETER_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9\\-\\_\\.]+$");
private static final Pattern OSGI_CONFIG_PROPERTY_PATTERN =
Pattern.compile("^[a-zA-Z0-9\\-\\_\\.\\$]+\\:[a-zA-Z0-9\\-\\_\\.]+$");
private static final Set> SUPPORTED_TYPES = ImmutableSet.>builder()
.add(String.class)
.add(String[].class)
.add(Integer.class)
.add(Long.class)
.add(Double.class)
.add(Boolean.class)
.add(Map.class)
.build();
private String name;
private Class type;
private String applicationId;
private String defaultOsgiConfigProperty;
private T defaultValue;
private final Map properties = new HashMap<>();
private ParameterBuilder() {
// private constructor
}
/**
* Create a new parameter builder.
* @param Parameter type
* @return Parameter builder
*/
public static ParameterBuilder create() {
return new ParameterBuilder();
}
/**
* Create a new parameter builder.
* @param Parameter type
* @param name Parameter name. Only characters, numbers, hyphen, underline and point are allowed.
* @return Parameter builder
*/
public static ParameterBuilder create(String name) {
return new ParameterBuilder()
.name(name);
}
/**
* Create a new parameter builder.
* @param Parameter type
* @param name Parameter name. Only characters, numbers, hyphen, underline and point are allowed.
* @param type Parameter value type
* @return Parameter builder
*/
public static ParameterBuilder create(String name, Class type) {
return new ParameterBuilder()
.name(name)
.type(type);
}
/**
* Create a new parameter builder.
* @param Parameter type
* @param name Parameter name. Only characters, numbers, hyphen, underline and point are allowed.
* @param type Parameter Value type.
* @param applicationId Application Id. Has to be a conent path starting with "/".
* @return Parameter builder
*/
public static ParameterBuilder create(String name, Class type, String applicationId) {
return new ParameterBuilder()
.name(name)
.type(type)
.applicationId(applicationId);
}
/**
* @param value Parameter name. Only characters, numbers, hyphen, underline and point are allowed.
* @return this
*/
public ParameterBuilder name(String value) {
if (value == null || !PARAMETER_NAME_PATTERN.matcher(value).matches()) {
throw new IllegalArgumentException("Invalid name: " + value);
}
this.name = value;
return this;
}
/**
* @param value Value type.
* @return this
*/
public ParameterBuilder type(Class value) {
if (value == null || !SUPPORTED_TYPES.contains(value)) {
throw new IllegalArgumentException("Invalid type: " + value);
}
this.type = value;
return this;
}
/**
* @param value Application Id. Has to be a conent path starting with "/".
* @return this
*/
public ParameterBuilder applicationId(String value) {
if (value == null || !ApplicationProvider.APPLICATION_ID_PATTERN.matcher(value).matches()) {
throw new IllegalArgumentException("Invalid applicaiton id: " + value);
}
this.applicationId = value;
return this;
}
/**
* References OSGi configuration property which is checked for default value if this parameter is not set
* in any configuration.
* @param value OSGi configuration parameter name with syntax {serviceClassName}:{propertyName}
* @return this
*/
public ParameterBuilder defaultOsgiConfigProperty(String value) {
if (value == null || !OSGI_CONFIG_PROPERTY_PATTERN.matcher(value).matches()) {
throw new IllegalArgumentException("Invalid value: " + value);
}
this.defaultOsgiConfigProperty = value;
return this;
}
/**
* @param value Default value if parameter is not set for configuration
* and no default value is defined in OSGi configuration
* @return this
*/
public ParameterBuilder defaultValue(T value) {
this.defaultValue = value;
return this;
}
/**
* Further properties for documentation and configuration of behavior in configuration editor.
* @param map Property map. Is merged with properties already set in builder.
* @return this
*/
public ParameterBuilder properties(Map map) {
if (map == null) {
throw new IllegalArgumentException("Map argument must not be null.");
}
this.properties.putAll(map);
return this;
}
/**
* Further property for documentation and configuration of behavior in configuration editor.
* @param key Property key
* @param value Property value
* @return this
*/
public ParameterBuilder property(String key, Object value) {
if (key == null) {
throw new IllegalArgumentException("Key argument must not be null.");
}
this.properties.put(key, value);
return this;
}
/**
* Builds the parameter definition.
* @return Parameter definition
*/
public Parameter build() {
if (this.name == null) {
throw new IllegalArgumentException("Name is missing.");
}
if (this.type == null) {
throw new IllegalArgumentException("Type is missing.");
}
if (this.applicationId == null) {
throw new IllegalArgumentException("Application id is missing.");
}
return new Parameter(
this.name,
this.type,
this.applicationId,
this.defaultOsgiConfigProperty,
this.defaultValue,
ImmutableValueMap.copyOf(this.properties));
}
}