io.getunleash.UnleashContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unleash-client-java Show documentation
Show all versions of unleash-client-java Show documentation
A client library for Unleash
package io.getunleash;
import io.getunleash.lang.Nullable;
import io.getunleash.util.UnleashConfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class UnleashContext {
private final Optional appName;
private final Optional environment;
private final Optional userId;
private final Optional sessionId;
private final Optional remoteAddress;
private final Map properties;
public UnleashContext(
String userId, String sessionId, String remoteAddress, Map properties) {
this(null, null, userId, sessionId, remoteAddress, properties);
}
public UnleashContext(
@Nullable String appName,
@Nullable String environment,
@Nullable String userId,
@Nullable String sessionId,
@Nullable String remoteAddress,
Map properties) {
this.appName = Optional.ofNullable(appName);
this.environment = Optional.ofNullable(environment);
this.userId = Optional.ofNullable(userId);
this.sessionId = Optional.ofNullable(sessionId);
this.remoteAddress = Optional.ofNullable(remoteAddress);
this.properties = properties;
}
public Optional getUserId() {
return userId;
}
public Optional getSessionId() {
return sessionId;
}
public Optional getRemoteAddress() {
return remoteAddress;
}
public Map getProperties() {
return properties;
}
public Optional getAppName() {
return appName;
}
public Optional getEnvironment() {
return environment;
}
public Optional getByName(String contextName) {
switch (contextName) {
case "environment":
return environment;
case "appName":
return appName;
case "userId":
return userId;
case "sessionId":
return sessionId;
case "remoteAddress":
return remoteAddress;
default:
return Optional.ofNullable(properties.get(contextName));
}
}
public UnleashContext applyStaticFields(UnleashConfig config) {
Builder builder = new Builder(this);
if (!this.environment.isPresent()) {
builder.environment(config.getEnvironment());
}
if (!this.appName.isPresent()) {
builder.appName(config.getAppName());
}
return builder.build();
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
@Nullable private String appName;
@Nullable private String environment;
@Nullable private String userId;
@Nullable private String sessionId;
@Nullable private String remoteAddress;
private final Map properties = new HashMap<>();
public Builder() {}
public Builder(UnleashContext context) {
context.appName.ifPresent(val -> this.appName = val);
context.environment.ifPresent(val -> this.environment = val);
context.userId.ifPresent(val -> this.userId = val);
context.sessionId.ifPresent(val -> this.sessionId = val);
context.remoteAddress.ifPresent(val -> this.remoteAddress = val);
context.properties.forEach(this.properties::put);
}
public Builder appName(String appName) {
this.appName = appName;
return this;
}
public Builder environment(String environment) {
this.environment = environment;
return this;
}
public Builder userId(String userId) {
this.userId = userId;
return this;
}
public Builder sessionId(String sessionId) {
this.sessionId = sessionId;
return this;
}
public Builder remoteAddress(String remoteAddress) {
this.remoteAddress = remoteAddress;
return this;
}
public Builder addProperty(String name, String value) {
properties.put(name, value);
return this;
}
public UnleashContext build() {
return new UnleashContext(
appName, environment, userId, sessionId, remoteAddress, properties);
}
}
}