Небольшие исправления
This commit is contained in:
parent
d3c29c4275
commit
f5bf79f441
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>org.sadtech.autoresponder</groupId>
|
||||
<artifactId>autoresponder</artifactId>
|
||||
<version>1.8-RELEASE</version>
|
||||
<version>1.9-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Abstract Autoresponder</name>
|
||||
|
@ -35,9 +35,9 @@ public class AutoResponder<U extends Unit> {
|
||||
private U defaultUnit;
|
||||
|
||||
@Getter
|
||||
private final UnitPointerService unitPointerService;
|
||||
private final UnitPointerService<U> unitPointerService;
|
||||
|
||||
public AutoResponder(UnitPointerService unitPointerService, Set<U> startUnits) {
|
||||
public AutoResponder(UnitPointerService<U> unitPointerService, Set<U> startUnits) {
|
||||
this.startUnits = startUnits;
|
||||
this.unitPointerService = unitPointerService;
|
||||
}
|
||||
@ -49,25 +49,20 @@ public class AutoResponder<U extends Unit> {
|
||||
* @param message Запрос пользователя - текстовое сообщение
|
||||
* @return {@link Unit}, который отвечает за данные для обработки данного запроса
|
||||
*/
|
||||
public Optional<U> answer(@NonNull Long entityId, String message) {
|
||||
Optional<UnitPointer> unitPointer = unitPointerService.getByEntityId(entityId);
|
||||
Optional<U> unitOpt = nextUnit(
|
||||
!unitPointer.isPresent() || newScenario(unitPointer.get()) ? startUnits : unitPointer.get().getUnit().getNextUnits(),
|
||||
message
|
||||
public Optional<U> answer(@NonNull Long entityId, @NonNull String message) {
|
||||
Optional<UnitPointer<U>> unitPointer = unitPointerService.getByEntityId(entityId);
|
||||
final Optional<U> 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<U extends Unit> {
|
||||
* @param message Запрос пользователя - текстовое сообщение
|
||||
* @return Юнит, который нуждается в обработке в соответствии с запросом пользователя
|
||||
*/
|
||||
private Optional<U> nextUnit(@NonNull Set<U> nextUnits, String message) {
|
||||
private Optional<U> nextUnit(@NonNull Set<U> nextUnits, @NonNull String message) {
|
||||
Set<U> searchUnit = new HashSet<>();
|
||||
|
||||
for (U unit : nextUnits) {
|
||||
@ -107,10 +102,10 @@ public class AutoResponder<U extends Unit> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR);
|
||||
final Optional<U> 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());
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ import org.sadtech.autoresponder.util.Description;
|
||||
@Data
|
||||
@RequiredArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UnitPointer {
|
||||
public class UnitPointer<U extends Unit> {
|
||||
|
||||
@Description("Идентификатор пользователя")
|
||||
private final Long entityId;
|
||||
|
||||
@Description("Юнит, который был обработан")
|
||||
private Unit unit;
|
||||
private U unit;
|
||||
|
||||
}
|
||||
|
@ -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<U extends Unit> {
|
||||
|
||||
UnitPointer add(@NonNull UnitPointer unitPointer);
|
||||
|
||||
void edit(@NonNull UnitPointer unitPointer);
|
||||
|
||||
void remove(@NonNull Integer entityId);
|
||||
|
||||
void addAll(@NonNull Collection<UnitPointer> unitPointers);
|
||||
UnitPointer<U> save(@NonNull UnitPointer<U> unitPointer);
|
||||
|
||||
/**
|
||||
* @param entityId Идентификатор пользователя
|
||||
* @return Объект с последним обработанным {@link Unit} для пользователя
|
||||
*/
|
||||
Optional<UnitPointer> findByEntityId(@NonNull Long entityId);
|
||||
Optional<UnitPointer<U>> findByEntityId(@NonNull Long entityId);
|
||||
|
||||
void removeByEntityId(@NonNull Long entityId);
|
||||
|
||||
}
|
||||
|
@ -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<U extends Unit> implements UnitPointerRepository<U> {
|
||||
|
||||
private Map<Long, UnitPointer> unitPointerMap = new HashMap<>();
|
||||
private Map<Long, UnitPointer<U>> unitPointerMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public UnitPointer add(@NonNull UnitPointer unitPointer) {
|
||||
public UnitPointer<U> save(@NonNull UnitPointer<U> 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<UnitPointer> unitPointers) {
|
||||
unitPointers.parallelStream().forEach(unitPointer -> unitPointerMap.put(unitPointer.getEntityId(), unitPointer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<UnitPointer> findByEntityId(@NonNull Long entityId) {
|
||||
public Optional<UnitPointer<U>> findByEntityId(@NonNull Long entityId) {
|
||||
return Optional.ofNullable(unitPointerMap.get(entityId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByEntityId(@NonNull Long entityId) {
|
||||
unitPointerMap.remove(entityId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import java.util.Optional;
|
||||
*
|
||||
* @author upagge [07/07/2019]
|
||||
*/
|
||||
public interface UnitPointerService {
|
||||
public interface UnitPointerService<U extends Unit> {
|
||||
|
||||
void add(@NonNull UnitPointer unitPointer);
|
||||
void save(@NonNull UnitPointer<U> 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<UnitPointer> getByEntityId(@NonNull Long entityId);
|
||||
Optional<UnitPointer<U>> getByEntityId(@NonNull Long entityId);
|
||||
|
||||
void edit(@NonNull Long entityId, Unit unit);
|
||||
void removeByEntityId(@NonNull Long entityId);
|
||||
|
||||
}
|
||||
|
@ -11,30 +11,28 @@ import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class UnitPointerServiceImpl implements UnitPointerService {
|
||||
public class UnitPointerServiceImpl<U extends Unit> implements UnitPointerService<U> {
|
||||
|
||||
private final UnitPointerRepository unitPointerRepository;
|
||||
private final UnitPointerRepository<U> unitPointerRepository;
|
||||
|
||||
@Override
|
||||
public Optional<UnitPointer> getByEntityId(@NonNull Long entityId) {
|
||||
public Optional<UnitPointer<U>> 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<U> unitPointer) {
|
||||
unitPointerRepository.save(unitPointer);
|
||||
log.trace("Пользователь отправлен в репозиторий");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(@NonNull Long entityId) {
|
||||
public boolean existsByEntityId(@NonNull Long entityId) {
|
||||
return unitPointerRepository.findByEntityId(entityId).isPresent();
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ public class AutoResponderTest {
|
||||
testUnits.add(regExp);
|
||||
testUnits.add(unreal);
|
||||
|
||||
UnitPointerServiceImpl unitPointerService = new UnitPointerServiceImpl(new UnitPointerRepositoryMap());
|
||||
UnitPointerServiceImpl<TestUnit> unitPointerService = new UnitPointerServiceImpl<>(new UnitPointerRepositoryMap<>());
|
||||
autoresponder = new AutoResponder<>(unitPointerService, testUnits);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user