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

com.wavefront.predicates.CachingRegexMatcher Maven / Gradle / Ivy

There is a newer version: 2023-22.3
Show newest version
package com.wavefront.predicates;

import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.regex.Pattern;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;

/**
 * A pattern matcher that uses a cache of compiled patterns.
 *
 * @author [email protected].
 */
public class CachingRegexMatcher implements BiFunction {
  private final LoadingCache patternCache;

  public CachingRegexMatcher() {
    this(0);
  }

  public CachingRegexMatcher(int flags) {
     this.patternCache = Caffeine.newBuilder().
        maximumSize(10000).
        expireAfterAccess(1, TimeUnit.MINUTES).
        build(regex -> Pattern.compile(regex, flags));
  }

  @Override
  public Boolean apply(String s, String s2) {
    return patternCache.get(s2).matcher(s).matches();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy