Design: Convention, Status: Accepted
This page describes the structure and conventions used for client configuration objects. Client configuration objects are any objects used to configure an AWS client builder.
This section walks through an example configuration class structure and describes each of its components at a high level.
/** * Configuration Description // (1) */ @Immutable @ThreadSafe // (2) public final class SdkConfiguration // (3) implements ToCopyableBuilder<SdkConfiguration.Builder, SdkConfiguration> { // (4) private final String option; // (5) /** * @see #builder() // (6) */ private SdkClientConfiguration(DefaultSdkConfigurationBuilder builder) { this.option = builder.option; } public static Builder builder() { return new DefaultSdkConfigurationBuilder(); } /** * @see #Builder#option(String) // (7) */ public String option() { return this.option; } @Override public ClientHttpConfiguration.Builder toBuilder() { return builder().option(option); } @NotThreadSafe public interface Builder extends CopyableBuilder<Builder, SdkConfiguration> { // (8) /** * Configuration Option Description // (9) */ Builder option(String option); } private static final class DefaultSdkConfigurationBuilder implements Builder { // (10) private String option; @Override public Builder option(String option) { // (11) this.option = option; return this; } public void setOption(String option) { // (12) this.option = option; } @Override public SdkConfiguration build() { return new SdkConfiguration(this); } } }
@Immutable
, and therefore @ThreadSafe
.final
to prevent extension.ToCopyableBuilder
to ensure they can be converted back to a builder object.private final
to prevent reassignment.private
to enforce creation by the Builder
and refer curious eyes to the builder()
method for creating the object.private static final
implementation of the Builder
interface should be created that is not exposed outside of the scope of the configuration class.Inspector
-based frameworks, like spring XML.This section details the semantics of configuration fields.
final
.List
and Set
should be wrapped in a type that prevents their modification (eg. Collections.unmodifiableList
) when referenced through the “get” method.List
and Set
should be copied in the “set” method to prevent their modification by mutating the original object.boolean
or int
should not be used because they can not convey the absence of configuration.config.enableRedirect()
should instead be config.redirectEnabled()
). This is to avoid confusing their “get” methods for a mutating action.Special notes for collection types, like List
, Set
, and Map
:
add
prefix, eg: addApiName
put
prefix, eg: putHeader
public interface Builder { /** * Sets options. * * <p> * This overrides any option values already configured in the builder. */ Builder options(List<String> options); /** * Add a single option to the collection. */ Builder addOption(String option); /** * Sets headers to be set on the HTTP request. * * <p> * This overrides any header values already configured in the builder. */ Builder headers(Map<String, String> headers); /** * Add a single header to be set on the HTTP request. * * <p> * This overrides any values already configured with this header name in the builder. */ Builder putHeader(String key, String value); }