diff --git a/src/main/java/org/sadtech/autoresponder/AutoResponder.java b/src/main/java/org/sadtech/autoresponder/AutoResponder.java index 792c722..3b87314 100644 --- a/src/main/java/org/sadtech/autoresponder/AutoResponder.java +++ b/src/main/java/org/sadtech/autoresponder/AutoResponder.java @@ -22,23 +22,23 @@ import java.util.Set; * @author upagge [07/07/2019] */ @Slf4j -public class AutoResponder { +public class AutoResponder { @Description("Компоратор для сортировки Unit-ов") - private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator(); + private final UnitPriorityComparator unitPriorityComparator = new UnitPriorityComparator<>(); @Description("Начальные юниты, первый запрос приходит на них") - private final Set startUnits; + private final Set startUnits; @Getter @Setter @Description("Дефолтный юнит, отправляется если ни один Unit не подошел") - private Unit defaultUnit; + private U defaultUnit; @Getter - private final UnitPointerService unitPointerService; + private final UnitPointerService unitPointerService; - public AutoResponder(UnitPointerService unitPointerService, Set startUnits) { + public AutoResponder(UnitPointerService unitPointerService, Set startUnits) { this.startUnits = startUnits; this.unitPointerService = unitPointerService; } @@ -50,14 +50,14 @@ public class AutoResponder { * @param message Запрос пользователя - текстовое сообщение * @return {@link Unit}, который отвечает за данные для обработки данного запроса */ - public Unit answer(@NonNull Integer personId, String message) { - UnitPointer unitPointer = checkAndAddPerson(personId); - Unit unit; + public U answer(@NonNull Integer personId, String message) { + UnitPointer unitPointer = checkAndAddPerson(personId); + U unit; try { if (unitPointer.getUnit() == null || unitPointer.getUnit().getNextUnits() == null || unitPointer.getUnit().getNextUnits().isEmpty()) { unit = nextUnit(startUnits, message); } else { - unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); + unit = (U) nextUnit(unitPointer.getUnit().getNextUnits(), message); } unitPointerService.edit(personId, unit); } catch (NotFoundUnitException e) { @@ -73,8 +73,8 @@ public class AutoResponder { * @param message Запрос пользователя - текстовое сообщение * @return Юнит, который нуждается в обработке в соответствии с запросом пользователя */ - private Unit nextUnit(@NonNull Set nextUnits, String message) { - Set searchUnit = new HashSet<>(); + private U nextUnit(@NonNull Set nextUnits, String message) { + Set searchUnit = new HashSet<>(); nextUnits.stream() .filter(nextUnit -> nextUnit.getPhrase() != null @@ -97,7 +97,7 @@ public class AutoResponder { .forEach(searchUnit::add); } - return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR).orElseThrow(NotFoundUnitException::new); + return searchUnit.stream().max(unitPriorityComparator).orElseThrow(NotFoundUnitException::new); } /** @@ -106,12 +106,12 @@ public class AutoResponder { * @param personId Идентификатор пользователя * @return {@link UnitPointer}, который сохраняет {@link Unit}, который был обработан последним */ - private UnitPointer checkAndAddPerson(@NonNull Integer personId) { - UnitPointer unitPointer; + private UnitPointer checkAndAddPerson(@NonNull Integer personId) { + UnitPointer unitPointer; if (unitPointerService.check(personId)) { unitPointer = unitPointerService.getByEntityId(personId); } else { - unitPointer = new UnitPointer(personId); + unitPointer = new UnitPointer<>(personId); unitPointerService.add(unitPointer); } return unitPointer; diff --git a/src/main/java/org/sadtech/autoresponder/compare/UnitPriorityComparator.java b/src/main/java/org/sadtech/autoresponder/compare/UnitPriorityComparator.java index 7b13c47..8fc1421 100644 --- a/src/main/java/org/sadtech/autoresponder/compare/UnitPriorityComparator.java +++ b/src/main/java/org/sadtech/autoresponder/compare/UnitPriorityComparator.java @@ -9,7 +9,7 @@ import java.util.Comparator; * * @author upagge [07/07/2019] */ -public class UnitPriorityComparator implements Comparator { +public class UnitPriorityComparator implements Comparator { @Override public int compare(Unit o1, Unit o2) { diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index cb00fb0..c00ed4f 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -19,7 +19,7 @@ import java.util.regex.Pattern; @EqualsAndHashCode @ToString @Setter -public abstract class Unit { +public abstract class Unit { @Description("Ключевые слова") protected Set keyWords; @@ -37,9 +37,9 @@ public abstract class Unit { protected Integer priority; @Description("Множество следующих Unit в сценарии") - protected Set nextUnits; + protected Set nextUnits; - protected Unit(Set keyWords, String phrase, Pattern pattern, Integer matchThreshold, Integer priority, Set nextUnits) { + protected Unit(Set keyWords, String phrase, Pattern pattern, Integer matchThreshold, Integer priority, Set nextUnits) { this.keyWords = keyWords; this.phrase = phrase; this.pattern = pattern; diff --git a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java index 9b19434..05aec5f 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java +++ b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java @@ -11,13 +11,13 @@ import org.sadtech.autoresponder.util.Description; */ @Data @AllArgsConstructor -public class UnitPointer { +public class UnitPointer { @Description("Идентификатор пользователя") private Integer entityId; @Description("Юнит, который был обработан") - private Unit unit; + private U unit; public UnitPointer(Integer entityId) { this.entityId = entityId; diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java index 58bfc5d..a889c52 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java @@ -1,5 +1,6 @@ package org.sadtech.autoresponder.repository; +import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; import java.util.Collection; @@ -9,20 +10,20 @@ import java.util.Collection; * * @author upagge [07/07/2019] */ -public interface UnitPointerRepository { +public interface UnitPointerRepository { - void add(UnitPointer unitPointer); + void add(UnitPointer unitPointer); - void edit(UnitPointer unitPointer); + void edit(UnitPointer unitPointer); void remove(Integer entityId); - void addAll(Collection unitPointers); + void addAll(Collection> unitPointers); /** * @param entityId Идентификатор пользователя * @return Объект с последним обработанным {@link org.sadtech.autoresponder.entity.Unit} для пользователя */ - UnitPointer findByEntityId(Integer entityId); + UnitPointer findByEntityId(Integer entityId); } diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java index 8b4e1a0..1c22747 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java @@ -1,6 +1,7 @@ package org.sadtech.autoresponder.repository; import lombok.NonNull; +import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; import java.util.Collection; @@ -12,17 +13,17 @@ import java.util.Map; * * @author upagge [07/07/2019] */ -public class UnitPointerRepositoryMap implements UnitPointerRepository { +public class UnitPointerRepositoryMap implements UnitPointerRepository { - private Map unitPointerMap = new HashMap<>(); + private Map> unitPointerMap = new HashMap<>(); @Override - public void add(@NonNull UnitPointer unitPointer) { + public void add(@NonNull UnitPointer unitPointer) { unitPointerMap.put(unitPointer.getEntityId(), unitPointer); } @Override - public void edit(@NonNull UnitPointer unitPointer) { + public void edit(@NonNull UnitPointer unitPointer) { unitPointerMap.get(unitPointer.getEntityId()).setUnit(unitPointer.getUnit()); } @@ -32,12 +33,12 @@ public class UnitPointerRepositoryMap implements UnitPointerRepository { } @Override - public void addAll(@NonNull Collection unitPointers) { + public void addAll(@NonNull Collection> unitPointers) { unitPointers.parallelStream().forEach(unitPointer -> unitPointerMap.put(unitPointer.getEntityId(), unitPointer)); } @Override - public UnitPointer findByEntityId(@NonNull Integer entityId) { + public UnitPointer findByEntityId(@NonNull Integer entityId) { return unitPointerMap.get(entityId); } } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java index 381167d..f134f54 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java @@ -8,19 +8,20 @@ import org.sadtech.autoresponder.entity.UnitPointer; * * @author upagge [07/07/2019] */ -public interface UnitPointerService { +public interface UnitPointerService { - void add(UnitPointer unitPointer); + void add(UnitPointer unitPointer); /** * Проверка наличия {@link UnitPointer} для пользователя + * * @param entityId Идентификатор пользователя * @return true - если найдено */ boolean check(Integer entityId); - UnitPointer getByEntityId(Integer entityId); + UnitPointer getByEntityId(Integer entityId); - void edit(Integer personId, Unit unit); + void edit(Integer personId, U unit); } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java index 27cd202..49e7150 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java @@ -7,28 +7,28 @@ import org.sadtech.autoresponder.entity.UnitPointer; import org.sadtech.autoresponder.repository.UnitPointerRepository; @Slf4j -public class UnitPointerServiceImpl implements UnitPointerService { +public class UnitPointerServiceImpl implements UnitPointerService { - private final UnitPointerRepository unitPointerRepository; + private final UnitPointerRepository unitPointerRepository; - public UnitPointerServiceImpl(UnitPointerRepository unitPointerRepository) { + public UnitPointerServiceImpl(UnitPointerRepository unitPointerRepository) { this.unitPointerRepository = unitPointerRepository; } @Override - public UnitPointer getByEntityId(@NonNull Integer entityId) { + public UnitPointer getByEntityId(@NonNull Integer entityId) { return unitPointerRepository.findByEntityId(entityId); } @Override - public void edit(@NonNull Integer personId, Unit unit) { + public void edit(@NonNull Integer personId, U unit) { if (check(personId)) { - unitPointerRepository.edit(new UnitPointer(personId, unit)); + unitPointerRepository.edit(new UnitPointer<>(personId, unit)); } } @Override - public void add(@NonNull UnitPointer unitPointer) { + public void add(@NonNull UnitPointer unitPointer) { unitPointerRepository.add(unitPointer); log.info("Пользователь отправлен в репозиторий"); }