aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/JsonNodeRawFlag.java
blob: 3441c3707fd4bf3037cac1bbd031fa7bc6010598 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

import static com.yahoo.yolean.Exceptions.uncheck;

/**
 * {@link RawFlag} using Jackson's {@link JsonNode}.
 *
 * @author hakonhall
 */
public class JsonNodeRawFlag implements RawFlag {

    private static final AtomicReference<ObjectMapper> mapper = new AtomicReference<>();

    private final JsonNode jsonNode;

    private JsonNodeRawFlag(JsonNode jsonNode) {
        this.jsonNode = jsonNode;
    }

    public static JsonNodeRawFlag fromJson(String json) {
        return new JsonNodeRawFlag(uncheck(() -> objectMapper().readTree(json)));
    }

    public static JsonNodeRawFlag fromJsonNode(JsonNode jsonNode) {
        return new JsonNodeRawFlag(jsonNode);
    }

    public static <T> JsonNodeRawFlag fromJacksonClass(T value) {
        return new JsonNodeRawFlag(uncheck(() -> objectMapper().valueToTree(value)));
    }

    public <T> T toJacksonClass(Class<T> jacksonClass) {
        return uncheck(() -> objectMapper().treeToValue(jsonNode, jacksonClass));
    }

    public <T> T toJacksonClass(JavaType jacksonClass) {
        return uncheck(() -> objectMapper().readValue(jsonNode.toString(), jacksonClass));
    }

    @SuppressWarnings("rawtypes")
    public static JavaType constructCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass) {
        return objectMapper().getTypeFactory().constructCollectionType(collectionClass, elementClass);
    }

    @Override
    public JsonNode asJsonNode() {
        return jsonNode;
    }

    @Override
    public String asJson() {
        return jsonNode.toString();
    }

    @Override
    public String toString() {
        return "JsonNodeRawFlag{" +
               "jsonNode=" + jsonNode +
               '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        JsonNodeRawFlag that = (JsonNodeRawFlag) o;
        return jsonNode.equals(that.jsonNode);
    }

    @Override
    public int hashCode() {
        return Objects.hash(jsonNode);
    }

    /** Initialize object mapper lazily */
    private static ObjectMapper objectMapper() {
        // ObjectMapper is a heavy-weight object so we construct it only when we need it
        return mapper.updateAndGet((objectMapper) -> {
            if (objectMapper != null) return objectMapper;
            return new ObjectMapper();
        });
    }

}