james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject svn commit: r1689018 - /james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapper.java
Date Fri, 03 Jul 2015 14:35:18 GMT
Author: btellier
Date: Fri Jul  3 14:35:18 2015
New Revision: 1689018

URL: http://svn.apache.org/r1689018
Log:
MAILBOX-208 Use java 8 in Cassandra Message Mapper

Modified:
    james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapper.java

Modified: james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapper.java?rev=1689018&r1=1689017&r2=1689018&view=diff
==============================================================================
--- james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapper.java
(original)
+++ james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMessageMapper.java
Fri Jul  3 14:35:18 2015
@@ -56,10 +56,13 @@ import static org.apache.james.mailbox.c
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.ByteBuffer;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 
 import javax.mail.Flags;
 import javax.mail.Flags.Flag;
@@ -101,14 +104,9 @@ import com.datastax.driver.core.querybui
 import com.datastax.driver.core.querybuilder.Select.Where;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSortedSet;
-import com.google.common.collect.ImmutableSortedSet.Builder;
 import com.google.common.io.ByteStreams;
 import com.google.common.primitives.Bytes;
 
-/**
- * Cassandra implementation of a {@link MessageMapper}.
- */
 public class CassandraMessageMapper implements MessageMapper<CassandraId> {
 
     private final Session session;
@@ -200,12 +198,9 @@ public class CassandraMessageMapper impl
 
     @Override
     public Iterator<Message<CassandraId>> findInMailbox(Mailbox<CassandraId>
mailbox, MessageRange set, FetchType ftype, int max) throws MailboxException {
-        Builder<Message<CassandraId>> result = ImmutableSortedSet.<Message<CassandraId>>
naturalOrder();
-        ResultSet rows = session.execute(buildQuery(mailbox, set));
-        for (Row row : rows) {
-            result.add(message(row));
-        }
-        return result.build().iterator();
+        return convertToStream(session.execute(buildQuery(mailbox, set)))
+            .map(this::message)
+            .iterator();
     }
 
     private byte[] getFullContent(Row row) {
@@ -217,13 +212,18 @@ public class CassandraMessageMapper impl
     }
 
     private Flags getFlags(Row row) {
-        Flags flags = new Flags();
-        for (String flag : CassandraMessageTable.Flag.ALL) {
-            if (row.getBool(flag)) {
-                flags.add(JAVAX_MAIL_FLAG.get(flag));
-            }
-        }
-        return flags;
+        return Arrays.stream(CassandraMessageTable.Flag.ALL)
+            .filter(row::getBool)
+            .map(JAVAX_MAIL_FLAG::get)
+            .reduce(
+                new Flags(),
+                (flags, flag) -> {
+                    flags.add(flag);
+                    return flags;
+                }, (flags1, flags2) -> {
+                    flags1.add(flags2);
+                    return flags1;
+                });
     }
 
     private PropertyBuilder getPropertyBuilder(Row row) {
@@ -515,4 +515,9 @@ public class CassandraMessageMapper impl
         return uidProvider.lastUid(mailboxSession, mailbox);
     }
 
+    private Stream<Row> convertToStream(ResultSet resultSet) {
+        return StreamSupport.stream(resultSet.spliterator(), true);
+    }
+
+
 }



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


Mime
View raw message