All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.getunleash.UnleashContext Maven / Gradle / Ivy

The newest version!
package io.getunleash;

import io.getunleash.lang.Nullable;
import io.getunleash.util.UnleashConfig;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
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 Optional currentTime;

    private final Map properties;

    public UnleashContext(
            String userId, String sessionId, String remoteAddress, Map properties) {
        this(null, null, userId, sessionId, remoteAddress, null, properties);
    }

    public UnleashContext(
            @Nullable String appName,
            @Nullable String environment,
            @Nullable String userId,
            @Nullable String sessionId,
            @Nullable String remoteAddress,
            Map properties) {
        this(appName, environment, userId, sessionId, remoteAddress, null, properties);
    }

    public UnleashContext(
            @Nullable String appName,
            @Nullable String environment,
            @Nullable String userId,
            @Nullable String sessionId,
            @Nullable String remoteAddress,
            @Nullable ZonedDateTime currentTime,
            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.currentTime = Optional.ofNullable(currentTime);
        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 getCurrentTime() {
        return currentTime;
    }

    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;
        @Nullable private ZonedDateTime currentTime;

        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.currentTime.ifPresent(val -> this.currentTime = 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 currentTime(ZonedDateTime currentTime) {
            this.currentTime = currentTime;
            return this;
        }

        /**
         * Defaults to now() in UTC timezone
         *
         * @return this
         */
        public Builder now() {
            this.currentTime = ZonedDateTime.now(ZoneOffset.UTC);
            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,
                    currentTime,
                    properties);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy