From f5bf79f4415726278b14ab3d9dad06c53a45744b Mon Sep 17 00:00:00 2001 From: upagge Date: Wed, 29 Apr 2020 17:20:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../sadtech/autoresponder/AutoResponder.java | 35 ++++++++----------- .../autoresponder/entity/UnitPointer.java | 4 +-- .../repository/UnitPointerRepository.java | 15 +++----- .../repository/UnitPointerRepositoryMap.java | 30 ++++++---------- .../service/UnitPointerService.java | 10 +++--- .../service/UnitPointerServiceImpl.java | 20 +++++------ .../autoresponder/AutoResponderTest.java | 2 +- 8 files changed, 48 insertions(+), 70 deletions(-) diff --git a/pom.xml b/pom.xml index 216dc9b..ade279e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder autoresponder - 1.8-RELEASE + 1.9-SNAPSHOT jar Abstract Autoresponder diff --git a/src/main/java/org/sadtech/autoresponder/AutoResponder.java b/src/main/java/org/sadtech/autoresponder/AutoResponder.java index 34a7685..7292c0c 100644 --- a/src/main/java/org/sadtech/autoresponder/AutoResponder.java +++ b/src/main/java/org/sadtech/autoresponder/AutoResponder.java @@ -35,9 +35,9 @@ public class AutoResponder { 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; } @@ -49,25 +49,20 @@ public class AutoResponder { * @param message Запрос пользователя - текстовое сообщение * @return {@link Unit}, который отвечает за данные для обработки данного запроса */ - public Optional answer(@NonNull Long entityId, String message) { - Optional unitPointer = unitPointerService.getByEntityId(entityId); - Optional unitOpt = nextUnit( - !unitPointer.isPresent() || newScenario(unitPointer.get()) ? startUnits : unitPointer.get().getUnit().getNextUnits(), - message + public Optional answer(@NonNull Long entityId, @NonNull String message) { + Optional> unitPointer = unitPointerService.getByEntityId(entityId); + final Optional answer = nextUnit( + unitPointer.isPresent() ? unitPointer.get().getUnit().getNextUnits() : startUnits, message ); - if (unitOpt.isPresent()) { - U unit = unitOpt.get(); - if (unitPointer.isPresent()) { - unitPointerService.edit(entityId, unit); + if (answer.isPresent()) { + final U unitAnswer = answer.get(); + if (unitAnswer.getNextUnits().isEmpty()) { + unitPointerService.removeByEntityId(entityId); } else { - unitPointerService.add(new UnitPointer(entityId, unit)); + unitPointerService.save(new UnitPointer<>(entityId, unitAnswer)); } } - return unitOpt.isPresent() ? unitOpt : Optional.ofNullable(defaultUnit); - } - - private boolean newScenario(UnitPointer unitPointer) { - return unitPointer.getUnit() == null || unitPointer.getUnit().getNextUnits() == null || unitPointer.getUnit().getNextUnits().isEmpty(); + return answer; } /** @@ -77,7 +72,7 @@ public class AutoResponder { * @param message Запрос пользователя - текстовое сообщение * @return Юнит, который нуждается в обработке в соответствии с запросом пользователя */ - private Optional nextUnit(@NonNull Set nextUnits, String message) { + private Optional nextUnit(@NonNull Set nextUnits, @NonNull String message) { Set searchUnit = new HashSet<>(); for (U unit : nextUnits) { @@ -107,10 +102,10 @@ public class AutoResponder { } } } - return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR); + final Optional max = searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR); + return max.isPresent() ? max : Optional.ofNullable(defaultUnit); } - private boolean patternReg(@NonNull U unit, String message) { return message.matches(unit.getPattern().pattern()); } diff --git a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java index d1f6041..20f39e3 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java +++ b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java @@ -13,12 +13,12 @@ import org.sadtech.autoresponder.util.Description; @Data @RequiredArgsConstructor @AllArgsConstructor -public class UnitPointer { +public class UnitPointer { @Description("Идентификатор пользователя") private final Long entityId; @Description("Юнит, который был обработан") - private Unit unit; + private U unit; } diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java index 68a31d4..bf4141f 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java @@ -4,7 +4,6 @@ import lombok.NonNull; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; -import java.util.Collection; import java.util.Optional; /** @@ -12,20 +11,16 @@ import java.util.Optional; * * @author upagge [07/07/2019] */ -public interface UnitPointerRepository { +public interface UnitPointerRepository { - UnitPointer add(@NonNull UnitPointer unitPointer); - - void edit(@NonNull UnitPointer unitPointer); - - void remove(@NonNull Integer entityId); - - void addAll(@NonNull Collection unitPointers); + UnitPointer save(@NonNull UnitPointer unitPointer); /** * @param entityId Идентификатор пользователя * @return Объект с последним обработанным {@link Unit} для пользователя */ - Optional findByEntityId(@NonNull Long entityId); + Optional> findByEntityId(@NonNull Long entityId); + + void removeByEntityId(@NonNull Long entityId); } diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java index 1d61a81..021cdfd 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java @@ -1,9 +1,9 @@ package org.sadtech.autoresponder.repository; import lombok.NonNull; +import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; -import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -13,34 +13,24 @@ import java.util.Optional; * * @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 UnitPointer add(@NonNull UnitPointer unitPointer) { + public UnitPointer save(@NonNull UnitPointer unitPointer) { unitPointerMap.put(unitPointer.getEntityId(), unitPointer); return unitPointer; } @Override - public void edit(@NonNull UnitPointer unitPointer) { - unitPointerMap.get(unitPointer.getEntityId()).setUnit(unitPointer.getUnit()); - } - - @Override - public void remove(@NonNull Integer entityId) { - unitPointerMap.remove(entityId); - } - - @Override - public void addAll(@NonNull Collection unitPointers) { - unitPointers.parallelStream().forEach(unitPointer -> unitPointerMap.put(unitPointer.getEntityId(), unitPointer)); - } - - @Override - public Optional findByEntityId(@NonNull Long entityId) { + public Optional> findByEntityId(@NonNull Long entityId) { return Optional.ofNullable(unitPointerMap.get(entityId)); } + @Override + public void removeByEntityId(@NonNull Long entityId) { + unitPointerMap.remove(entityId); + } + } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java index a5f0fe2..3c114fd 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java @@ -11,9 +11,9 @@ import java.util.Optional; * * @author upagge [07/07/2019] */ -public interface UnitPointerService { +public interface UnitPointerService { - void add(@NonNull UnitPointer unitPointer); + void save(@NonNull UnitPointer unitPointer); /** * Проверка наличия {@link UnitPointer} для пользователя @@ -21,10 +21,10 @@ public interface UnitPointerService { * @param entityId Идентификатор пользователя * @return true - если найдено */ - boolean check(@NonNull Long entityId); + boolean existsByEntityId(@NonNull Long entityId); - Optional getByEntityId(@NonNull Long entityId); + Optional> getByEntityId(@NonNull Long entityId); - void edit(@NonNull Long entityId, Unit unit); + void removeByEntityId(@NonNull Long entityId); } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java index 30f7fad..b42c053 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java @@ -11,30 +11,28 @@ import java.util.Optional; @Slf4j @RequiredArgsConstructor -public class UnitPointerServiceImpl implements UnitPointerService { +public class UnitPointerServiceImpl implements UnitPointerService { - private final UnitPointerRepository unitPointerRepository; + private final UnitPointerRepository unitPointerRepository; @Override - public Optional getByEntityId(@NonNull Long entityId) { + public Optional> getByEntityId(@NonNull Long entityId) { return unitPointerRepository.findByEntityId(entityId); } @Override - public void edit(@NonNull Long entityId, Unit unit) { - if (check(entityId)) { - unitPointerRepository.edit(new UnitPointer(entityId, unit)); - } + public void removeByEntityId(@NonNull Long entityId) { + unitPointerRepository.removeByEntityId(entityId); } @Override - public void add(@NonNull UnitPointer unitPointer) { - unitPointerRepository.add(unitPointer); - log.info("Пользователь отправлен в репозиторий"); + public void save(@NonNull UnitPointer unitPointer) { + unitPointerRepository.save(unitPointer); + log.trace("Пользователь отправлен в репозиторий"); } @Override - public boolean check(@NonNull Long entityId) { + public boolean existsByEntityId(@NonNull Long entityId) { return unitPointerRepository.findByEntityId(entityId).isPresent(); } diff --git a/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java index 076a90a..67ad123 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java @@ -64,7 +64,7 @@ public class AutoResponderTest { testUnits.add(regExp); testUnits.add(unreal); - UnitPointerServiceImpl unitPointerService = new UnitPointerServiceImpl(new UnitPointerRepositoryMap()); + UnitPointerServiceImpl unitPointerService = new UnitPointerServiceImpl<>(new UnitPointerRepositoryMap<>()); autoresponder = new AutoResponder<>(unitPointerService, testUnits); }