Added caching to literal pattern compilation
This commit is contained in:
@@ -2,6 +2,7 @@ package com.willfp.eco.core.placeholder;
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import com.willfp.eco.util.PatternUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -40,7 +41,7 @@ public final class PlayerPlaceholder implements RegistrablePlaceholder {
|
||||
@NotNull final String identifier,
|
||||
@NotNull final Function<@NotNull Player, @Nullable String> function) {
|
||||
this.plugin = plugin;
|
||||
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
|
||||
this.pattern = PatternUtils.compileLiteral(identifier);
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.eco.core.placeholder;
|
||||
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import com.willfp.eco.util.PatternUtils;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -38,7 +39,7 @@ public final class PlayerStaticPlaceholder implements InjectablePlaceholder {
|
||||
public PlayerStaticPlaceholder(@NotNull final String identifier,
|
||||
@NotNull final Function<@NotNull Player, @Nullable String> function) {
|
||||
this.identifier = "%" + identifier + "%";
|
||||
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
|
||||
this.pattern = PatternUtils.compileLiteral(identifier);
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.willfp.eco.core.placeholder;
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import com.willfp.eco.util.PatternUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -39,7 +40,7 @@ public final class PlayerlessPlaceholder implements RegistrablePlaceholder {
|
||||
@NotNull final String identifier,
|
||||
@NotNull final Supplier<@Nullable String> function) {
|
||||
this.plugin = plugin;
|
||||
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
|
||||
this.pattern = PatternUtils.compileLiteral(identifier);
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.eco.core.placeholder;
|
||||
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import com.willfp.eco.util.PatternUtils;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -37,7 +38,7 @@ public final class StaticPlaceholder implements InjectablePlaceholder {
|
||||
public StaticPlaceholder(@NotNull final String identifier,
|
||||
@NotNull final Supplier<@Nullable String> function) {
|
||||
this.identifier = "%" + identifier + "%";
|
||||
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
|
||||
this.pattern = PatternUtils.compileLiteral(identifier);
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.willfp.eco.core.placeholder.templates;
|
||||
|
||||
import com.willfp.eco.core.placeholder.InjectablePlaceholder;
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import com.willfp.eco.util.PatternUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -28,7 +29,7 @@ public abstract class SimpleInjectablePlaceholder implements InjectablePlacehold
|
||||
*/
|
||||
protected SimpleInjectablePlaceholder(@NotNull final String identifier) {
|
||||
this.identifier = identifier;
|
||||
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
|
||||
this.pattern = PatternUtils.compileLiteral(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.core.placeholder.templates;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.placeholder.RegistrablePlaceholder;
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import com.willfp.eco.util.PatternUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -37,7 +38,7 @@ public abstract class SimplePlaceholder implements RegistrablePlaceholder {
|
||||
@NotNull final String identifier) {
|
||||
this.plugin = plugin;
|
||||
this.identifier = identifier;
|
||||
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
|
||||
this.pattern = PatternUtils.compileLiteral(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
35
eco-api/src/main/java/com/willfp/eco/util/PatternUtils.java
Normal file
35
eco-api/src/main/java/com/willfp/eco/util/PatternUtils.java
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user