JAMES-1717 Implement a memory Notification Registry

Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/caebbc02
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/caebbc02
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/caebbc02

Branch: refs/heads/master
Commit: caebbc02b4c7b31e55e3a3a0d3871c9c168946d4
Parents: 9ebad96
Author: Benoit Tellier <btell...@linagora.com>
Authored: Mon May 23 14:02:12 2016 +0700
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Fri May 27 18:02:46 2016 +0700

----------------------------------------------------------------------
 .../vacation/MemoryNotificationRegistry.java    | 94 ++++++++++++++++++++
 .../MemoryNotificationRegistryTest.java         | 32 +++++++
 2 files changed, 126 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/caebbc02/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistry.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistry.java
 
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistry.java
new file mode 100644
index 0000000..f9e6733
--- /dev/null
+++ 
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistry.java
@@ -0,0 +1,94 @@
+/****************************************************************
+ * 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.james.jmap.memory.vacation;
+
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+
+import javax.inject.Inject;
+
+import org.apache.james.jmap.api.vacation.AccountId;
+import org.apache.james.jmap.api.vacation.NotificationRegistry;
+import org.apache.james.jmap.api.vacation.RecipientId;
+import org.apache.james.util.date.ZonedDateTimeProvider;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+
+public class MemoryNotificationRegistry implements NotificationRegistry {
+
+    public static class Entry {
+        private final RecipientId recipientId;
+        private final Optional<ZonedDateTime> expiryDate;
+
+        public Entry(RecipientId recipientId, Optional<ZonedDateTime> 
expiryDate) {
+            this.recipientId = recipientId;
+            this.expiryDate = expiryDate;
+        }
+
+        public RecipientId getRecipientId() {
+            return recipientId;
+        }
+
+        public Optional<ZonedDateTime> getExpiryDate() {
+            return expiryDate;
+        }
+    }
+
+    private final ZonedDateTimeProvider zonedDateTimeProvider;
+    private final Multimap<AccountId, Entry> registrations;
+
+    @Inject
+    public MemoryNotificationRegistry(ZonedDateTimeProvider 
zonedDateTimeProvider) {
+        this.zonedDateTimeProvider = zonedDateTimeProvider;
+        this.registrations = 
Multimaps.synchronizedMultimap(HashMultimap.create());
+}
+
+    @Override
+    public CompletableFuture<Void> register(AccountId accountId, RecipientId 
recipientId, Optional<ZonedDateTime> expiryDate) {
+        registrations.put(accountId, new Entry(recipientId, expiryDate));
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> isRegistered(AccountId accountId, 
RecipientId recipientId) {
+        ZonedDateTime currentTime = zonedDateTimeProvider.get();
+        return CompletableFuture.completedFuture(
+            registrations.get(accountId)
+                .stream()
+                .filter(entry -> entry.getRecipientId().equals(recipientId))
+                .map(Entry::getExpiryDate)
+                .findAny()
+                .filter(optional -> optional.map(registrationEnd -> 
isStrictlyBefore(currentTime, registrationEnd)).orElse(true))
+                .isPresent());
+    }
+
+    private boolean isStrictlyBefore(ZonedDateTime currentTime, ZonedDateTime 
registrationEnd) {
+        return ! currentTime.isAfter(registrationEnd);
+    }
+
+    @Override
+    public CompletableFuture<Void> flush(AccountId accountId) {
+        registrations.removeAll(accountId);
+        return CompletableFuture.completedFuture(null);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/caebbc02/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java
 
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java
new file mode 100644
index 0000000..1d6bbc6
--- /dev/null
+++ 
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java
@@ -0,0 +1,32 @@
+/****************************************************************
+ * 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.james.jmap.memory.vacation;
+
+import org.apache.james.jmap.api.vacation.AbstractNotificationRegistryTest;
+import org.apache.james.jmap.api.vacation.NotificationRegistry;
+import org.apache.james.util.date.ZonedDateTimeProvider;
+
+public class MemoryNotificationRegistryTest extends 
AbstractNotificationRegistryTest {
+
+    @Override
+    protected NotificationRegistry 
createNotificationRegistry(ZonedDateTimeProvider zonedDateTimeProvider) {
+        return new MemoryNotificationRegistry(zonedDateTimeProvider);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to