Skip to content

Commit

Permalink
feat: Implement MultiPatternReplaceableResult
Browse files Browse the repository at this point in the history
fixes #23
  • Loading branch information
4drian3d committed Jun 22, 2022
1 parent 56487aa commit e858bef
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import me.dreamerzero.chatregulator.enums.InfractionType;
import me.dreamerzero.chatregulator.result.Result;
import net.kyori.adventure.builder.AbstractBuilder;
import me.dreamerzero.chatregulator.result.MultiPatternReplaceableResult;
import me.dreamerzero.chatregulator.result.PatternResult;
import me.dreamerzero.chatregulator.result.ReplaceableResult;

Expand Down Expand Up @@ -50,7 +51,8 @@ private InfractionCheck(boolean blockable, Pattern... blockedWords){
@Override
public CompletableFuture<Result> check(final @NotNull String string){
return CompletableFuture.supplyAsync(() -> {
final List<Pattern> patterns = new ArrayList<>();
final List<Matcher> matchers = new ArrayList<>(5);
final List<Pattern> patterns = new ArrayList<>(5);
boolean detected = false;
for (final Pattern pattern : blockedWords) {
final Matcher match = pattern.matcher(string);
Expand All @@ -59,16 +61,18 @@ public CompletableFuture<Result> check(final @NotNull String string){
if (blockable) {
return new PatternResult(match.group(), blockable, pattern, match);
}
matchers.add(match);
patterns.add(pattern);
}
}
return detected
? new ReplaceableResult(patterns.toString(), true){
? new MultiPatternReplaceableResult(string, true, matchers.toArray(Matcher[]::new)){
@Override
public String replaceInfraction(){
String original = string;
for (final Pattern pattern : patterns) {
original = pattern.matcher(original).replaceAll(InfractionCheck::generateReplacement);
original = pattern.matcher(original)
.replaceAll(InfractionCheck::generateReplacement);
}
return original;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package me.dreamerzero.chatregulator.result;

import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;

import org.jetbrains.annotations.Nullable;

public class MultiPatternReplaceableResult extends Result implements IReplaceable {
private final Matcher[] matchers;

/**
* Creates a new MultiPatternReplaceableResult
* @param infractionString the infraction string
* @param infricted if it was infricted
* @param matchers the matchers
*/
public MultiPatternReplaceableResult(String infractionString, boolean infricted, Matcher[] matchers) {
super(infractionString, infricted);
this.matchers = matchers;
}

@Override
public @Nullable String replaceInfraction() {
throw new UnsupportedOperationException("Not implemented yet");
}

/**
* Obtain the corresponding {@link Matcher}s of the detections performed
*
* It can be null if the check has not yet been performed,
* if the check does not return a Matcher
* or because the check gave a negative result
* @return the matcher of this check
*/
public Matcher[] getMatchers(){
return this.matchers;
}

@Override
public boolean equals(Object o){
if(this == o) return true;
if(!(o instanceof PatternResult that)) return false;
return Objects.equals(that.getInfractionString(), this.getInfractionString())
&& that.isInfraction() == this.isInfraction();
}

@Override
public int hashCode(){
return Objects.hash(this.getInfractionString(), matchers);
}

@Override
public String toString(){
String string = Arrays.toString(matchers);
if(string.isBlank()){
return super.toString();
}
return "PatternResult["+ super.toString() + string + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public boolean isInfraction(){
public boolean equals(Object o){
if(this == o) return true;
if(!(o instanceof Result that)) return false;
return that.infractionString.equals(this.infractionString) && that.infricted == this.infricted;
return that.infractionString.equals(this.infractionString)
&& that.infricted == this.infricted;
}

@Override
Expand Down

0 comments on commit e858bef

Please sign in to comment.