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

Commit

Permalink
Merge pull request #24 from spotify/parmus/replace_guava_optional
Browse files Browse the repository at this point in the history
Parmus/replace guava optional
  • Loading branch information
parmus authored Jul 11, 2016
2 parents e01a33b + 47d8881 commit a3f09a9
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 165 deletions.
10 changes: 0 additions & 10 deletions api/src/main/java/com/spotify/ffwd/filter/TrueFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.google.common.base.Supplier;
import com.spotify.ffwd.model.Event;
import com.spotify.ffwd.model.Metric;
import lombok.Data;
Expand Down Expand Up @@ -49,13 +48,4 @@ public Filter deserialize(JsonParser p, DeserializationContext ctx)
return new TrueFilter();
}
}

public static Supplier<TrueFilter> supplier() {
return new Supplier<TrueFilter>() {
@Override
public TrueFilter get() {
return new TrueFilter();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Supplier;
import lombok.Data;

import java.net.InetSocketAddress;
import java.util.function.Supplier;

/**
* Data type suitable for building using a @JsonCreator block.
Expand Down Expand Up @@ -54,12 +54,7 @@ public ProtocolFactory(
* @return
*/
public static Supplier<ProtocolFactory> defaultFor() {
return new Supplier<ProtocolFactory>() {
@Override
public ProtocolFactory get() {
return new ProtocolFactory(null, null, null, null);
}
};
return () -> new ProtocolFactory(null, null, null, null);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions api/src/main/java/com/spotify/ffwd/protocol/RetryPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.Optional;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
Expand Down Expand Up @@ -51,7 +51,7 @@ class Constant implements RetryPolicy {

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

@Override
Expand All @@ -75,8 +75,8 @@ class Exponential implements RetryPolicy {

@JsonCreator
public Exponential(@JsonProperty("initial") Long initial, @JsonProperty("max") Long max) {
this.initial = Optional.fromNullable(initial).or(DEFAULT_INITIAL);
this.max = Optional.fromNullable(max).or(DEFAULT_MAX);
this.initial = Optional.ofNullable(initial).orElse(DEFAULT_INITIAL);
this.max = Optional.ofNullable(max).orElse(DEFAULT_MAX);
this.maxAttempt =
new Double(Math.floor(Math.log(this.max / this.initial) / Math.log(2))).intValue();
}
Expand Down Expand Up @@ -108,8 +108,8 @@ class Linear implements RetryPolicy {

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

Expand Down
38 changes: 14 additions & 24 deletions core/src/main/java/com/spotify/ffwd/AgentConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.Maps;
import com.spotify.ffwd.input.InputManagerModule;
import com.spotify.ffwd.output.OutputManagerModule;
Expand All @@ -30,6 +28,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;

@Data
public class AgentConfig {
Expand Down Expand Up @@ -65,27 +64,18 @@ public AgentConfig(
@JsonProperty("workerThreads") Integer workerThreads, @JsonProperty("ttl") Long ttl,
@JsonProperty("qlog") String qlog
) {
this.debug = Optional.fromNullable(debug);
this.host = Optional.fromNullable(host).or(this.defaultHostSupplier());
this.tags = Optional.fromNullable(tags).or(DEFAULT_TAGS);
this.input = Optional.fromNullable(input).or(InputManagerModule.supplyDefault());
this.output = Optional.fromNullable(output).or(OutputManagerModule.supplyDefault());
this.asyncThreads = Optional.fromNullable(asyncThreads).or(DEFAULT_ASYNC_THREADS);
this.debug = Optional.ofNullable(debug);
this.host = Optional.ofNullable(host).orElseGet(this::buildDefaultHost);
this.tags = Optional.ofNullable(tags).orElse(DEFAULT_TAGS);
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.fromNullable(schedulerThreads).or(DEFAULT_SCHEDULER_THREADS);
this.bossThreads = Optional.fromNullable(bossThreads).or(DEFAULT_BOSS_THREADS);
this.workerThreads = Optional.fromNullable(workerThreads).or(DEFAULT_WORKER_THREADS);
this.ttl = Optional.fromNullable(ttl).or(0L);
this.qlog = Paths.get(Optional.fromNullable(qlog).or(DEFAULT_QLOG));
}

private Supplier<String> defaultHostSupplier() {
return new Supplier<String>() {
@Override
public String get() {
return buildDefaultHost();
}
};
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));
}

private String buildDefaultHost() {
Expand All @@ -105,8 +95,8 @@ public static final class Debug {

@JsonCreator
public Debug(@JsonProperty("host") String host, @JsonProperty("port") Integer port) {
this.localAddress = buildLocalAddress(Optional.fromNullable(host).or(DEFAULT_HOST),
Optional.fromNullable(port).or(DEFAULT_PORT));
this.localAddress = buildLocalAddress(Optional.ofNullable(host).orElse(DEFAULT_HOST),
Optional.ofNullable(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 @@ -17,19 +17,20 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.PrivateModule;
import com.spotify.ffwd.input.InputPlugin;
import com.spotify.ffwd.input.PluginSource;

import java.util.Optional;

public class GeneratedInputPlugin implements InputPlugin {
private final boolean sameHost;

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

@Override
Expand Down
15 changes: 5 additions & 10 deletions core/src/main/java/com/spotify/ffwd/input/InputManagerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.inject.Key;
import com.google.inject.Module;
Expand All @@ -36,6 +34,8 @@

import java.util.List;
import java.util.Set;
import java.util.Optional;
import java.util.function.Supplier;

public class InputManagerModule {
private static final List<InputPlugin> DEFAULT_PLUGINS = Lists.newArrayList();
Expand All @@ -47,8 +47,8 @@ public class InputManagerModule {
public InputManagerModule(
@JsonProperty("plugins") List<InputPlugin> plugins, @JsonProperty("filter") Filter filter
) {
this.plugins = Optional.fromNullable(plugins).or(DEFAULT_PLUGINS);
this.filter = Optional.fromNullable(filter).or(new TrueFilter());
this.plugins = Optional.ofNullable(plugins).orElse(DEFAULT_PLUGINS);
this.filter = Optional.ofNullable(filter).orElseGet(TrueFilter::new);
}

public Module module() {
Expand Down Expand Up @@ -98,11 +98,6 @@ private void bindPlugins() {
}

public static Supplier<InputManagerModule> supplyDefault() {
return new Supplier<InputManagerModule>() {
@Override
public InputManagerModule get() {
return new InputManagerModule(null, null);
}
};
return () -> new InputManagerModule(null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Scopes;
Expand All @@ -27,6 +26,7 @@
import com.spotify.ffwd.output.OutputPluginModule;
import com.spotify.ffwd.output.PluginSink;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class NoopOutputPlugin implements OutputPlugin {
Expand All @@ -37,7 +37,7 @@ public class NoopOutputPlugin implements OutputPlugin {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.inject.Key;
import com.google.inject.Module;
Expand All @@ -37,7 +35,9 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

public class OutputManagerModule {
private static final List<OutputPlugin> DEFAULT_PLUGINS = Lists.newArrayList();
Expand All @@ -49,8 +49,8 @@ public class OutputManagerModule {
public OutputManagerModule(
@JsonProperty("plugins") List<OutputPlugin> plugins, @JsonProperty("filter") Filter filter
) {
this.plugins = Optional.fromNullable(plugins).or(DEFAULT_PLUGINS);
this.filter = Optional.fromNullable(filter).or(new TrueFilter());
this.plugins = Optional.ofNullable(plugins).orElse(DEFAULT_PLUGINS);
this.filter = Optional.ofNullable(filter).orElseGet(TrueFilter::new);
}

public Module module() {
Expand Down Expand Up @@ -119,11 +119,6 @@ private void bindPlugins() {
}

public static Supplier<OutputManagerModule> supplyDefault() {
return new Supplier<OutputManagerModule>() {
@Override
public OutputManagerModule get() {
return new OutputManagerModule(null, null);
}
};
return () -> new OutputManagerModule(null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
package com.spotify.ffwd.serializer;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.google.common.base.Supplier;
import com.spotify.ffwd.model.Event;
import com.spotify.ffwd.model.Metric;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Supplier;

@Slf4j
public class ToStringSerializer implements Serializer {
@JsonCreator
Expand All @@ -39,11 +40,6 @@ public byte[] serialize(Metric metric) throws Exception {
}

public static Supplier<Serializer> defaultSupplier() {
return new Supplier<Serializer>() {
@Override
public Serializer get() {
return new ToStringSerializer();
}
};
return ToStringSerializer::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.PrivateModule;
Expand All @@ -30,6 +29,8 @@
import com.spotify.ffwd.protocol.ProtocolType;
import com.spotify.ffwd.protocol.RetryPolicy;

import java.util.Optional;

public class CarbonInputPlugin implements InputPlugin {
private static final ProtocolType DEFAULT_PROTOCOL = ProtocolType.TCP;
private static final int DEFAULT_PORT = 20003;
Expand All @@ -51,13 +52,13 @@ public CarbonInputPlugin(
@JsonProperty("retry") final RetryPolicy retry, @JsonProperty("key") final String key
) {
this.protocol = Optional
.fromNullable(protocol)
.or(ProtocolFactory.defaultFor())
.ofNullable(protocol)
.orElseGet(ProtocolFactory.defaultFor())
.protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
this.protocolServer =
parseProtocolServer(Optional.fromNullable(delimiter).or(defaultDelimiter()));
this.retry = Optional.fromNullable(retry).or(new RetryPolicy.Exponential());
this.metricKey = Optional.fromNullable(key).or(DEFAULT_KEY);
parseProtocolServer(Optional.ofNullable(delimiter).orElseGet(this::defaultDelimiter));
this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
this.metricKey = Optional.ofNullable(key).orElse(DEFAULT_KEY);
}

private String defaultDelimiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.PrivateModule;
Expand All @@ -30,6 +29,8 @@
import com.spotify.ffwd.protocol.ProtocolType;
import com.spotify.ffwd.protocol.RetryPolicy;

import java.util.Optional;

public class JsonInputPlugin implements InputPlugin {
private static final ProtocolType DEFAULT_PROTOCOL = ProtocolType.UDP;
private static final int DEFAULT_PORT = 19000;
Expand All @@ -49,12 +50,12 @@ public JsonInputPlugin(
@JsonProperty("delimiter") String delimiter, @JsonProperty("retry") RetryPolicy retry
) {
this.protocol = Optional
.fromNullable(protocol)
.or(ProtocolFactory.defaultFor())
.ofNullable(protocol)
.orElseGet(ProtocolFactory.defaultFor())
.protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
this.protocolServer =
parseProtocolServer(Optional.fromNullable(delimiter).or(defaultDelimiter()));
this.retry = Optional.fromNullable(retry).or(new RetryPolicy.Exponential());
parseProtocolServer(Optional.ofNullable(delimiter).orElseGet(this::defaultDelimiter));
this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
}

private String defaultDelimiter() {
Expand Down
Loading

0 comments on commit a3f09a9

Please sign in to comment.