Рефакторинг

This commit is contained in:
serega6531
2020-03-14 19:13:03 +03:00
parent 3a697f79e5
commit d3ed284ebe
2 changed files with 63 additions and 48 deletions

View File

@@ -4,7 +4,6 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.codec.Hex; import org.springframework.security.crypto.codec.Hex;
import ru.serega6531.packmate.model.FoundPattern; import ru.serega6531.packmate.model.FoundPattern;
import ru.serega6531.packmate.model.Pattern; import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.model.enums.PatternSearchType;
import ru.serega6531.packmate.utils.Bytes; import ru.serega6531.packmate.utils.Bytes;
import java.util.*; import java.util.*;
@@ -20,19 +19,32 @@ class PatternMatcher {
private final Set<FoundPattern> result = new HashSet<>(); private final Set<FoundPattern> result = new HashSet<>();
public PatternMatcher(byte[] contentBytes, List<Pattern> patterns) { PatternMatcher(byte[] contentBytes, List<Pattern> patterns) {
this.contentBytes = contentBytes; this.contentBytes = contentBytes;
this.content = new String(contentBytes); this.content = new String(contentBytes);
this.patterns = patterns; this.patterns = patterns;
} }
public Set<FoundPattern> findMatches() { Set<FoundPattern> findMatches() {
patterns.forEach(this::match); patterns.forEach(this::match);
return result; return result;
} }
private void match(Pattern pattern) { private void match(Pattern pattern) {
if (pattern.getSearchType() == PatternSearchType.REGEX) { switch (pattern.getSearchType()) {
case REGEX:
matchRegex(pattern);
break;
case SUBSTRING:
matchSubstring(pattern);
break;
case SUBBYTES:
matchSubbytes(pattern);
break;
}
}
private void matchRegex(Pattern pattern) {
final var regex = compilePattern(pattern); final var regex = compilePattern(pattern);
final Matcher matcher = regex.matcher(content); final Matcher matcher = regex.matcher(content);
int startPos = 0; int startPos = 0;
@@ -45,7 +57,9 @@ class PatternMatcher {
.build()); .build());
startPos = matcher.end(); startPos = matcher.end();
} }
} else if (pattern.getSearchType() == PatternSearchType.SUBSTRING) { }
private void matchSubstring(Pattern pattern) {
int startSearch = 0; int startSearch = 0;
final String value = pattern.getValue(); final String value = pattern.getValue();
@@ -65,7 +79,9 @@ class PatternMatcher {
startSearch = end + 1; startSearch = end + 1;
} }
} else if (pattern.getSearchType() == PatternSearchType.SUBBYTES) { }
private void matchSubbytes(Pattern pattern) {
int startSearch = 0; int startSearch = 0;
final byte[] value = Hex.decode(pattern.getValue()); final byte[] value = Hex.decode(pattern.getValue());
@@ -86,7 +102,6 @@ class PatternMatcher {
startSearch = end + 1; startSearch = end + 1;
} }
} }
}
private void addIfPossible(FoundPattern found) { private void addIfPossible(FoundPattern found) {
if (result.stream().noneMatch(match -> between(match.getStartPosition(), match.getEndPosition(), found.getStartPosition()) || if (result.stream().noneMatch(match -> between(match.getStartPosition(), match.getEndPosition(), found.getStartPosition()) ||

View File

@@ -39,7 +39,7 @@ public class StreamSubscriptionService {
log.info("Отписан пользователь {}", Objects.requireNonNull(session.getRemoteAddress()).getHostName()); log.info("Отписан пользователь {}", Objects.requireNonNull(session.getRemoteAddress()).getHostName());
} }
public void broadcast(SubscriptionMessage message) { void broadcast(SubscriptionMessage message) {
subscribers.forEach(s -> { subscribers.forEach(s -> {
try { try {
s.sendMessage(objectToTextMessage(message)); s.sendMessage(objectToTextMessage(message));