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

org.kiwiproject.consul.option.QueryOptions Maven / Gradle / Ivy

package org.kiwiproject.consul.option;

import static com.google.common.base.Preconditions.checkArgument;

import org.immutables.value.Value;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Container for common query options used by the Consul API.
 */
@Value.Immutable
@Value.Style(jakarta = true)
public abstract class QueryOptions implements ParamAdder {

    /**
     * @deprecated for removal in 2.0.0 (replaced by {@link Options#BLANK_QUERY_OPTIONS} in 1.4.0)
     */
    @Deprecated(since = "1.3.3", forRemoval = true)
    public static final QueryOptions BLANK = ImmutableQueryOptions.builder().build();

    public abstract Optional getWait();
    public abstract Optional getToken();
    public abstract Optional getHash();
    public abstract Optional getIndex();
    public abstract Optional getNear();
    public abstract Optional getDatacenter();
    public abstract Optional getFilter();
    public abstract Optional getNamespace();
    public abstract Optional getWan();
    public abstract Optional getSegment();
    public abstract Optional getNote();
    public abstract Optional getEnable();
    public abstract Optional getReason();
    public abstract List getNodeMeta();
    public abstract List getTag();

    @Value.Default
    public ConsistencyMode getConsistencyMode() {
        return ConsistencyMode.DEFAULT;
    }

    @Value.Derived
    public boolean isBlocking() {
        return getWait().isPresent();
    }

    @Value.Derived
    public boolean hasToken() {
        return getToken().isPresent();
    }

    @Value.Derived
    public List getNodeMetaQuery() {
        return List.copyOf(getNodeMeta());
    }

    @Value.Derived
    public List getTagsQuery() {
        return List.copyOf(getTag());
    }

    @Value.Check
    void validate() {
        if (isBlocking()) {
            checkArgument(getIndex().isPresent() || getHash().isPresent(), "If wait is specified, index/hash must also be specified");
            checkArgument(!(getIndex().isPresent() && getHash().isPresent()), "Cannot specify index and hash ath the same time");
        }
    }

    public static ImmutableQueryOptions.Builder blockSeconds(int seconds, BigInteger index) {
        return blockBuilder("s", seconds, index);
    }

    public static ImmutableQueryOptions.Builder blockMinutes(int minutes, BigInteger index) {
        return blockBuilder("m", minutes, index);
    }

    private static ImmutableQueryOptions.Builder blockBuilder(String identifier, int qty, BigInteger index) {
        return ImmutableQueryOptions.builder()
                .wait(String.format("%s%s", qty, identifier))
                .index(index);
    }

    public static ImmutableQueryOptions.Builder blockSeconds(int seconds, String hash) {
        return blockBuilder("s", seconds, hash);
    }

    public static ImmutableQueryOptions.Builder blockMinutes(int minutes, String hash) {
        return blockBuilder("m", minutes, hash);
    }

    private static ImmutableQueryOptions.Builder blockBuilder(String identifier, int qty, String hash) {
        return ImmutableQueryOptions.builder()
                .wait(String.format("%s%s", qty, identifier))
                .hash(hash);
    }

    @Override
    public Map toQuery() {
        Map result = new HashMap<>();

        Optional consistency = getConsistencyMode().toParam();
        consistency.ifPresent(s -> result.put(s, ""));

        if (isBlocking()) {
            Options.optionallyAdd(result, "wait", getWait());
            Options.optionallyAdd(result, "index", getIndex());
            Options.optionallyAdd(result, "hash", getHash());
        }

        Options.optionallyAdd(result, "token", getToken());
        Options.optionallyAdd(result, "near", getNear());
        Options.optionallyAdd(result, "dc", getDatacenter());
        Options.optionallyAdd(result, "filter", getFilter());
        Options.optionallyAdd(result, "ns", getNamespace());
        Options.optionallyAdd(result, "wan", getWan());
        Options.optionallyAdd(result, "segment", getSegment());
        Options.optionallyAdd(result, "note", getNote());
        Options.optionallyAdd(result, "enable", getEnable());
        Options.optionallyAdd(result, "reason", getReason());

        return result;
    }

    @Override
    public Map toHeaders() {
        return getConsistencyMode().getAdditionalHeaders();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy