Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regular expression validation with CachedRegExPatternSupplier #72

Open
QEnvelope opened this issue Apr 1, 2024 · 0 comments
Open

Regular expression validation with CachedRegExPatternSupplier #72

QEnvelope opened this issue Apr 1, 2024 · 0 comments

Comments

@QEnvelope
Copy link

QEnvelope commented Apr 1, 2024

Hi, would it be possible to replace CachedRegExPatternSupplier.map by a synchronized map? Or define custom supplier implements RegExPatternSupplier is recommended?


import java.util.WeakHashMap;

public class CachedRegExPatternSupplier implements RegExPatternSupplier {
  private final RegExPatternSupplier wrapped;
  private final WeakHashMap<String, RegExPattern> map = new WeakHashMap<>();

  public CachedRegExPatternSupplier(RegExPatternSupplier wrapped) {
    this.wrapped = wrapped;
  }

  @Override
  public RegExPattern newPattern(String pattern) throws InvalidRegexException {
    RegExPattern regExPattern1 = map.get(pattern);
    if (regExPattern1 == null) {
      regExPattern1 = wrapped.newPattern(pattern);
      map.put(pattern, regExPattern1);
    }
    return regExPattern1;
  }
}

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant