Замена List на Set

В Unit заменил с List на Set поле хранящее следующие юниты
This commit is contained in:
Mark Struchkov 2019-02-05 21:50:49 +03:00
parent 5a8cdef9d9
commit ec0049d602
8 changed files with 45 additions and 18 deletions

View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.autoresponder</groupId> <groupId>org.sadtech.autoresponder</groupId>
<artifactId>autoresponder</artifactId> <artifactId>autoresponder</artifactId>
<version>1.3.0-RELEASE</version> <version>1.4.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<build> <build>
<plugins> <plugins>

View File

@ -8,10 +8,7 @@ import org.sadtech.autoresponder.service.PersonService;
import org.sadtech.autoresponder.service.UnitService; import org.sadtech.autoresponder.service.UnitService;
import org.sadtech.autoresponder.submodule.parser.Parser; import org.sadtech.autoresponder.submodule.parser.Parser;
import java.util.HashSet; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -20,6 +17,15 @@ public class Autoresponder {
public static final Logger log = Logger.getLogger(Autoresponder.class); public static final Logger log = Logger.getLogger(Autoresponder.class);
private UnitService unitService; private UnitService unitService;
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
private PersonService personService; private PersonService personService;
public Autoresponder(UnitService unitService, PersonService personService) { public Autoresponder(UnitService unitService, PersonService personService) {
@ -56,16 +62,19 @@ public class Autoresponder {
return person; return person;
} }
private Unit nextUnit(List<Unit> nextUnits, String message) { private Unit nextUnit(Set<Unit> nextUnits, String message) {
if (nextUnits.size() > 0) { if (nextUnits.size() > 0) {
UnitPriorityComparator unitPriorityComparator = new UnitPriorityComparator(); UnitPriorityComparator unitPriorityComparator = new UnitPriorityComparator();
Optional<Unit> patternUnits = nextUnits.stream().filter(nextUnit -> nextUnit.getPattern() != null).filter(nextUnit -> patternReg(nextUnit, message)).max(unitPriorityComparator); Optional<Unit> patternUnits = nextUnits.stream().filter(nextUnit -> nextUnit.getPattern() != null).filter(nextUnit -> patternReg(nextUnit, message)).max(unitPriorityComparator);
if (!patternUnits.isPresent()) { if (!patternUnits.isPresent()) {
Parser parser = new Parser(); patternUnits = nextUnits.stream().filter(nextUnit -> (textPercentageMatch(nextUnit, new HashSet<>(Collections.singleton(message))) == 100.0)).max(unitPriorityComparator);
parser.setText(message); if (!patternUnits.isPresent()) {
parser.parse(); Parser parser = new Parser();
patternUnits = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold()).max(unitPriorityComparator); parser.setText(message);
parser.parse();
patternUnits = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold()).max(unitPriorityComparator);
}
} }
if (!patternUnits.isPresent()) { if (!patternUnits.isPresent()) {

View File

@ -6,10 +6,12 @@ public class Person {
private Integer id; private Integer id;
private Unit unit; private Unit unit;
private Boolean replyStatus;
public Person(Integer id, Unit unit) { public Person(Integer id, Unit unit) {
this.id = id; this.id = id;
this.unit = unit; this.unit = unit;
replyStatus = true;
} }
public Person(Integer id) { public Person(Integer id) {
@ -32,6 +34,14 @@ public class Person {
this.unit = unit; this.unit = unit;
} }
public Boolean getReplyStatus() {
return replyStatus;
}
public void setReplyStatus(Boolean replyStatus) {
this.replyStatus = replyStatus;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -9,14 +9,14 @@ public abstract class Unit {
private Pattern pattern; private Pattern pattern;
private Integer matchThreshold; private Integer matchThreshold;
private Integer priority; private Integer priority;
private List<Unit> nextUnits; private Set<Unit> nextUnits;
public Unit() { public Unit() {
priority = 10; priority = 10;
matchThreshold = 10; matchThreshold = 10;
} }
public Unit(Set<String> keyWords, Pattern pattern, Integer matchThreshold, Integer priority, List<Unit> nextUnits) { public Unit(Set<String> keyWords, Pattern pattern, Integer matchThreshold, Integer priority, Set<Unit> nextUnits) {
this.keyWords = keyWords; this.keyWords = keyWords;
this.pattern = pattern; this.pattern = pattern;
this.matchThreshold = matchThreshold; this.matchThreshold = matchThreshold;
@ -33,7 +33,7 @@ public abstract class Unit {
public void setNextUnit(Unit unit) { public void setNextUnit(Unit unit) {
if (nextUnits == null) { if (nextUnits == null) {
nextUnits = new ArrayList<>(); nextUnits = new HashSet<>();
} }
nextUnits.add(unit); nextUnits.add(unit);
} }
@ -62,11 +62,11 @@ public abstract class Unit {
this.priority = priority; this.priority = priority;
} }
public List<Unit> getNextUnits() { public Set<Unit> getNextUnits() {
return nextUnits; return nextUnits;
} }
public void setNextUnits(List<Unit> nextUnits) { public void setNextUnits(Set<Unit> nextUnits) {
this.nextUnits = nextUnits; this.nextUnits = nextUnits;
} }

View File

@ -3,6 +3,7 @@ package org.sadtech.autoresponder.repository;
import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.Unit;
import java.util.List; import java.util.List;
import java.util.Set;
public interface UnitRepository { public interface UnitRepository {
@ -10,6 +11,6 @@ public interface UnitRepository {
void addUnits(List<Unit> units); void addUnits(List<Unit> units);
List<Unit> menuUnits(); Set<Unit> menuUnits();
} }

View File

@ -4,10 +4,11 @@ import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository; import org.sadtech.autoresponder.repository.UnitRepository;
import java.util.List; import java.util.List;
import java.util.Set;
public interface UnitService { public interface UnitService {
List<Unit> menuUnit(); Set<Unit> menuUnit();
void addUnit(Unit unit); void addUnit(Unit unit);

View File

@ -2,6 +2,7 @@ package org.sadtech.autoresponder.service.impl;
import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Person;
import org.sadtech.autoresponder.repository.PersonRepository; import org.sadtech.autoresponder.repository.PersonRepository;
import org.sadtech.autoresponder.repository.impl.PersonRepositoryMap;
import org.sadtech.autoresponder.service.PersonService; import org.sadtech.autoresponder.service.PersonService;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -12,6 +13,10 @@ public class PersonServiceImpl implements PersonService {
private PersonRepository personRepository; private PersonRepository personRepository;
public PersonServiceImpl() {
this.personRepository = new PersonRepositoryMap();
}
public PersonServiceImpl(PersonRepository personRepository) { public PersonServiceImpl(PersonRepository personRepository) {
this.personRepository = personRepository; this.personRepository = personRepository;
} }

View File

@ -7,6 +7,7 @@ import org.sadtech.autoresponder.service.UnitService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
public class UnitServiceImpl implements UnitService { public class UnitServiceImpl implements UnitService {
@ -19,7 +20,7 @@ public class UnitServiceImpl implements UnitService {
} }
@Override @Override
public List<Unit> menuUnit() { public Set<Unit> menuUnit() {
return unitRepository.menuUnits(); return unitRepository.menuUnits();
} }