Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
Refactor Optional parameters for JsonCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Parm committed Jul 25, 2016
1 parent 98ffbe3 commit 8b53042
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 106 deletions.
22 changes: 14 additions & 8 deletions api/src/main/java/com/spotify/ffwd/protocol/RetryPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class Constant implements RetryPolicy {
private final long value;

@JsonCreator
public Constant(@JsonProperty("value") Long value) {
this.value = Optional.ofNullable(value).orElse(DEFAULT_VALUE);
public Constant(@JsonProperty("value") Optional<Long> value) {
this.value = value.orElse(DEFAULT_VALUE);
}

@Override
Expand All @@ -74,9 +74,12 @@ class Exponential implements RetryPolicy {
private final int maxAttempt;

@JsonCreator
public Exponential(@JsonProperty("initial") Long initial, @JsonProperty("max") Long max) {
this.initial = Optional.ofNullable(initial).orElse(DEFAULT_INITIAL);
this.max = Optional.ofNullable(max).orElse(DEFAULT_MAX);
public Exponential(
@JsonProperty("initial") Optional<Long> initial,
@JsonProperty("max") Optional<Long> max
) {
this.initial = initial.orElse(DEFAULT_INITIAL);
this.max = max.orElse(DEFAULT_MAX);
this.maxAttempt =
new Double(Math.floor(Math.log(this.max / this.initial) / Math.log(2))).intValue();
}
Expand Down Expand Up @@ -107,9 +110,12 @@ class Linear implements RetryPolicy {
private final int maxAttempt;

@JsonCreator
public Linear(@JsonProperty("value") Long value, @JsonProperty("max") Long max) {
this.value = Optional.ofNullable(value).orElse(DEFAULT_VALUE);
this.max = Optional.ofNullable(max).orElse(DEFAULT_MAX);
public Linear(
@JsonProperty("value") Optional<Long> value,
@JsonProperty("max") Optional<Long> max
) {
this.value = value.orElse(DEFAULT_VALUE);
this.max = max.orElse(DEFAULT_MAX);
this.maxAttempt = (int) ((this.max / this.value) - 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Optional;

/**
* Created by parmus on 29-01-16.
Expand All @@ -33,7 +34,10 @@ public void testlinearBackoff(){
final long initial = 2L;
final long max = initial * 5;

RetryPolicy.Linear policy = new RetryPolicy.Linear(initial, max);
RetryPolicy.Linear policy = new RetryPolicy.Linear(
Optional.of(initial),
Optional.of(max)
);

// The delay should ramp up regularly
for(int i=0; i<5; i++){
Expand All @@ -52,7 +56,10 @@ public void testExponentialBackoff(){
final long initial = 2L;
final long max = ((long) Math.pow(2, 5) * initial) - 1;

RetryPolicy.Exponential policy = new RetryPolicy.Exponential(initial, max);
RetryPolicy.Exponential policy = new RetryPolicy.Exponential(
Optional.of(initial),
Optional.of(max)
);

// The delay should ramp up regularly
for(int i=0; i<5; i++){
Expand Down
58 changes: 32 additions & 26 deletions core/src/main/java/com/spotify/ffwd/AgentConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,31 @@ public class AgentConfig {

@JsonCreator
public AgentConfig(
@JsonProperty("debug") Debug debug, @JsonProperty("host") String host,
@JsonProperty("tags") Map<String, String> tags,
@JsonProperty("riemannTags") Set<String> riemannTags,
@JsonProperty("input") InputManagerModule input,
@JsonProperty("output") OutputManagerModule output,
@JsonProperty("asyncThreads") Integer asyncThreads,
@JsonProperty("schedulerThreads") Integer schedulerThreads,
@JsonProperty("bossThreads") Integer bossThreads,
@JsonProperty("workerThreads") Integer workerThreads, @JsonProperty("ttl") Long ttl,
@JsonProperty("qlog") String qlog
@JsonProperty("debug") Optional<Debug> debug,
@JsonProperty("host") Optional<String> host,
@JsonProperty("tags") Optional<Map<String, String>> tags,
@JsonProperty("riemannTags") Optional<Set<String>> riemannTags,
@JsonProperty("input") Optional<InputManagerModule> input,
@JsonProperty("output") Optional<OutputManagerModule> output,
@JsonProperty("asyncThreads") Optional<Integer> asyncThreads,
@JsonProperty("schedulerThreads") Optional<Integer> schedulerThreads,
@JsonProperty("bossThreads") Optional<Integer> bossThreads,
@JsonProperty("workerThreads") Optional<Integer> workerThreads,
@JsonProperty("ttl") Optional<Long> ttl,
@JsonProperty("qlog") Optional<String> qlog
) {
this.debug = Optional.ofNullable(debug);
this.host = Optional.ofNullable(host).orElseGet(this::buildDefaultHost);
this.tags = Optional.ofNullable(tags).orElse(DEFAULT_TAGS);
this.riemannTags = Optional.ofNullable(riemannTags).orElse(DEFAULT_RIEMANNTAGS);
this.input = Optional.ofNullable(input).orElseGet(InputManagerModule.supplyDefault());
this.output = Optional.ofNullable(output).orElseGet(OutputManagerModule.supplyDefault());
this.asyncThreads = Optional.ofNullable(asyncThreads).orElse(DEFAULT_ASYNC_THREADS);
this.schedulerThreads =
Optional.ofNullable(schedulerThreads).orElse(DEFAULT_SCHEDULER_THREADS);
this.bossThreads = Optional.ofNullable(bossThreads).orElse(DEFAULT_BOSS_THREADS);
this.workerThreads = Optional.ofNullable(workerThreads).orElse(DEFAULT_WORKER_THREADS);
this.ttl = Optional.ofNullable(ttl).orElse(0L);
this.qlog = Paths.get(Optional.ofNullable(qlog).orElse(DEFAULT_QLOG));
this.debug = debug;
this.host = host.orElseGet(this::buildDefaultHost);
this.tags = tags.orElse(DEFAULT_TAGS);
this.riemannTags = riemannTags.orElse(DEFAULT_RIEMANNTAGS);
this.input = input.orElseGet(InputManagerModule.supplyDefault());
this.output = output.orElseGet(OutputManagerModule.supplyDefault());
this.asyncThreads = asyncThreads.orElse(DEFAULT_ASYNC_THREADS);
this.schedulerThreads = schedulerThreads.orElse(DEFAULT_SCHEDULER_THREADS);
this.bossThreads = bossThreads.orElse(DEFAULT_BOSS_THREADS);
this.workerThreads = workerThreads.orElse(DEFAULT_WORKER_THREADS);
this.ttl = ttl.orElse(0L);
this.qlog = Paths.get(qlog.orElse(DEFAULT_QLOG));
}

private String buildDefaultHost() {
Expand All @@ -101,9 +102,14 @@ public static final class Debug {
private final InetSocketAddress localAddress;

@JsonCreator
public Debug(@JsonProperty("host") String host, @JsonProperty("port") Integer port) {
this.localAddress = buildLocalAddress(Optional.ofNullable(host).orElse(DEFAULT_HOST),
Optional.ofNullable(port).orElse(DEFAULT_PORT));
public Debug(
@JsonProperty("host") Optional<String> host,
@JsonProperty("port") Optional<Integer> port
) {
this.localAddress = buildLocalAddress(
host.orElse(DEFAULT_HOST),
port.orElse(DEFAULT_PORT)
);
}

private InetSocketAddress buildLocalAddress(String host, Integer port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class GeneratedInputPlugin implements InputPlugin {
private final boolean sameHost;

@JsonCreator
public GeneratedInputPlugin(@JsonProperty("sameHost") Boolean sameHost) {
this.sameHost = Optional.ofNullable(sameHost).orElse(false);
public GeneratedInputPlugin(@JsonProperty("sameHost") Optional<Boolean> sameHost) {
this.sameHost = sameHost.orElse(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ public class InputManagerModule {

@JsonCreator
public InputManagerModule(
@JsonProperty("plugins") List<InputPlugin> plugins, @JsonProperty("filter") Filter filter
@JsonProperty("plugins") Optional<List<InputPlugin>> plugins,
@JsonProperty("filter") Optional<Filter> filter
) {
this.plugins = Optional.ofNullable(plugins).orElse(DEFAULT_PLUGINS);
this.filter = Optional.ofNullable(filter).orElseGet(TrueFilter::new);
this.plugins = plugins.orElse(DEFAULT_PLUGINS);
this.filter = filter.orElseGet(TrueFilter::new);
}

public Module module() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class NoopOutputPlugin implements OutputPlugin {
private final Long flushInterval;

@JsonCreator
public NoopOutputPlugin(@JsonProperty("flushInterval") Long flushInterval) {
this.flushInterval = Optional.ofNullable(flushInterval).orElse(DEFAULT_FLUSH_INTERVAL);
public NoopOutputPlugin(@JsonProperty("flushInterval") Optional<Long> flushInterval) {
this.flushInterval = flushInterval.orElse(DEFAULT_FLUSH_INTERVAL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public class OutputManagerModule {

@JsonCreator
public OutputManagerModule(
@JsonProperty("plugins") List<OutputPlugin> plugins, @JsonProperty("filter") Filter filter
@JsonProperty("plugins") Optional<List<OutputPlugin>> plugins,
@JsonProperty("filter") Optional<Filter> filter
) {
this.plugins = Optional.ofNullable(plugins).orElse(DEFAULT_PLUGINS);
this.filter = Optional.ofNullable(filter).orElseGet(TrueFilter::new);
this.plugins = plugins.orElse(DEFAULT_PLUGINS);
this.filter = filter.orElseGet(TrueFilter::new);
}

public Module module() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ public class CarbonInputPlugin implements InputPlugin {

@JsonCreator
public CarbonInputPlugin(
@JsonProperty("protocol") final ProtocolFactory protocol,
@JsonProperty("delimiter") final String delimiter,
@JsonProperty("retry") final RetryPolicy retry, @JsonProperty("key") final String key
@JsonProperty("protocol") final Optional<ProtocolFactory> protocolFactory,
@JsonProperty("delimiter") final Optional<String> delimiter,
@JsonProperty("retry") final Optional<RetryPolicy> retry,
@JsonProperty("key") final Optional<String> key
) {
this.protocol = Optional
.ofNullable(protocol)
this.protocol = protocolFactory
.orElseGet(ProtocolFactory.defaultFor())
.protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
this.protocolServer =
parseProtocolServer(Optional.ofNullable(delimiter).orElseGet(this::defaultDelimiter));
this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
this.metricKey = Optional.ofNullable(key).orElse(DEFAULT_KEY);
parseProtocolServer(delimiter.orElseGet(this::defaultDelimiter));
this.retry = retry.orElseGet(RetryPolicy.Exponential::new);
this.metricKey = key.orElse(DEFAULT_KEY);
}

private String defaultDelimiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public class JsonInputPlugin implements InputPlugin {

@JsonCreator
public JsonInputPlugin(
@JsonProperty("protocol") ProtocolFactory protocol,
@JsonProperty("delimiter") String delimiter, @JsonProperty("retry") RetryPolicy retry
@JsonProperty("protocol") Optional<ProtocolFactory> protocolFactory,
@JsonProperty("delimiter") Optional<String> delimiter,
@JsonProperty("retry") Optional<RetryPolicy> retry
) {
this.protocol = Optional
.ofNullable(protocol)
this.protocol = protocolFactory
.orElseGet(ProtocolFactory.defaultFor())
.protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
this.protocolServer =
parseProtocolServer(Optional.ofNullable(delimiter).orElseGet(this::defaultDelimiter));
this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
parseProtocolServer(delimiter.orElseGet(this::defaultDelimiter));
this.retry = retry.orElseGet(RetryPolicy.Exponential::new);
}

private String defaultDelimiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,21 @@ public class KafkaOutputPlugin implements OutputPlugin {

@JsonCreator
public KafkaOutputPlugin(
@JsonProperty("producer") Map<String, String> properties,
@JsonProperty("flushInterval") Long flushInterval,
@JsonProperty("router") KafkaRouter router,
@JsonProperty("partitioner") KafkaPartitioner partitioner,
@JsonProperty("serializer") Serializer serializer,
@JsonProperty("batchSize") Integer batchSize,
@JsonProperty("compression") Boolean compression
@JsonProperty("producer") Optional<Map<String, String>> properties,
@JsonProperty("flushInterval") Optional<Long> flushInterval,
@JsonProperty("router") Optional<KafkaRouter> router,
@JsonProperty("partitioner") Optional<KafkaPartitioner> partitioner,
@JsonProperty("serializer") Optional<Serializer> serializer,
@JsonProperty("batchSize") Optional<Integer> batchSize,
@JsonProperty("compression") Optional<Boolean> compression
) {
this.router = Optional.ofNullable(router).orElseGet(KafkaRouter.Tag.supplier());
this.partitioner = Optional.ofNullable(partitioner)
.orElseGet(KafkaPartitioner.Host::new);
this.flushInterval = Optional.ofNullable(flushInterval);
this.properties = Optional.ofNullable(properties).orElseGet(HashMap::new);
this.serializer = Optional.ofNullable(serializer);
this.batchSize = Optional.ofNullable(batchSize).orElse(DEFAULT_BATCH_SIZE);
this.compression = Optional.ofNullable(compression).orElse(DEFAULT_COMPRESSION);
this.router = router.orElseGet(KafkaRouter.Tag.supplier());
this.partitioner = partitioner.orElseGet(KafkaPartitioner.Host::new);
this.flushInterval = flushInterval;
this.properties = properties.orElseGet(HashMap::new);
this.serializer = serializer;
this.batchSize = batchSize.orElse(DEFAULT_BATCH_SIZE);
this.compression = compression.orElse(DEFAULT_COMPRESSION);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Tag implements KafkaPartitioner {
private final String tagKey;

@JsonCreator
public Tag(@JsonProperty("tag") final String tagKey) {
this.tagKey = Optional.ofNullable(tagKey).orElse(DEFAULT_TAGKEY);
public Tag(@JsonProperty("tag") final Optional<String> tagKey) {
this.tagKey = tagKey.orElse(DEFAULT_TAGKEY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ class Tag implements KafkaRouter {

@JsonCreator
public Tag(
@JsonProperty("tag") final String tagKey, @JsonProperty("metrics") String metrics,
@JsonProperty("events") String events
@JsonProperty("tag") final Optional<String> tagKey,
@JsonProperty("metrics") Optional<String> metrics,
@JsonProperty("events") Optional<String> events
) {
this.tagKey = Optional.ofNullable(tagKey).orElse(DEFAULT_TAGKEY);
this.metrics = Optional.ofNullable(metrics).orElse(DEFAULT_METRICS);
this.events = Optional.ofNullable(events).orElse(DEFAULT_EVENTS);
this.tagKey = tagKey.orElse(DEFAULT_TAGKEY);
this.metrics = metrics.orElse(DEFAULT_METRICS);
this.events = events.orElse(DEFAULT_EVENTS);
}

@Override
Expand Down Expand Up @@ -91,10 +92,11 @@ class Static implements KafkaRouter {

@JsonCreator
public Static(
@JsonProperty("metrics") String metrics, @JsonProperty("events") String events
@JsonProperty("metrics") Optional<String> metrics,
@JsonProperty("events") Optional<String> events
) {
this.metrics = Optional.ofNullable(metrics).orElse(DEFAULT_METRICS);
this.events = Optional.ofNullable(events).orElse(DEFAULT_EVENTS);
this.metrics = metrics.orElse(DEFAULT_METRICS);
this.events = events.orElse(DEFAULT_EVENTS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public class ProtobufInputPlugin implements InputPlugin {

@JsonCreator
public ProtobufInputPlugin(
@JsonProperty("protocol") ProtocolFactory protocol, @JsonProperty("retry") RetryPolicy retry
@JsonProperty("protocol") Optional<ProtocolFactory> protocolFactory,
@JsonProperty("retry") Optional<RetryPolicy> retry
) {
this.protocol = Optional
.ofNullable(protocol)
this.protocol = protocolFactory
.orElseGet(ProtocolFactory.defaultFor())
.protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
this.protocolServer = parseProtocolServer();
this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
this.retry = retry.orElseGet(RetryPolicy.Exponential::new);
}

private Class<? extends ProtocolServer> parseProtocolServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public class RiemannInputPlugin implements InputPlugin {

@JsonCreator
public RiemannInputPlugin(
@JsonProperty("protocol") ProtocolFactory protocol, @JsonProperty("retry") RetryPolicy retry
@JsonProperty("protocol") Optional<ProtocolFactory> protocolFactory,
@JsonProperty("retry") Optional<RetryPolicy> retry
) {
this.protocol = Optional
.ofNullable(protocol)
this.protocol = protocolFactory
.orElseGet(ProtocolFactory.defaultFor())
.protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
this.protocolServer = parseProtocolServer();
this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
this.retry = retry.orElseGet(RetryPolicy.Exponential::new);
}

private Class<? extends ProtocolServer> parseProtocolServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public class RiemannOutputPlugin implements OutputPlugin {

@JsonCreator
public RiemannOutputPlugin(
@JsonProperty("filter") Filter filter, @JsonProperty("flushInterval") Long flushInterval,
@JsonProperty("protocol") ProtocolFactory protocol,
@JsonProperty("retry") RetryPolicy retry
@JsonProperty("filter") Optional<Filter> filter,
@JsonProperty("flushInterval") Optional<Long> flushInterval,
@JsonProperty("protocol") Optional<ProtocolFactory> protocolFactory,
@JsonProperty("retry") Optional<RetryPolicy> retry
) {
this.filter = Optional.ofNullable(filter).orElseGet(TrueFilter::new);
this.flushInterval = Optional.ofNullable(flushInterval).orElse(DEFAULT_FLUSH_INTERVAL);
this.protocol = Optional
.ofNullable(protocol)
this.filter = filter.orElseGet(TrueFilter::new);
this.flushInterval = flushInterval.orElse(DEFAULT_FLUSH_INTERVAL);
this.protocol = protocolFactory
.orElseGet(ProtocolFactory.defaultFor())
.protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
this.protocolClient = parseProtocolClient();
this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
this.retry = retry.orElseGet(RetryPolicy.Exponential::new);
}

private Class<? extends ProtocolClient> parseProtocolClient() {
Expand Down
Loading

0 comments on commit 8b53042

Please sign in to comment.