diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java index 30947fc..f136494 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java @@ -2,10 +2,15 @@ package org.sadtech.vkbot.core.distribution; import java.util.Set; -public abstract class AbstractBasketSubscribe implements EventSubscribe { +public abstract class AbstractBasketSubscribe { private Set basketSubscribes; private AbstractBasketSubscribe prioritySubscribe; + protected Convert convert; + + public AbstractBasketSubscribe() { + convert = (object) -> (C) object; + } public Set getBasketSubscribes() { return basketSubscribes; @@ -23,9 +28,17 @@ public abstract class AbstractBasketSubscribe implements EventSubscribe { this.prioritySubscribe = prioritySubscribe; } + public Convert getConvert() { + return convert; + } + + public void setConvert(Convert convert) { + this.convert = convert; + } + protected abstract boolean check(S object); - private boolean goNextSubscribe(S object) { + private boolean goNextSubscribe(C object) { boolean flag = false; if (prioritySubscribe != null && prioritySubscribe.check(object)) { prioritySubscribe.update(object); @@ -34,7 +47,6 @@ public abstract class AbstractBasketSubscribe implements EventSubscribe { for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) { if (basketSubscribe.check(object)) { basketSubscribe.update(object); - } else { flag = true; } } @@ -42,12 +54,12 @@ public abstract class AbstractBasketSubscribe implements EventSubscribe { return flag; } - @Override public void update(S object) { - if (!goNextSubscribe(object)) { - processing(object); + C newObject = convert.converting(object); + if (!goNextSubscribe(newObject)) { + processing(newObject); } } - public abstract void processing(S object); + public abstract void processing(C object); } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java index 790b61f..aba0a81 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -3,7 +3,7 @@ package org.sadtech.vkbot.core.distribution; import com.vk.api.sdk.objects.messages.Message; import org.sadtech.bot.core.service.AccountService; -public class AccountSubscribe extends AbstractBasketSubscribe { +public class AccountSubscribe extends AbstractBasketSubscribe { private final AccountService accountService; diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java b/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java new file mode 100644 index 0000000..6f2e201 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java @@ -0,0 +1,8 @@ +package org.sadtech.vkbot.core.distribution; + +@FunctionalInterface +public interface Convert { + + C converting(T target); + +} 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 5fbc61b..f5e250f 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java @@ -4,7 +4,7 @@ import com.google.gson.JsonObject; import org.apache.log4j.Logger; import org.sadtech.bot.core.service.RawEventService; -public class EventDistributor extends AbstractBasketSubscribe implements Runnable { +public class EventDistributor extends AbstractBasketSubscribe implements Runnable { private static final Logger log = Logger.getLogger(EventDistributor.class); @@ -15,22 +15,16 @@ public class EventDistributor extends AbstractBasketSubscribe implem log.info("EventDistributor инициализирован"); } - public void update() { + @Override + public void run() { while (true) { if (rawEventService.getJsonObjects().peek() != null) { JsonObject event = rawEventService.getJsonObjects().poll(); - log.info("Главный дистрибьютор отправил событие дальше"); super.update(event); } } } - @Override - public void run() { - log.info("EventDistributor запущен"); - update(); - } - @Override protected boolean check(JsonObject object) { return false; diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java deleted file mode 100644 index bd8dead..0000000 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sadtech.vkbot.core.distribution; - -public interface EventSubscribe { - - void update(T object); - -} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java index c582499..267c363 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java @@ -17,7 +17,7 @@ import java.util.ArrayList; import java.util.List; import java.util.TimeZone; -public class MailSubscriber extends AbstractBasketSubscribe { +public class MailSubscriber extends AbstractBasketSubscribe { private static final Logger log = Logger.getLogger(MailSubscriber.class); @@ -25,6 +25,10 @@ public class MailSubscriber extends AbstractBasketSubscribe { public MailSubscriber(MailService mailService) { this.mailService = mailService; + this.convert = (object) -> { + Gson gson = new Gson(); + return gson.fromJson(object.getAsJsonObject("object"), Message.class); + }; } @Override @@ -34,11 +38,8 @@ public class MailSubscriber extends AbstractBasketSubscribe { } @Override - public void processing(JsonObject object) { - Gson gson = new Gson(); - Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); - log.info(userMessage); - mailService.add(createMail(userMessage)); + public void processing(Message object) { + mailService.add(createMail(object)); } private Mail createMail(Message message) {