Добавлена возможность делать паттерны регулярками

This commit is contained in:
serega6531
2019-05-12 02:11:07 +03:00
parent 3ce16c5bd3
commit 1e08c70a3b
2 changed files with 20 additions and 2 deletions

View File

@@ -7,8 +7,9 @@ import org.springframework.stereotype.Service;
import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.repository.PatternRepository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@@ -17,6 +18,8 @@ public class PatternService {
private final PatternRepository repository;
private Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
@Autowired
public PatternService(PatternRepository repository) {
this.repository = repository;
@@ -30,10 +33,19 @@ public class PatternService {
String content = new String(bytes);
return findAll().stream()
.filter(pattern -> StringUtils.containsIgnoreCase(content, pattern.getValue()))
.filter(pattern -> matches(pattern, content))
.collect(Collectors.toList());
}
private boolean matches(Pattern pattern, String content) {
if(pattern.isRegex()) {
final java.util.regex.Pattern regex = compilePattern(pattern);
return regex.matcher(content).find();
} else {
return StringUtils.containsIgnoreCase(content, pattern.getValue());
}
}
public void deleteById(int id) {
repository.deleteById(id);
}
@@ -43,4 +55,8 @@ public class PatternService {
return repository.save(pattern);
}
public java.util.regex.Pattern compilePattern(Pattern pattern) {
return compiledPatterns.computeIfAbsent(pattern.getValue(), java.util.regex.Pattern::compile);
}
}