-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added caching to literal pattern compilation
- Loading branch information
Showing
7 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
eco-api/src/main/java/com/willfp/eco/util/PatternUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.willfp.eco.util; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Utilities / API methods for patterns. | ||
*/ | ||
public final class PatternUtils { | ||
/** | ||
* Cache of compiled literal patterns. | ||
*/ | ||
private static final Cache<String, Pattern> LITERAL_PATTERN_CACHE = Caffeine.newBuilder() | ||
.expireAfterAccess(1, TimeUnit.MINUTES) | ||
.build(); | ||
|
||
/** | ||
* Compile a literal pattern. | ||
* | ||
* @param pattern The pattern. | ||
* @return The compiled pattern. | ||
*/ | ||
@NotNull | ||
public static Pattern compileLiteral(@NotNull final String pattern) { | ||
return LITERAL_PATTERN_CACHE.get(pattern, (it) -> Pattern.compile(it, Pattern.LITERAL)); | ||
} | ||
|
||
private PatternUtils() { | ||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); | ||
} | ||
} |