JAMES-2555 Working with ImpactingMessageEvents should not require casts
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/62e79d1b Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/62e79d1b Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/62e79d1b Branch: refs/heads/master Commit: 62e79d1b9ec6009b329d8279710acfa8c3af9ac5 Parents: 2d3691d Author: Benoit Tellier <btell...@linagora.com> Authored: Tue Oct 9 08:52:30 2018 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Fri Oct 12 15:27:13 2018 +0700 ---------------------------------------------------------------------- .../mailbox/tools/indexer/ReIndexerImpl.java | 9 +- .../tools/indexer/events/FlagsMessageEvent.java | 75 ------------ .../indexer/events/ImpactingMessageEvent.java | 116 +++++++++++++++++++ .../indexer/events/MessageDeletedEvent.java | 65 ----------- .../registrations/MailboxRegistration.java | 4 +- .../registrations/MailboxRegistrationTest.java | 4 +- 6 files changed, 124 insertions(+), 149 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/62e79d1b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerImpl.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerImpl.java b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerImpl.java index 28c8421..413f6b7 100644 --- a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerImpl.java +++ b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerImpl.java @@ -37,7 +37,6 @@ import org.apache.james.mailbox.store.mail.MessageMapper; import org.apache.james.mailbox.store.mail.model.Mailbox; import org.apache.james.mailbox.store.mail.model.MailboxMessage; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; -import org.apache.mailbox.tools.indexer.events.FlagsMessageEvent; import org.apache.mailbox.tools.indexer.events.ImpactingEventType; import org.apache.mailbox.tools.indexer.events.ImpactingMessageEvent; import org.apache.mailbox.tools.indexer.registrations.GlobalRegistration; @@ -137,10 +136,10 @@ public class ReIndexerImpl implements ReIndexer { while (iterator.hasNext()) { MailboxMessage message = iterator.next(); ImpactingMessageEvent impactingMessageEvent = findMostRelevant(mailboxRegistration.getImpactingEvents(message.getUid())); - if (impactingMessageEvent == null) { - messageSearchIndex.add(mailboxSession, mailbox, message); - } else if (impactingMessageEvent instanceof FlagsMessageEvent) { - message.setFlags(((FlagsMessageEvent) impactingMessageEvent).getFlags()); + + impactingMessageEvent.newFlags().ifPresent(message::setFlags); + + if (!impactingMessageEvent.wasDeleted()) { messageSearchIndex.add(mailboxSession, mailbox, message); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/62e79d1b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/FlagsMessageEvent.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/FlagsMessageEvent.java b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/FlagsMessageEvent.java deleted file mode 100644 index 10d7482..0000000 --- a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/FlagsMessageEvent.java +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.mailbox.tools.indexer.events; - -import javax.mail.Flags; - -import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.model.MailboxPath; - -import com.google.common.base.Objects; - -public class FlagsMessageEvent implements ImpactingMessageEvent { - - private final MailboxPath mailboxPath; - private final MessageUid uid; - private final Flags flags; - - public FlagsMessageEvent(MailboxPath mailboxPath, MessageUid uid, Flags flags) { - this.mailboxPath = mailboxPath; - this.uid = uid; - this.flags = flags; - } - - @Override - public MessageUid getUid() { - return uid; - } - - @Override - public MailboxPath getMailboxPath() { - return mailboxPath; - } - - @Override - public ImpactingEventType getType() { - return ImpactingEventType.FlagsUpdate; - } - - public Flags getFlags() { - return flags; - } - - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) { - return false; - } - FlagsMessageEvent that = (FlagsMessageEvent) o; - return Objects.equal(uid, that.uid) && - Objects.equal(mailboxPath, that.mailboxPath) && - Objects.equal(flags, that.flags); - } - - @Override - public int hashCode() { - return Objects.hashCode(uid, mailboxPath, flags); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/62e79d1b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/ImpactingMessageEvent.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/ImpactingMessageEvent.java b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/ImpactingMessageEvent.java index 83764f3..527f463 100644 --- a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/ImpactingMessageEvent.java +++ b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/ImpactingMessageEvent.java @@ -19,10 +19,126 @@ package org.apache.mailbox.tools.indexer.events; +import java.util.Objects; +import java.util.Optional; + +import javax.mail.Flags; + import org.apache.james.mailbox.MessageUid; +import org.apache.james.mailbox.model.MailboxPath; public interface ImpactingMessageEvent extends ImpactingEvent { MessageUid getUid(); + Optional<Flags> newFlags(); + + boolean wasDeleted(); + + class FlagsMessageEvent implements ImpactingMessageEvent { + + private final MailboxPath mailboxPath; + private final MessageUid uid; + private final Flags flags; + + public FlagsMessageEvent(MailboxPath mailboxPath, MessageUid uid, Flags flags) { + this.mailboxPath = mailboxPath; + this.uid = uid; + this.flags = flags; + } + + @Override + public MessageUid getUid() { + return uid; + } + + @Override + public MailboxPath getMailboxPath() { + return mailboxPath; + } + + @Override + public ImpactingEventType getType() { + return ImpactingEventType.FlagsUpdate; + } + + @Override + public Optional<Flags> newFlags() { + return Optional.of(flags); + } + + @Override + public boolean wasDeleted() { + return false; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof FlagsMessageEvent) { + FlagsMessageEvent that = (FlagsMessageEvent) o; + + return Objects.equals(this.mailboxPath, that.mailboxPath) + && Objects.equals(this.uid, that.uid) + && Objects.equals(this.flags, that.flags); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(mailboxPath, uid, flags); + } + } + + class MessageDeletedEvent implements ImpactingMessageEvent { + + private final MailboxPath mailboxPath; + private final MessageUid uid; + + public MessageDeletedEvent(MailboxPath mailboxPath, MessageUid uid) { + this.mailboxPath = mailboxPath; + this.uid = uid; + } + + @Override + public MessageUid getUid() { + return uid; + } + + @Override + public MailboxPath getMailboxPath() { + return mailboxPath; + } + + @Override + public ImpactingEventType getType() { + return ImpactingEventType.Deletion; + } + + @Override + public Optional<Flags> newFlags() { + return Optional.empty(); + } + + @Override + public boolean wasDeleted() { + return true; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof MessageDeletedEvent) { + MessageDeletedEvent that = (MessageDeletedEvent) o; + + return Objects.equals(this.mailboxPath, that.mailboxPath) + && Objects.equals(this.uid, that.uid); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(mailboxPath, uid); + } + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/62e79d1b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/MessageDeletedEvent.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/MessageDeletedEvent.java b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/MessageDeletedEvent.java deleted file mode 100644 index a74178a..0000000 --- a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/events/MessageDeletedEvent.java +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.mailbox.tools.indexer.events; - -import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.model.MailboxPath; - -import com.google.common.base.Objects; - -public class MessageDeletedEvent implements ImpactingMessageEvent { - - private final MailboxPath mailboxPath; - private final MessageUid uid; - - public MessageDeletedEvent(MailboxPath mailboxPath, MessageUid uid) { - this.mailboxPath = mailboxPath; - this.uid = uid; - } - - @Override - public MessageUid getUid() { - return uid; - } - - @Override - public MailboxPath getMailboxPath() { - return mailboxPath; - } - - @Override - public ImpactingEventType getType() { - return ImpactingEventType.Deletion; - } - - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) { - return false; - } - MessageDeletedEvent that = (MessageDeletedEvent) o; - return Objects.equal(uid, that.uid) && Objects.equal(mailboxPath, that.mailboxPath); - } - - @Override - public int hashCode() { - return Objects.hashCode(uid, mailboxPath); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/62e79d1b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistration.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistration.java b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistration.java index 7c98233..251fde8 100644 --- a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistration.java +++ b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistration.java @@ -26,9 +26,9 @@ import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.MessageUid; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.UpdatedFlags; -import org.apache.mailbox.tools.indexer.events.FlagsMessageEvent; import org.apache.mailbox.tools.indexer.events.ImpactingMessageEvent; -import org.apache.mailbox.tools.indexer.events.MessageDeletedEvent; +import org.apache.mailbox.tools.indexer.events.ImpactingMessageEvent.FlagsMessageEvent; +import org.apache.mailbox.tools.indexer.events.ImpactingMessageEvent.MessageDeletedEvent; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableList; http://git-wip-us.apache.org/repos/asf/james-project/blob/62e79d1b/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistrationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistrationTest.java b/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistrationTest.java index 743c1f8..33a19b7 100644 --- a/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistrationTest.java +++ b/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/MailboxRegistrationTest.java @@ -37,8 +37,8 @@ import org.apache.james.mailbox.store.event.EventFactory; import org.apache.james.mailbox.store.mail.model.DefaultMessageId; import org.apache.james.mailbox.store.mail.model.MailboxMessage; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox; -import org.apache.mailbox.tools.indexer.events.FlagsMessageEvent; -import org.apache.mailbox.tools.indexer.events.MessageDeletedEvent; +import org.apache.mailbox.tools.indexer.events.ImpactingMessageEvent.FlagsMessageEvent; +import org.apache.mailbox.tools.indexer.events.ImpactingMessageEvent.MessageDeletedEvent; import org.junit.Before; import org.junit.Test; --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org