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

com.github.tomakehurst.wiremock.matching.MemoizingStringValuePattern Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
package com.github.tomakehurst.wiremock.matching;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;

import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;

public abstract class MemoizingStringValuePattern extends StringValuePattern {

    private final LoadingCache cache = CacheBuilder.newBuilder()
            .build(new CacheLoader() {
                @Override
                public MatchResult load(String value) {
                    return new MemoizingMatchResult(calculateMatch(value));
                }
            });

    public MemoizingStringValuePattern(String expectedValue) {
        super(expectedValue);
    }

    @Override
    public final MatchResult match(String value) {
        if (value == null) {
            return MatchResult.noMatch();
        }

        try {
            return cache.get(value);
        } catch (ExecutionException e) {
            return throwUnchecked(e, MatchResult.class);
        }
    }

    protected abstract MatchResult calculateMatch(String value);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy