Дженерики
This commit is contained in:
parent
7bc6923ed4
commit
32ec215d81
@ -22,23 +22,23 @@ import java.util.Set;
|
||||
* @author upagge [07/07/2019]
|
||||
*/
|
||||
@Slf4j
|
||||
public class AutoResponder {
|
||||
public class AutoResponder<U extends Unit> {
|
||||
|
||||
@Description("Компоратор для сортировки Unit-ов")
|
||||
private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator();
|
||||
private final UnitPriorityComparator<U> unitPriorityComparator = new UnitPriorityComparator<>();
|
||||
|
||||
@Description("Начальные юниты, первый запрос приходит на них")
|
||||
private final Set<Unit> startUnits;
|
||||
private final Set<U> startUnits;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Description("Дефолтный юнит, отправляется если ни один Unit не подошел")
|
||||
private Unit defaultUnit;
|
||||
private U defaultUnit;
|
||||
|
||||
@Getter
|
||||
private final UnitPointerService unitPointerService;
|
||||
private final UnitPointerService<U> unitPointerService;
|
||||
|
||||
public AutoResponder(UnitPointerService unitPointerService, Set<Unit> startUnits) {
|
||||
public AutoResponder(UnitPointerService<U> unitPointerService, Set<U> 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<U> 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<Unit> nextUnits, String message) {
|
||||
Set<Unit> searchUnit = new HashSet<>();
|
||||
private U nextUnit(@NonNull Set<U> nextUnits, String message) {
|
||||
Set<U> 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<U> checkAndAddPerson(@NonNull Integer personId) {
|
||||
UnitPointer<U> unitPointer;
|
||||
if (unitPointerService.check(personId)) {
|
||||
unitPointer = unitPointerService.getByEntityId(personId);
|
||||
} else {
|
||||
unitPointer = new UnitPointer(personId);
|
||||
unitPointer = new UnitPointer<>(personId);
|
||||
unitPointerService.add(unitPointer);
|
||||
}
|
||||
return unitPointer;
|
||||
|
@ -9,7 +9,7 @@ import java.util.Comparator;
|
||||
*
|
||||
* @author upagge [07/07/2019]
|
||||
*/
|
||||
public class UnitPriorityComparator implements Comparator<Unit> {
|
||||
public class UnitPriorityComparator<U extends Unit> implements Comparator<U> {
|
||||
|
||||
@Override
|
||||
public int compare(Unit o1, Unit o2) {
|
||||
|
@ -19,7 +19,7 @@ import java.util.regex.Pattern;
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
@Setter
|
||||
public abstract class Unit {
|
||||
public abstract class Unit<U extends Unit> {
|
||||
|
||||
@Description("Ключевые слова")
|
||||
protected Set<String> keyWords;
|
||||
@ -37,9 +37,9 @@ public abstract class Unit {
|
||||
protected Integer priority;
|
||||
|
||||
@Description("Множество следующих Unit в сценарии")
|
||||
protected Set<Unit> nextUnits;
|
||||
protected Set<U> nextUnits;
|
||||
|
||||
protected Unit(Set<String> keyWords, String phrase, Pattern pattern, Integer matchThreshold, Integer priority, Set<Unit> nextUnits) {
|
||||
protected Unit(Set<String> keyWords, String phrase, Pattern pattern, Integer matchThreshold, Integer priority, Set<U> nextUnits) {
|
||||
this.keyWords = keyWords;
|
||||
this.phrase = phrase;
|
||||
this.pattern = pattern;
|
||||
|
@ -11,13 +11,13 @@ import org.sadtech.autoresponder.util.Description;
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class UnitPointer {
|
||||
public class UnitPointer<U extends Unit> {
|
||||
|
||||
@Description("Идентификатор пользователя")
|
||||
private Integer entityId;
|
||||
|
||||
@Description("Юнит, который был обработан")
|
||||
private Unit unit;
|
||||
private U unit;
|
||||
|
||||
public UnitPointer(Integer entityId) {
|
||||
this.entityId = entityId;
|
||||
|
@ -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<U extends Unit> {
|
||||
|
||||
void add(UnitPointer unitPointer);
|
||||
void add(UnitPointer<U> unitPointer);
|
||||
|
||||
void edit(UnitPointer unitPointer);
|
||||
void edit(UnitPointer<U> unitPointer);
|
||||
|
||||
void remove(Integer entityId);
|
||||
|
||||
void addAll(Collection<UnitPointer> unitPointers);
|
||||
void addAll(Collection<UnitPointer<U>> unitPointers);
|
||||
|
||||
/**
|
||||
* @param entityId Идентификатор пользователя
|
||||
* @return Объект с последним обработанным {@link org.sadtech.autoresponder.entity.Unit} для пользователя
|
||||
*/
|
||||
UnitPointer findByEntityId(Integer entityId);
|
||||
UnitPointer<U> findByEntityId(Integer entityId);
|
||||
|
||||
}
|
||||
|
@ -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<U extends Unit> implements UnitPointerRepository<U> {
|
||||
|
||||
private Map<Integer, UnitPointer> unitPointerMap = new HashMap<>();
|
||||
private Map<Integer, UnitPointer<U>> unitPointerMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void add(@NonNull UnitPointer unitPointer) {
|
||||
public void add(@NonNull UnitPointer<U> unitPointer) {
|
||||
unitPointerMap.put(unitPointer.getEntityId(), unitPointer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void edit(@NonNull UnitPointer unitPointer) {
|
||||
public void edit(@NonNull UnitPointer<U> unitPointer) {
|
||||
unitPointerMap.get(unitPointer.getEntityId()).setUnit(unitPointer.getUnit());
|
||||
}
|
||||
|
||||
@ -32,12 +33,12 @@ public class UnitPointerRepositoryMap implements UnitPointerRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAll(@NonNull Collection<UnitPointer> unitPointers) {
|
||||
public void addAll(@NonNull Collection<UnitPointer<U>> unitPointers) {
|
||||
unitPointers.parallelStream().forEach(unitPointer -> unitPointerMap.put(unitPointer.getEntityId(), unitPointer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public UnitPointer findByEntityId(@NonNull Integer entityId) {
|
||||
public UnitPointer<U> findByEntityId(@NonNull Integer entityId) {
|
||||
return unitPointerMap.get(entityId);
|
||||
}
|
||||
}
|
||||
|
@ -8,19 +8,20 @@ import org.sadtech.autoresponder.entity.UnitPointer;
|
||||
*
|
||||
* @author upagge [07/07/2019]
|
||||
*/
|
||||
public interface UnitPointerService {
|
||||
public interface UnitPointerService<U extends Unit> {
|
||||
|
||||
void add(UnitPointer unitPointer);
|
||||
void add(UnitPointer<U> unitPointer);
|
||||
|
||||
/**
|
||||
* Проверка наличия {@link UnitPointer} для пользователя
|
||||
*
|
||||
* @param entityId Идентификатор пользователя
|
||||
* @return true - если найдено
|
||||
*/
|
||||
boolean check(Integer entityId);
|
||||
|
||||
UnitPointer getByEntityId(Integer entityId);
|
||||
UnitPointer<U> getByEntityId(Integer entityId);
|
||||
|
||||
void edit(Integer personId, Unit unit);
|
||||
void edit(Integer personId, U unit);
|
||||
|
||||
}
|
||||
|
@ -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<U extends Unit> implements UnitPointerService<U> {
|
||||
|
||||
private final UnitPointerRepository unitPointerRepository;
|
||||
private final UnitPointerRepository<U> unitPointerRepository;
|
||||
|
||||
public UnitPointerServiceImpl(UnitPointerRepository unitPointerRepository) {
|
||||
public UnitPointerServiceImpl(UnitPointerRepository<U> unitPointerRepository) {
|
||||
this.unitPointerRepository = unitPointerRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UnitPointer getByEntityId(@NonNull Integer entityId) {
|
||||
public UnitPointer<U> 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<U> unitPointer) {
|
||||
unitPointerRepository.add(unitPointer);
|
||||
log.info("Пользователь отправлен в репозиторий");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user