diff options
author | Valerij Fredriksen <valerijf@oath.com> | 2019-01-11 15:12:25 +0100 |
---|---|---|
committer | Valerij Fredriksen <valerij92@gmail.com> | 2019-01-11 21:19:47 +0100 |
commit | 9292e734e54576c50fc4d88d3fed9aaa4cfc022b (patch) | |
tree | ed18ce68b46df1820457826d75459ac2f0732b43 /flags | |
parent | 0922672e4709f02cf0da1c32de9fcc583a30f8b8 (diff) |
Add basic InMemoryFlagSource
Diffstat (limited to 'flags')
-rw-r--r-- | flags/src/main/java/com/yahoo/vespa/flags/InMemoryFlagSource.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/flags/src/main/java/com/yahoo/vespa/flags/InMemoryFlagSource.java b/flags/src/main/java/com/yahoo/vespa/flags/InMemoryFlagSource.java new file mode 100644 index 00000000000..25b871f8cd1 --- /dev/null +++ b/flags/src/main/java/com/yahoo/vespa/flags/InMemoryFlagSource.java @@ -0,0 +1,34 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.flags; + +import com.yahoo.vespa.flags.json.FlagData; +import com.yahoo.vespa.flags.json.Rule; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Basic in-memory flag source useful for testing + * + * @author freva + */ +public class InMemoryFlagSource implements FlagSource { + private final Map<FlagId, FlagData> flagDataById = new ConcurrentHashMap<>(); + + public InMemoryFlagSource withFlag(FlagId flagId) { + flagDataById.put(flagId, new FlagData(flagId)); + return this; + } + + public InMemoryFlagSource withFlag(FlagId flagId, FetchVector defaultFetchVector, Rule... rules) { + flagDataById.put(flagId, new FlagData(flagId, defaultFetchVector, rules)); + return this; + } + + @Override + public Optional<RawFlag> fetch(FlagId id, FetchVector vector) { + return Optional.ofNullable(flagDataById.get(id)) + .flatMap(flagData -> flagData.resolve(vector)); + } +} |