aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/UnboundStringFlag.java
blob: 6772cf21c5fb80af94cbc9dd6d7c2190abc4f5b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 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<String, StringFlag, UnboundStringFlag> {
    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<String> validator) {
        this(id, defaultValue, new FetchVector(), validator);
    }

    public UnboundStringFlag(FlagId id, String defaultValue, FetchVector fetchVector, Predicate<String> 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<String> serializer) {
        super(id, defaultValue, defaultFetchVector, serializer, UnboundStringFlag::new, StringFlag::new);
    }
}