From 920771366522c4150ec41a7d1dcd68c52db8106c Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 1 Aug 2019 23:41:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=B1=D0=B0=D0=B3=D0=B8=20=D1=83=20=D1=81=D0=B0=D0=B1?= =?UTF-8?q?=D1=81=D0=BA=D1=80=D0=B0=D0=B9=D0=B1=D0=B5=D1=80=D0=BE=D0=B2,?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B8=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B5?= =?UTF-8?q?=D0=B2=D0=B5=D0=BD=D1=82=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/distribution/EventDistributor.java | 23 +++++----------- .../AbstractBasketSubscribe.java | 4 +-- .../{ => subscriber}/AccountSubscribe.java | 4 +-- .../BoardCommentSubscribe.java | 4 +-- .../{ => subscriber}/MailSubscriber.java | 4 +-- .../core/repository/RawEventRepository.java | 2 +- .../impl/RawEventRepositoryQueue.java | 26 ------------------- .../impl/RawEventRepositorySet.java | 26 +++++++++++++++++++ .../impl/jpa/RawEventRepositoryJpaImpl.java | 2 +- .../vkbot/core/service/RawEventService.java | 2 +- .../service/impl/RawEventServiceImpl.java | 4 +-- 11 files changed, 46 insertions(+), 55 deletions(-) rename src/main/java/org/sadtech/vkbot/core/distribution/{ => subscriber}/AbstractBasketSubscribe.java (94%) rename src/main/java/org/sadtech/vkbot/core/distribution/{ => subscriber}/AccountSubscribe.java (95%) rename src/main/java/org/sadtech/vkbot/core/distribution/{ => subscriber}/BoardCommentSubscribe.java (96%) rename src/main/java/org/sadtech/vkbot/core/distribution/{ => subscriber}/MailSubscriber.java (91%) delete mode 100644 src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositoryQueue.java create mode 100644 src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java index 1d856b5..44c4343 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java @@ -1,41 +1,32 @@ package org.sadtech.vkbot.core.distribution; import com.google.gson.JsonObject; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.sadtech.vkbot.core.distribution.subscriber.AbstractBasketSubscribe; import org.sadtech.vkbot.core.service.RawEventService; import java.util.Optional; import java.util.Set; -import java.util.concurrent.atomic.AtomicBoolean; @Slf4j +@RequiredArgsConstructor public class EventDistributor implements Runnable { private final RawEventService rawEventService; - private final Set basketSubscribes; - - public EventDistributor(RawEventService rawEventService, Set basketSubscribes) { - this.rawEventService = rawEventService; - this.basketSubscribes = basketSubscribes; - log.info("EventDistributor инициализирован"); - } + private final Set> basketSubscribes; @Override public void run() { while (true) { - Optional.ofNullable(rawEventService.getJsonObjects()) + Optional.ofNullable(rawEventService.getNewEvent()) .ifPresent(events -> events.forEach(this::goNextSubscribe)); } } - private boolean goNextSubscribe(JsonObject object) { - AtomicBoolean flag = new AtomicBoolean(false); + private void goNextSubscribe(JsonObject object) { basketSubscribes.stream() .filter(basketSubscribe -> basketSubscribe.check(object)) - .forEach(basketSubscribe -> { - basketSubscribe.update(object); - flag.set(true); - }); - return flag.get(); + .forEach(basketSubscribe -> basketSubscribe.update(object)); } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AbstractBasketSubscribe.java similarity index 94% rename from src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java rename to src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AbstractBasketSubscribe.java index 32fb57f..192c882 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AbstractBasketSubscribe.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.distribution; +package org.sadtech.vkbot.core.distribution.subscriber; import org.sadtech.vkbot.core.convert.Convert; @@ -15,7 +15,7 @@ public abstract class AbstractBasketSubscribe { convert = (object) -> (C) object; } - protected abstract boolean check(S object); + public abstract boolean check(S object); public void update(S object) { C newObject = convert.converting(object); diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java similarity index 95% rename from src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java rename to src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java index e2c9532..de6b979 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.distribution; +package org.sadtech.vkbot.core.distribution.subscriber; import com.vk.api.sdk.objects.messages.Message; import com.vk.api.sdk.objects.messages.MessageAttachmentType; @@ -23,7 +23,7 @@ public class AccountSubscribe extends AbstractBasketSubscribe } @Override - protected boolean check(Message userMessage) { + public boolean check(Message userMessage) { return userMessage.getAttachments().size() > 0 && MessageAttachmentType.LINK.equals(userMessage.getAttachments().get(0).getType()) && "Payment awaiting acceptance".equals(userMessage.getAttachments().get(0).getLink().getCaption()); diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/BoardCommentSubscribe.java similarity index 96% rename from src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java rename to src/main/java/org/sadtech/vkbot/core/distribution/subscriber/BoardCommentSubscribe.java index a2865ec..e358df6 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/BoardCommentSubscribe.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.distribution; +package org.sadtech.vkbot.core.distribution.subscriber; import com.google.gson.Gson; import com.google.gson.JsonObject; @@ -53,7 +53,7 @@ public class BoardCommentSubscribe extends AbstractBasketSubscribe } @Override - protected boolean check(JsonObject object) { + public boolean check(JsonObject object) { String type = object.get("type").getAsString(); return "message_new".equals(type); } diff --git a/src/main/java/org/sadtech/vkbot/core/repository/RawEventRepository.java b/src/main/java/org/sadtech/vkbot/core/repository/RawEventRepository.java index 94b7885..7c5b316 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/RawEventRepository.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/RawEventRepository.java @@ -15,6 +15,6 @@ public interface RawEventRepository { void cleanAll(); - Set getEventQueue(); + Set findNewEvent(); } diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositoryQueue.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositoryQueue.java deleted file mode 100644 index 8b3fa1d..0000000 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositoryQueue.java +++ /dev/null @@ -1,26 +0,0 @@ -//package org.sadtech.vkbot.core.repository.impl; -// -//import com.google.gson.JsonObject; -//import org.sadtech.vkbot.core.repository.RawEventRepository; -// -//import java.util.Queue; -//import java.util.concurrent.ConcurrentLinkedQueue; -// -//public class RawEventRepositoryQueue implements RawEventRepository { -// -// private final Queue jsonObjects = new ConcurrentLinkedQueue<>(); -// -// @Override -// public void add(JsonObject jsonObject) { -// jsonObjects.offer(jsonObject); -// } -// -// @Override -// public void cleanAll() { -// jsonObjects.clear(); -// } -// -// public Set getEventQueue() { -// return jsonObjects; -// } -//} diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java new file mode 100644 index 0000000..41b0f6b --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java @@ -0,0 +1,26 @@ +package org.sadtech.vkbot.core.repository.impl; + +import com.google.gson.JsonObject; +import org.sadtech.vkbot.core.repository.RawEventRepository; + +import java.util.HashSet; +import java.util.Set; + +public class RawEventRepositorySet implements RawEventRepository { + + private final Set jsonObjects = new HashSet<>(); + + @Override + public void add(JsonObject jsonObject) { + jsonObjects.add(jsonObject); + } + + @Override + public void cleanAll() { + jsonObjects.clear(); + } + + public Set findNewEvent() { + return jsonObjects; + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/jpa/RawEventRepositoryJpaImpl.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/jpa/RawEventRepositoryJpaImpl.java index e7550e8..cea6a23 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/jpa/RawEventRepositoryJpaImpl.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/impl/jpa/RawEventRepositoryJpaImpl.java @@ -38,7 +38,7 @@ public class RawEventRepositoryJpaImpl implements RawEventRepository { } @Override - public Set getEventQueue() { + public Set findNewEvent() { if (addFlag) { List allEvent = rawEventRepositoryJpa.findAll(); rawEventRepositoryJpa.deleteAll(allEvent); diff --git a/src/main/java/org/sadtech/vkbot/core/service/RawEventService.java b/src/main/java/org/sadtech/vkbot/core/service/RawEventService.java index 28a6aa9..27d8279 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/RawEventService.java +++ b/src/main/java/org/sadtech/vkbot/core/service/RawEventService.java @@ -15,6 +15,6 @@ public interface RawEventService { void add(JsonObject jsonObject); - Set getJsonObjects(); + Set getNewEvent(); } \ No newline at end of file diff --git a/src/main/java/org/sadtech/vkbot/core/service/impl/RawEventServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/impl/RawEventServiceImpl.java index 045c812..c4b1ca4 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/impl/RawEventServiceImpl.java +++ b/src/main/java/org/sadtech/vkbot/core/service/impl/RawEventServiceImpl.java @@ -27,8 +27,8 @@ public class RawEventServiceImpl implements RawEventService { } @Override - public Set getJsonObjects() { - return rawEventRepository.getEventQueue(); + public Set getNewEvent() { + return rawEventRepository.findNewEvent(); } }