com.yahoo.vespa.flags.UnboundStringFlag Maven / Gradle / Ivy
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.flags;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* @author hakonhall
*/
public class UnboundStringFlag extends UnboundFlagImpl {
public UnboundStringFlag(FlagId id, String defaultValue) {
this(id, defaultValue, new FetchVector());
}
public UnboundStringFlag(FlagId id, String defaultValue, FetchVector defaultFetchVector) {
this(id, defaultValue, defaultFetchVector,
new SimpleFlagSerializer<>(TextNode::new, JsonNode::isTextual, JsonNode::asText));
}
public UnboundStringFlag(FlagId id, String defaultValue, Predicate validator) {
this(id, defaultValue, new FetchVector(), validator);
}
public UnboundStringFlag(FlagId id, String defaultValue, FetchVector fetchVector, Predicate validator) {
this(id, defaultValue, fetchVector,
new SimpleFlagSerializer<>(TextNode::new,
JsonNode::isTextual,
jsonNode -> {
if (!validator.test(jsonNode.asText()))
throw new IllegalArgumentException("Invalid value: '" + jsonNode.asText() + "'");
return jsonNode.asText();
}));
}
public UnboundStringFlag(FlagId id, String defaultValue, FetchVector defaultFetchVector,
FlagSerializer serializer) {
super(id, defaultValue, defaultFetchVector, serializer, UnboundStringFlag::new, StringFlag::new);
}
}