From a9f329271bd00744198c598ae7277876d5b97e54 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 17 Aug 2019 11:36:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=BE?= =?UTF-8?q?=D0=B9=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D0=BE=D0=B2=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8E=20*=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B8=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D1=84=D0=B5=D0=B9=D1=81=D0=BE=D0=B2=20=D0=B8=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Рефракторинг дженериков --- .../social/core/domain/content/Comment.java | 2 + .../social/core/service/sender/SendBox.java | 26 ------------- .../social/core/service/sender/SendType.java | 13 +++++++ .../sender/{Sent.java => Sending.java} | 8 +++- .../{EmailSent.java => EmailSending.java} | 10 ++++- .../social/core/utils/ExceptionMessages.java | 15 ++++++++ .../social/core/utils/InsertWords.java | 7 +++- .../social/core/utils/MessageUtils.java | 4 +- .../org/sadtech/social/core/utils/Sender.java | 37 +++++++++++++++++++ 9 files changed, 90 insertions(+), 32 deletions(-) delete mode 100644 src/main/java/org/sadtech/social/core/service/sender/SendBox.java create mode 100644 src/main/java/org/sadtech/social/core/service/sender/SendType.java rename src/main/java/org/sadtech/social/core/service/sender/{Sent.java => Sending.java} (66%) rename src/main/java/org/sadtech/social/core/service/sender/email/{EmailSent.java => EmailSending.java} (87%) create mode 100644 src/main/java/org/sadtech/social/core/utils/ExceptionMessages.java create mode 100644 src/main/java/org/sadtech/social/core/utils/Sender.java diff --git a/src/main/java/org/sadtech/social/core/domain/content/Comment.java b/src/main/java/org/sadtech/social/core/domain/content/Comment.java index b75b0be..612fd0f 100644 --- a/src/main/java/org/sadtech/social/core/domain/content/Comment.java +++ b/src/main/java/org/sadtech/social/core/domain/content/Comment.java @@ -1,6 +1,7 @@ package org.sadtech.social.core.domain.content; import lombok.Data; +import lombok.EqualsAndHashCode; import org.sadtech.social.core.utils.Description; /** @@ -8,6 +9,7 @@ import org.sadtech.social.core.utils.Description; * * @author upagge [08/07/2019] */ +@EqualsAndHashCode(callSuper = true) @Data public abstract class Comment extends Message { diff --git a/src/main/java/org/sadtech/social/core/service/sender/SendBox.java b/src/main/java/org/sadtech/social/core/service/sender/SendBox.java deleted file mode 100644 index e3b5945..0000000 --- a/src/main/java/org/sadtech/social/core/service/sender/SendBox.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.sadtech.social.core.service.sender; - -import org.sadtech.social.core.domain.BoxAnswer; -import org.sadtech.social.core.domain.content.Comment; -import org.sadtech.social.core.domain.content.Message; - -public class SendBox { - - private SendBox() { - throw new IllegalStateException("Утилитный класс"); - } - - public static void sent(Message message, BoxAnswer boxAnswer, Sent sent) { - switch (message.getType()) { - case BOARD_COMMENT: - sent.send(((Comment) message).getContentId(), message.getPersonId(), boxAnswer); - break; - case MAIL: - sent.send(message.getPersonId(), boxAnswer); - break; - default: - sent.send(message.getPersonId(), boxAnswer); - } - } - -} diff --git a/src/main/java/org/sadtech/social/core/service/sender/SendType.java b/src/main/java/org/sadtech/social/core/service/sender/SendType.java new file mode 100644 index 0000000..b2c6ceb --- /dev/null +++ b/src/main/java/org/sadtech/social/core/service/sender/SendType.java @@ -0,0 +1,13 @@ +package org.sadtech.social.core.service.sender; + +/** + * Тип объекта отправляющего ответы пользователю. + * + * @author upagge [17/08/2019] + */ +public enum SendType { + + PRIVATE, + PUBLIC + +} diff --git a/src/main/java/org/sadtech/social/core/service/sender/Sent.java b/src/main/java/org/sadtech/social/core/service/sender/Sending.java similarity index 66% rename from src/main/java/org/sadtech/social/core/service/sender/Sent.java rename to src/main/java/org/sadtech/social/core/service/sender/Sending.java index 4eee71f..4759320 100644 --- a/src/main/java/org/sadtech/social/core/service/sender/Sent.java +++ b/src/main/java/org/sadtech/social/core/service/sender/Sending.java @@ -7,7 +7,7 @@ import org.sadtech.social.core.domain.BoxAnswer; * * @author upagge [08/07/2019] */ -public interface Sent { +public interface Sending { /** * Отрпавляет ответ пользователю @@ -19,4 +19,10 @@ public interface Sent { void send(Integer contentId, Integer personId, BoxAnswer boxAnswer); + /** + * Возвращает тип объекта отправляющего ответ пользователя. В зависимости от типа ответ будет отправлен с помощью + * разных методов. + */ + SendType getType(); + } diff --git a/src/main/java/org/sadtech/social/core/service/sender/email/EmailSent.java b/src/main/java/org/sadtech/social/core/service/sender/email/EmailSending.java similarity index 87% rename from src/main/java/org/sadtech/social/core/service/sender/email/EmailSent.java rename to src/main/java/org/sadtech/social/core/service/sender/email/EmailSending.java index a563136..550b6a7 100644 --- a/src/main/java/org/sadtech/social/core/service/sender/email/EmailSent.java +++ b/src/main/java/org/sadtech/social/core/service/sender/email/EmailSending.java @@ -4,7 +4,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.sadtech.social.core.domain.BoxAnswer; import org.sadtech.social.core.exception.MailSendException; -import org.sadtech.social.core.service.sender.Sent; +import org.sadtech.social.core.service.sender.SendType; +import org.sadtech.social.core.service.sender.Sending; import javax.mail.Authenticator; import javax.mail.Message; @@ -17,7 +18,7 @@ import javax.mail.internet.MimeMessage; @Slf4j @RequiredArgsConstructor -public class EmailSent implements Sent { +public class EmailSending implements Sending { private final EmailConfig emailConfig; @@ -46,4 +47,9 @@ public class EmailSent implements Sent { public void send(Integer contentId, Integer personId, BoxAnswer boxAnswer) { throw new MailSendException(); } + + @Override + public SendType getType() { + return SendType.PUBLIC; + } } diff --git a/src/main/java/org/sadtech/social/core/utils/ExceptionMessages.java b/src/main/java/org/sadtech/social/core/utils/ExceptionMessages.java new file mode 100644 index 0000000..c7d0f87 --- /dev/null +++ b/src/main/java/org/sadtech/social/core/utils/ExceptionMessages.java @@ -0,0 +1,15 @@ +package org.sadtech.social.core.utils; + +/** + * Класс утилита, содержащий сообщения об ошибках, и сообщения логирования. + * + * @author upagge [15/08/2019] + */ +public class ExceptionMessages { + + public final static String UTILITY_CLASS = "Класс утилита"; + + private ExceptionMessages() { + throw new IllegalStateException(UTILITY_CLASS); + } +} diff --git a/src/main/java/org/sadtech/social/core/utils/InsertWords.java b/src/main/java/org/sadtech/social/core/utils/InsertWords.java index 29744e0..35c8c87 100644 --- a/src/main/java/org/sadtech/social/core/utils/InsertWords.java +++ b/src/main/java/org/sadtech/social/core/utils/InsertWords.java @@ -4,6 +4,8 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.sadtech.social.core.utils.ExceptionMessages.UTILITY_CLASS; + /** * Класс для вставки слов в текстовую строку вместо подстрок - шаблонов маркеров. * @@ -11,8 +13,10 @@ import java.util.regex.Pattern; */ public class InsertWords { + private static final Pattern pattern = Pattern.compile("\\{(\\d+)}"); + private InsertWords() { - throw new IllegalStateException("Утилитный класс"); + throw new IllegalStateException(UTILITY_CLASS); } /** @@ -23,7 +27,6 @@ public class InsertWords { * @return Модифицированная строка */ public static String insert(String text, List words) { - Pattern pattern = Pattern.compile("\\{(\\d+)}"); Matcher m = pattern.matcher(text); StringBuffer result = new StringBuffer(); while (m.find()) { diff --git a/src/main/java/org/sadtech/social/core/utils/MessageUtils.java b/src/main/java/org/sadtech/social/core/utils/MessageUtils.java index ae31591..eacd1c7 100644 --- a/src/main/java/org/sadtech/social/core/utils/MessageUtils.java +++ b/src/main/java/org/sadtech/social/core/utils/MessageUtils.java @@ -3,6 +3,8 @@ package org.sadtech.social.core.utils; import org.sadtech.social.core.domain.content.EmptyMessage; import org.sadtech.social.core.domain.content.Message; +import static org.sadtech.social.core.utils.ExceptionMessages.UTILITY_CLASS; + /** * Класс для хранения объекта заглушки для {@link Message}. * @@ -11,7 +13,7 @@ import org.sadtech.social.core.domain.content.Message; public class MessageUtils { private MessageUtils() { - throw new IllegalStateException("Утилитный класс"); + throw new IllegalStateException(UTILITY_CLASS); } public static final EmptyMessage EMPTY_MESSAGE = new EmptyMessage(); diff --git a/src/main/java/org/sadtech/social/core/utils/Sender.java b/src/main/java/org/sadtech/social/core/utils/Sender.java new file mode 100644 index 0000000..8d19c51 --- /dev/null +++ b/src/main/java/org/sadtech/social/core/utils/Sender.java @@ -0,0 +1,37 @@ +package org.sadtech.social.core.utils; + +import org.sadtech.social.core.domain.BoxAnswer; +import org.sadtech.social.core.domain.content.Comment; +import org.sadtech.social.core.domain.content.Message; +import org.sadtech.social.core.service.sender.Sending; + +import static org.sadtech.social.core.utils.ExceptionMessages.UTILITY_CLASS; + +public class Sender { + + private Sender() { + throw new IllegalStateException(UTILITY_CLASS); + } + + public static void sends(Message message, BoxAnswer boxAnswer, Sending sending) { + switch (sending.getType()) { + case PUBLIC: + publicSend(message, boxAnswer, sending); + break; + case PRIVATE: + privateSend(message, boxAnswer, sending); + break; + } + } + + private static void publicSend(Message message, BoxAnswer boxAnswer, Sending sending) { + if (message instanceof Comment) { + sending.send(((Comment) message).getContentId(), message.getPersonId(), boxAnswer); + } + } + + private static void privateSend(Message message, BoxAnswer boxAnswer, Sending sending) { + sending.send(message.getPersonId(), boxAnswer); + } + +}