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

io.split.client.impressions.ImpressionObserver Maven / Gradle / Ivy

package io.split.client.impressions;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.Objects;

/*
According to guava's docs (https://guava.dev/releases/18.0/api/docs/com/google/common/annotations/Beta.html),
the @Beta decorator only means that the api is not frozen, and has nothing to do with behaviour stability, but
rather to a non-frozen API which may introduce breaking changes at any time in future versions.
Since the library is shaded and should not be exposed to users of the SDK, it's safe to use it here.
 */

@SuppressWarnings("UnstableApiUsage")
public class ImpressionObserver {

    private final Cache _cache;

    public ImpressionObserver(long size) {
        _cache = CacheBuilder.newBuilder()
                .maximumSize(size)
                .concurrencyLevel(4)  // Just setting the default value explicitly
                .build();
    }
    
    public Long testAndSet(Impression impression) {
        if (null == impression) {
            return null;
        }

        Long hash = ImpressionHasher.process(impression);
        Long previous = _cache.getIfPresent(hash);
        _cache.put(hash, impression.time());
        return (Objects.isNull(previous)) ? null : Math.min(previous, impression.time());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy