From 1963b5ba2776079f53c51f46af4f131e04250390 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Fri, 15 Jul 2022 10:26:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B6=D0=BD=D0=BE=20=D1=83=D0=BA=D0=B0=D0=B7=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BA=D0=BE=20=D1=84=D1=80=D0=B0=D0=B7=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D1=8E=D0=BD=D0=B8=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../struchkov/autoresponder/Responder.java | 14 +++++---- .../struchkov/autoresponder/entity/Unit.java | 29 +++++++++++-------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index 8762eb7..cd19900 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov autoresponder - 3.1.0 + 3.2.0 jar Abstract Autoresponder diff --git a/src/main/java/dev/struchkov/autoresponder/Responder.java b/src/main/java/dev/struchkov/autoresponder/Responder.java index 010ed29..591a20d 100644 --- a/src/main/java/dev/struchkov/autoresponder/Responder.java +++ b/src/main/java/dev/struchkov/autoresponder/Responder.java @@ -48,11 +48,11 @@ public final class Responder { if (message != null && nextUnits != null) { for (U unit : nextUnits) { - final String unitPhrase = unit.getPhrase(); + final Set unitPhrases = unit.getPhrases(); if ( - unitPhrase != null - && !unitPhrase.isEmpty() - && unitPhrase.equalsIgnoreCase(message) + unitPhrases != null + && !unitPhrases.isEmpty() + && unitPhrases.contains(message) ) { searchUnit.add(unit); } @@ -79,7 +79,8 @@ public final class Responder { } private static > boolean isNotPhrase(U nextUnit) { - return nextUnit.getPhrase() == null; + final Set phrases = nextUnit.getPhrases(); + return phrases == null || phrases.isEmpty(); } private static > boolean isNotPattern(U nextUnit) { @@ -87,7 +88,8 @@ public final class Responder { } private static > boolean isNotKeyWords(U nextUnit) { - return nextUnit.getKeyWords() == null || nextUnit.getKeyWords().isEmpty(); + final Set keyWords = nextUnit.getKeyWords(); + return keyWords == null || keyWords.isEmpty(); } private static boolean patternReg(Pattern pattern, String message) { diff --git a/src/main/java/dev/struchkov/autoresponder/entity/Unit.java b/src/main/java/dev/struchkov/autoresponder/entity/Unit.java index 9c7df84..940ee47 100644 --- a/src/main/java/dev/struchkov/autoresponder/entity/Unit.java +++ b/src/main/java/dev/struchkov/autoresponder/entity/Unit.java @@ -1,5 +1,6 @@ package dev.struchkov.autoresponder.entity; +import java.util.Collection; import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -15,12 +16,12 @@ public abstract class Unit> { /** * Ключевые слова. */ - protected Set keyWords = new HashSet<>(); + protected Set keyWords; /** * Точная фраза. */ - protected String phrase; + protected Set phrases; /** * Регулярное выражение. @@ -40,18 +41,18 @@ public abstract class Unit> { /** * Множество следующих Unit в сценарии. */ - protected Set nextUnits = new HashSet<>(); + protected Set nextUnits; protected Unit( Set keyWords, - String phrase, + Set phrases, Pattern pattern, Integer matchThreshold, Integer priority, Set nextUnits ) { this.keyWords = keyWords; - this.phrase = phrase; + this.phrases = phrases; this.pattern = pattern; this.matchThreshold = matchThreshold == null ? 10 : matchThreshold; this.priority = priority == null ? 10 : priority; @@ -66,12 +67,16 @@ public abstract class Unit> { this.keyWords = keyWords; } - public String getPhrase() { - return phrase; + public Set getPhrases() { + return phrases; } - public void setPhrase(String phrase) { - this.phrase = phrase; + public void addPhrase(String phrase) { + phrases.add(phrase); + } + + public void addPhrases(Collection phrases) { + phrases.addAll(phrases); } public Pattern getPattern() { @@ -111,19 +116,19 @@ public abstract class Unit> { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Unit unit = (Unit) o; - return Objects.equals(keyWords, unit.keyWords) && Objects.equals(phrase, unit.phrase) && Objects.equals(pattern, unit.pattern) && Objects.equals(matchThreshold, unit.matchThreshold) && Objects.equals(priority, unit.priority); + return Objects.equals(keyWords, unit.keyWords) && Objects.equals(phrases, unit.phrases) && Objects.equals(pattern, unit.pattern) && Objects.equals(matchThreshold, unit.matchThreshold) && Objects.equals(priority, unit.priority); } @Override public int hashCode() { - return Objects.hash(keyWords, phrase, pattern, matchThreshold, priority); + return Objects.hash(keyWords, phrases, pattern, matchThreshold, priority); } @Override public String toString() { return "Unit{" + "keyWords=" + keyWords + - ", phrase='" + phrase + '\'' + + ", phrases='" + phrases + '\'' + ", pattern=" + pattern + ", matchThreshold=" + matchThreshold + ", priority=" + priority +