Skip to content

Commit

Permalink
Fix fluent API for config classes
Browse files Browse the repository at this point in the history
Setters and other modification methods should try to return the same
instance for a fluent API.

Fixes: hazelcast#11669
  • Loading branch information
Matko Medenjak committed Aug 10, 2019
1 parent dece01f commit 9966257
Show file tree
Hide file tree
Showing 34 changed files with 118 additions and 65 deletions.
Expand Up @@ -178,10 +178,12 @@ public ClientConfig(ClientConfig config) {
*
* @param configPatternMatcher the pattern matcher
* @throws IllegalArgumentException if the pattern matcher is {@code null}
* @return this configuration
*/
public void setConfigPatternMatcher(ConfigPatternMatcher configPatternMatcher) {
public ClientConfig setConfigPatternMatcher(ConfigPatternMatcher configPatternMatcher) {
Preconditions.isNotNull(configPatternMatcher, "configPatternMatcher");
this.configPatternMatcher = configPatternMatcher;
return this;
}

/**
Expand Down Expand Up @@ -744,18 +746,20 @@ public Map<String, Map<String, QueryCacheConfig>> getQueryCacheConfigs() {
return queryCacheConfigs;
}

public void setQueryCacheConfigs(Map<String, Map<String, QueryCacheConfig>> queryCacheConfigs) {
public ClientConfig setQueryCacheConfigs(Map<String, Map<String, QueryCacheConfig>> queryCacheConfigs) {
Preconditions.isNotNull(queryCacheConfigs, "queryCacheConfigs");
this.queryCacheConfigs.clear();
this.queryCacheConfigs.putAll(queryCacheConfigs);
return this;
}

public String getInstanceName() {
return instanceName;
}

public void setInstanceName(String instanceName) {
public ClientConfig setInstanceName(String instanceName) {
this.instanceName = instanceName;
return this;
}

public ClientConnectionStrategyConfig getConnectionStrategyConfig() {
Expand Down
Expand Up @@ -62,8 +62,9 @@ public String getName() {
* Sets the name or name pattern for this config. Must not be modified after this
* instance is added to {@link ClientConfig}.
*/
public void setName(String name) {
public ClientFlakeIdGeneratorConfig setName(String name) {
this.name = name;
return this;
}

/**
Expand Down
Expand Up @@ -103,9 +103,11 @@ public DiscoveryConfig getDiscoveryConfig() {
*
* @param discoveryConfig the Discovery Provider SPI configuration
* @throws java.lang.IllegalArgumentException if discoveryConfig is null
* @return this configuration
*/
public void setDiscoveryConfig(DiscoveryConfig discoveryConfig) {
public ClientNetworkConfig setDiscoveryConfig(DiscoveryConfig discoveryConfig) {
this.discoveryConfig = isNotNull(discoveryConfig, "discoveryConfig");
return this;
}

/**
Expand Down Expand Up @@ -446,9 +448,10 @@ public ClientCloudConfig getCloudConfig() {
return cloudConfig;
}

public void setCloudConfig(ClientCloudConfig cloudConfig) {
public ClientNetworkConfig setCloudConfig(ClientCloudConfig cloudConfig) {
isNotNull(cloudConfig, "cloudConfig");
this.cloudConfig = cloudConfig;
return this;
}

/**
Expand Down
Expand Up @@ -424,7 +424,7 @@ public ConfigPatternMatcher getConfigPatternMatcher() {
}

@Override
public void setConfigPatternMatcher(ConfigPatternMatcher configPatternMatcher) {
public Config setConfigPatternMatcher(ConfigPatternMatcher configPatternMatcher) {
throw new UnsupportedOperationException(UNSUPPORTED_ERROR_MESSAGE);
}

Expand All @@ -444,7 +444,7 @@ public MemberAttributeConfig getMemberAttributeConfig() {
}

@Override
public void setMemberAttributeConfig(MemberAttributeConfig memberAttributeConfig) {
public Config setMemberAttributeConfig(MemberAttributeConfig memberAttributeConfig) {
throw new UnsupportedOperationException(UNSUPPORTED_ERROR_MESSAGE);
}

Expand Down
Expand Up @@ -308,7 +308,7 @@ public int getPortCount() {
}

@Override
public void setPortCount(int portCount) {
public NetworkConfig setPortCount(int portCount) {
throw new UnsupportedOperationException();
}

Expand Down
Expand Up @@ -496,9 +496,11 @@ public boolean isDisablePerEntryInvalidationEvents() {
*
* @param disablePerEntryInvalidationEvents disables invalidation event sending behaviour if it is {@code true},
* otherwise enables it
* @return this configuration
*/
public void setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
public CacheConfig<K, V> setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
this.disablePerEntryInvalidationEvents = disablePerEntryInvalidationEvents;
return this;
}

@Override
Expand Down
Expand Up @@ -210,7 +210,7 @@ public CacheConfiguration<K, V> setReadThrough(boolean isReadThrough) {
}

@Override
public void setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
public CacheConfig<K, V> setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
throw throwReadOnly();
}

Expand Down
Expand Up @@ -563,9 +563,11 @@ public WanReplicationRef getWanReplicationRef() {
* Sets the WAN target replication reference.
*
* @param wanReplicationRef the WAN target replication reference
* @return this configuration
*/
public void setWanReplicationRef(WanReplicationRef wanReplicationRef) {
public CacheSimpleConfig setWanReplicationRef(WanReplicationRef wanReplicationRef) {
this.wanReplicationRef = wanReplicationRef;
return this;
}

/**
Expand Down Expand Up @@ -699,9 +701,11 @@ public boolean isDisablePerEntryInvalidationEvents() {
*
* @param disablePerEntryInvalidationEvents Disables invalidation event sending behaviour if it is {@code true},
* otherwise enables it
* @return this configuration
*/
public void setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
public CacheSimpleConfig setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
this.disablePerEntryInvalidationEvents = disablePerEntryInvalidationEvents;
return this;
}

@Override
Expand Down
Expand Up @@ -137,7 +137,7 @@ public CacheSimpleConfig addEntryListenerConfig(CacheSimpleEntryListenerConfig l
}

@Override
public void setWanReplicationRef(WanReplicationRef wanReplicationRef) {
public CacheSimpleConfig setWanReplicationRef(WanReplicationRef wanReplicationRef) {
throw new UnsupportedOperationException("This config is read-only cache: " + getName());
}

Expand All @@ -163,7 +163,7 @@ public CacheSimpleConfig setPartitionLostListenerConfigs(
}

@Override
public void setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
public CacheSimpleConfig setDisablePerEntryInvalidationEvents(boolean disablePerEntryInvalidationEvents) {
throw new UnsupportedOperationException("This config is read-only cache: " + getName());
}

Expand Down
Expand Up @@ -61,16 +61,18 @@ public String getCacheEntryListenerFactory() {
return cacheEntryListenerFactory;
}

public void setCacheEntryListenerFactory(String cacheEntryListenerFactory) {
public CacheSimpleEntryListenerConfig setCacheEntryListenerFactory(String cacheEntryListenerFactory) {
this.cacheEntryListenerFactory = cacheEntryListenerFactory;
return this;
}

public String getCacheEntryEventFilterFactory() {
return cacheEntryEventFilterFactory;
}

public void setCacheEntryEventFilterFactory(String cacheEntryEventFilterFactory) {
public CacheSimpleEntryListenerConfig setCacheEntryEventFilterFactory(String cacheEntryEventFilterFactory) {
this.cacheEntryEventFilterFactory = cacheEntryEventFilterFactory;
return this;
}

/**
Expand All @@ -89,9 +91,11 @@ public boolean isOldValueRequired() {
* creates additional traffic. Default value is {@code false}.
*
* @param oldValueRequired {@code true} to have old value required, {@code false} otherwise
* @return this configuration
*/
public void setOldValueRequired(boolean oldValueRequired) {
public CacheSimpleEntryListenerConfig setOldValueRequired(boolean oldValueRequired) {
this.oldValueRequired = oldValueRequired;
return this;
}

/**
Expand All @@ -109,9 +113,11 @@ public boolean isSynchronous() {
*
* @param synchronous {@code true} to have this cache entry listener implementation called in a synchronous manner,
* {@code false} otherwise.
* @return this configuration
*/
public void setSynchronous(boolean synchronous) {
public CacheSimpleEntryListenerConfig setSynchronous(boolean synchronous) {
this.synchronous = synchronous;
return this;
}

@Override
Expand Down
Expand Up @@ -28,22 +28,22 @@ public CacheSimpleEntryListenerConfigReadOnly(CacheSimpleEntryListenerConfig lis
}

@Override
public void setSynchronous(boolean synchronous) {
super.setSynchronous(synchronous);
public CacheSimpleEntryListenerConfig setSynchronous(boolean synchronous) {
throw new UnsupportedOperationException("This config is read-only");
}

@Override
public void setOldValueRequired(boolean oldValueRequired) {
public CacheSimpleEntryListenerConfig setOldValueRequired(boolean oldValueRequired) {
throw new UnsupportedOperationException("This config is read-only");
}

@Override
public void setCacheEntryEventFilterFactory(String cacheEntryEventFilterFactory) {
public CacheSimpleEntryListenerConfig setCacheEntryEventFilterFactory(String cacheEntryEventFilterFactory) {
throw new UnsupportedOperationException("This config is read-only");
}

@Override
public void setCacheEntryListenerFactory(String cacheEntryListenerFactory) {
public CacheSimpleEntryListenerConfig setCacheEntryListenerFactory(String cacheEntryListenerFactory) {
throw new UnsupportedOperationException("This config is read-only");
}
}
Expand Up @@ -222,9 +222,11 @@ public T setStatisticsEnabled(boolean statisticsEnabled) {
* Adds an item listener to this collection (listens for when items are added or removed).
*
* @param itemListenerConfig the item listener to add to this collection
* @return this configuration
*/
public void addItemListenerConfig(ItemListenerConfig itemListenerConfig) {
public T addItemListenerConfig(ItemListenerConfig itemListenerConfig) {
getItemListenerConfigs().add(itemListenerConfig);
return (T) this;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions hazelcast/src/main/java/com/hazelcast/config/Config.java
Expand Up @@ -220,12 +220,14 @@ public ConfigPatternMatcher getConfigPatternMatcher() {
*
* @param configPatternMatcher the pattern matcher
* @throws IllegalArgumentException if the pattern matcher is {@code null}
* @return this configuration
*/
public void setConfigPatternMatcher(ConfigPatternMatcher configPatternMatcher) {
public Config setConfigPatternMatcher(ConfigPatternMatcher configPatternMatcher) {
if (configPatternMatcher == null) {
throw new IllegalArgumentException("ConfigPatternMatcher is not allowed to be null!");
}
this.configPatternMatcher = configPatternMatcher;
return this;
}

/**
Expand Down Expand Up @@ -274,9 +276,11 @@ public MemberAttributeConfig getMemberAttributeConfig() {
* attributes are exchanged with other members, e.g. on membership events.
*
* @param memberAttributeConfig the member attribute configuration
* @return this configuration
*/
public void setMemberAttributeConfig(MemberAttributeConfig memberAttributeConfig) {
public Config setMemberAttributeConfig(MemberAttributeConfig memberAttributeConfig) {
this.memberAttributeConfig = memberAttributeConfig;
return this;
}

/**
Expand Down
17 changes: 12 additions & 5 deletions hazelcast/src/main/java/com/hazelcast/config/DiscoveryConfig.java
Expand Up @@ -73,8 +73,9 @@ public DiscoveryConfig getAsReadOnly() {
return readonly;
}

public void setDiscoveryServiceProvider(DiscoveryServiceProvider discoveryServiceProvider) {
public DiscoveryConfig setDiscoveryServiceProvider(DiscoveryServiceProvider discoveryServiceProvider) {
this.discoveryServiceProvider = discoveryServiceProvider;
return this;
}

public DiscoveryServiceProvider getDiscoveryServiceProvider() {
Expand All @@ -85,16 +86,18 @@ public NodeFilter getNodeFilter() {
return nodeFilter;
}

public void setNodeFilter(NodeFilter nodeFilter) {
public DiscoveryConfig setNodeFilter(NodeFilter nodeFilter) {
this.nodeFilter = nodeFilter;
return this;
}

public String getNodeFilterClass() {
return nodeFilterClass;
}

public void setNodeFilterClass(String nodeFilterClass) {
public DiscoveryConfig setNodeFilterClass(String nodeFilterClass) {
this.nodeFilterClass = nodeFilterClass;
return this;
}

public boolean isEnabled() {
Expand All @@ -119,11 +122,13 @@ public Collection<DiscoveryStrategyConfig> getDiscoveryStrategyConfigs() {
* Sets the strategy configurations for this discovery config.
*
* @param discoveryStrategyConfigs the strategy configurations
* @return this configuration
*/
public void setDiscoveryStrategyConfigs(List<DiscoveryStrategyConfig> discoveryStrategyConfigs) {
public DiscoveryConfig setDiscoveryStrategyConfigs(List<DiscoveryStrategyConfig> discoveryStrategyConfigs) {
this.discoveryStrategyConfigs = discoveryStrategyConfigs == null
? new ArrayList<DiscoveryStrategyConfig>(1)
: discoveryStrategyConfigs;
return this;
}

/**
Expand All @@ -133,9 +138,11 @@ public void setDiscoveryStrategyConfigs(List<DiscoveryStrategyConfig> discoveryS
* remember when building custom {@link com.hazelcast.config.Config} instances.
*
* @param discoveryStrategyConfig the {@link DiscoveryStrategyConfig} to add
* @return this configuration
*/
public void addDiscoveryStrategyConfig(DiscoveryStrategyConfig discoveryStrategyConfig) {
public DiscoveryConfig addDiscoveryStrategyConfig(DiscoveryStrategyConfig discoveryStrategyConfig) {
discoveryStrategyConfigs.add(discoveryStrategyConfig);
return this;
}

@Override
Expand Down
Expand Up @@ -34,27 +34,27 @@ public class DiscoveryConfigReadOnly extends DiscoveryConfig {
}

@Override
public void setDiscoveryServiceProvider(DiscoveryServiceProvider discoveryServiceProvider) {
public DiscoveryConfig setDiscoveryServiceProvider(DiscoveryServiceProvider discoveryServiceProvider) {
throw new UnsupportedOperationException("Configuration is readonly");
}

@Override
public void setNodeFilter(NodeFilter nodeFilter) {
public DiscoveryConfig setNodeFilter(NodeFilter nodeFilter) {
throw new UnsupportedOperationException("Configuration is readonly");
}

@Override
public void setNodeFilterClass(String nodeFilterClass) {
public DiscoveryConfig setNodeFilterClass(String nodeFilterClass) {
throw new UnsupportedOperationException("Configuration is readonly");
}

@Override
public void addDiscoveryStrategyConfig(DiscoveryStrategyConfig discoveryStrategyConfig) {
public DiscoveryConfig addDiscoveryStrategyConfig(DiscoveryStrategyConfig discoveryStrategyConfig) {
throw new UnsupportedOperationException("Configuration is readonly");
}

@Override
public void setDiscoveryStrategyConfigs(List<DiscoveryStrategyConfig> discoveryStrategyConfigs) {
public DiscoveryConfig setDiscoveryStrategyConfigs(List<DiscoveryStrategyConfig> discoveryStrategyConfigs) {
throw new UnsupportedOperationException("Configuration is readonly");
}
}
Expand Up @@ -94,12 +94,14 @@ public DiscoveryStrategyFactory getDiscoveryStrategyFactory() {
return discoveryStrategyFactory;
}

public void addProperty(String key, Comparable value) {
public DiscoveryStrategyConfig addProperty(String key, Comparable value) {
properties.put(key, value);
return this;
}

public void removeProperty(String key) {
public DiscoveryStrategyConfig removeProperty(String key) {
properties.remove(key);
return this;
}

public DiscoveryStrategyConfig setProperties(Map<String, Comparable> properties) {
Expand Down

0 comments on commit 9966257

Please sign in to comment.