/
CapsCheck.java
88 lines (75 loc) · 2.38 KB
/
CapsCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package me.dreamerzero.chatregulator.modules.checks;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import org.jetbrains.annotations.NotNull;
import me.dreamerzero.chatregulator.config.Configuration;
import me.dreamerzero.chatregulator.enums.InfractionType;
import me.dreamerzero.chatregulator.result.Result;
import net.kyori.adventure.builder.AbstractBuilder;
import me.dreamerzero.chatregulator.result.ReplaceableResult;
/**
* Check for compliance with uppercase character limit in a string
*/
public final class CapsCheck implements ICheck {
private final int limit;
private CapsCheck(){
this.limit = Configuration.getConfig().getCapsConfig().limit();
}
private CapsCheck(int limit){
this.limit = limit;
}
/**
* {@inheritDoc}
*
* @return A {@link ReplaceableResult} if the check was succesfully or a {@link Result} if not
*/
@Override
public CompletableFuture<Result> check(final @NotNull String string) {
return CompletableFuture.supplyAsync(() -> Objects.requireNonNull(string)
.chars()
.filter(Character::isUpperCase)
.count() >= this.limit
? new ReplaceableResult(string, true){
@Override
public String replaceInfraction(){
return string.toLowerCase(Locale.ROOT);
}
}
: new Result(string, false));
}
public static CompletableFuture<Result> createCheck(String string){
return new CapsCheck().check(string);
}
@Override
public @NotNull InfractionType type() {
return InfractionType.CAPS;
}
/**
* Create a builder
* @return a new CapsCheck.Builder
*/
public static CapsCheck.Builder builder(){
return new CapsCheck.Builder();
}
/**Caps Check Builder */
public static class Builder implements AbstractBuilder<CapsCheck> {
private int limit;
Builder(){}
/**
* Set the new caps limit
* @param limit the new limit
* @return this
*/
public Builder limit(int limit){
this.limit = limit;
return this;
}
@Override
public CapsCheck build(){
return limit == 0
? new CapsCheck()
: new CapsCheck(limit);
}
}
}