james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1131138 - /james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/
Date Fri, 03 Jun 2011 18:54:59 GMT
Author: norman
Date: Fri Jun  3 18:54:59 2011
New Revision: 1131138

URL: http://svn.apache.org/viewvc?rev=1131138&view=rev
Log:
Start to implement Comparators for SearchQuery implementation which need to sort. See MAILBOX-78

Added:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java?rev=1131138&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
(added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
Fri Jun  3 18:54:59 2011
@@ -0,0 +1,51 @@
+/****************************************************************
+ * 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.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+
+import org.apache.james.mailbox.store.mail.model.Message;
+
+/**
+ * {@link Comparator} which takes a Array of other {@link Comparator}'s and use them to compare
two {@link Message} instances till one of them
+ * return <> 0
+ *
+ */
+public class CombinedComparator implements Comparator<Message<?>>{
+
+    private final Comparator<Message<?>>[] comparators;
+    public CombinedComparator(Comparator<Message<?>>[] comparators) {
+        if(comparators == null || comparators.length < 1) {
+            throw new IllegalArgumentException();
+        }
+        this.comparators = comparators;
+    }
+    @Override
+    public int compare(Message<?> o1, Message<?> o2) {
+        int i = 0;
+        for (int a = 0; a < comparators.length; a++) {
+            i = comparators[a].compare(o1, o2);
+            if (i != 0) {
+                break;
+            }
+        }
+        return i;
+    }
+
+}

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java?rev=1131138&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
(added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
Fri Jun  3 18:54:59 2011
@@ -0,0 +1,104 @@
+package org.apache.james.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.james.mailbox.store.mail.model.Header;
+import org.apache.james.mailbox.store.mail.model.Message;
+import org.apache.james.mime4j.field.address.Address;
+import org.apache.james.mime4j.field.address.AddressList;
+import org.apache.james.mime4j.field.address.Group;
+import org.apache.james.mime4j.field.address.Mailbox;
+import org.apache.james.mime4j.field.address.MailboxList;
+
+public class HeaderMailboxComparator implements Comparator<Message<?>>{
+
+    private final String headerName;
+
+    private final static Comparator<Message<?>> FROM = new HeaderMailboxComparator("from");
+    private final static Comparator<Message<?>> REVERSE_FROM = new ReverseComparator(FROM);
+
+
+    private final static Comparator<Message<?>> TO = new HeaderMailboxComparator("to");
+    private final static Comparator<Message<?>> REVERSE_TO = new ReverseComparator(TO);
+
+
+    private final static Comparator<Message<?>> CC = new HeaderMailboxComparator("cc");
+    private final static Comparator<Message<?>> REVERSE_CC = new ReverseComparator(CC);
+
+    
+    public HeaderMailboxComparator(String headerName) {
+        this.headerName = headerName;
+    }
+    
+    @Override
+    public int compare(Message<?> o1, Message<?> o2) {
+        String mailbox1 = getMailbox(o1, headerName);
+        String mailbox2 = getMailbox(o2, headerName);
+
+        return mailbox1.compareTo(mailbox2);
+    }
+    
+    
+    private String getMailbox(Message<?> message, String headerName) {
+        final List<Header> headers = message.getHeaders();
+        for (Header header:headers) {
+            final String name = header.getFieldName();
+            if (headerName.equalsIgnoreCase(name)) {
+                final String value = header.getValue();
+                try {
+                    AddressList aList = AddressList.parse(value);
+                    for (int i = 0; i < aList.size(); i++) {
+                        Address address = aList.get(i);
+                        if (address instanceof Mailbox) {
+                            Mailbox m = (Mailbox) address;
+                            String mailboxName = m.getName();
+                            if (mailboxName == null) {
+                                mailboxName ="";
+                            }
+                            return mailboxName;
+                        } else if (address instanceof Group) {
+                            MailboxList mList = ((Group) address).getMailboxes();
+                            for (int a = 0; a < mList.size(); ) {
+                                String mailboxName = mList.get(a).getName();
+                                if (mailboxName == null) {
+                                    mailboxName ="";
+                                }
+                                return mailboxName;                         
+                            }
+                        }
+                    }
+
+                } catch (org.apache.james.mime4j.field.address.parser.ParseException e) {
+                    return "";
+                }
+            }
+        }
+        return "";
+    }
+    
+    public static Comparator<Message<?>> from(boolean reverse) {
+        if (reverse) {
+            return REVERSE_FROM;
+        } else {
+            return FROM;
+        }
+    }
+    
+    public static Comparator<Message<?>> cc(boolean reverse) {
+        if (reverse) {
+            return REVERSE_CC;
+        } else {
+            return CC;
+        }
+    }
+    
+    public static Comparator<Message<?>> to(boolean reverse) {
+        if (reverse) {
+            return REVERSE_TO;
+        } else {
+            return TO;
+        }
+    }
+}
+

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java?rev=1131138&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
(added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
Fri Jun  3 18:54:59 2011
@@ -0,0 +1,48 @@
+/****************************************************************
+ * 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.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+
+import org.apache.james.mailbox.store.mail.model.Message;
+
+/**
+ * {@link Comparator} which compares {@link Message}'s with their {@link Message#getInternalDate()}
value
+ *
+ */
+public class InternalDateComparator  implements Comparator<Message<?>>{
+
+
+    private final static Comparator<Message<?>> INTERNALDATE = new InternalDateComparator();;
+    private final static Comparator<Message<?>> REVERSE_INTERNALDATE = new ReverseComparator(INTERNALDATE);
+
+    
+    @Override
+    public int compare(Message<?> o1, Message<?> o2) {
+        return (o1.getInternalDate().compareTo(o2.getInternalDate()));
+    }
+
+    public static Comparator<Message<?>> internalDate(boolean reverse){
+        if (reverse) {
+            return REVERSE_INTERNALDATE;
+        } else {
+            return INTERNALDATE;
+        }
+    }
+}
\ No newline at end of file

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java?rev=1131138&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
(added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
Fri Jun  3 18:54:59 2011
@@ -0,0 +1,40 @@
+/****************************************************************
+ * 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.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+
+import org.apache.james.mailbox.store.mail.model.Message;
+
+/**
+ * {@link Comparator} which wraps an other {@link Comparator} and reverse it
+ *
+ */
+public class ReverseComparator implements Comparator<Message<?>>{
+
+    private final Comparator<Message<?>> comparator;
+    public ReverseComparator(Comparator<Message<?>> comparator) {
+        this.comparator = comparator;
+    }
+    @Override
+    public int compare(Message<?> o1, Message<?> o2) {
+        return comparator.compare(o2, o1);
+    }
+
+}

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java?rev=1131138&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
(added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
Fri Jun  3 18:54:59 2011
@@ -0,0 +1,89 @@
+/****************************************************************
+ * 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.mailbox.store.search.comparator;
+
+import java.io.StringReader;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.james.mailbox.store.mail.model.Header;
+import org.apache.james.mailbox.store.mail.model.Message;
+import org.apache.james.mime4j.field.datetime.DateTime;
+import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
+import org.apache.james.mime4j.field.datetime.parser.ParseException;
+
+/**
+ * {@link Comparator} which works like stated in RFC5256 2.2 Sent Date
+ *
+ */
+public class SentDateComparator implements Comparator<Message<?>>{
+
+
+
+    private final static Comparator<Message<?>> SENTDATE = new SentDateComparator(false);
+    private final static Comparator<Message<?>> REVERSE_SENTDATE = new ReverseComparator(new
SentDateComparator(true));
+    
+    private final boolean reverse;
+
+    public SentDateComparator(boolean reverse) {
+        this.reverse = reverse;
+    }
+    
+    @Override
+    public int compare(Message<?> o1, Message<?> o2) {
+        Date date1 = getSentDate(o1);
+        Date date2 = getSentDate(o2);
+        int i = date1.compareTo(date2);
+        
+        // sent date was the same so use the uid as tie-breaker
+        if (i == 0) {
+            return UidComparator.uid(reverse).compare(o1, o2);
+        }
+        return 0;
+    }
+    
+    private Date getSentDate(Message<?> message) {
+        final List<Header> headers = message.getHeaders();
+        for (Header header:headers) {
+            final String name = header.getFieldName();
+            if ("Date".equalsIgnoreCase(name)) {
+                final String value = header.getValue();
+                    final StringReader reader = new StringReader(value);
+                    try {
+                        DateTime dateTime = new DateTimeParser(reader).parseAll();
+                        return dateTime.getDate();
+                    } catch (ParseException e) {
+                        break;
+                    }
+                
+            }
+        }
+        return message.getInternalDate();
+    }
+    
+    public static Comparator<Message<?>> sentDate(boolean reverse){
+        if (reverse) {
+            return REVERSE_SENTDATE;
+        } else {
+            return SENTDATE;
+        }
+    }
+
+}

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java?rev=1131138&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
(added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
Fri Jun  3 18:54:59 2011
@@ -0,0 +1,48 @@
+/****************************************************************
+ * 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.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+
+import org.apache.james.mailbox.store.mail.model.Message;
+
+/**
+ * {@link Comparator} which compares {@link Message}'s with their {@link Message#getFullContentOctets()}
value
+ *
+ */
+public class SizeComparator implements Comparator<Message<?>>{
+
+
+    private final static Comparator<Message<?>> SIZE = new SizeComparator();
+    private final static Comparator<Message<?>> REVERSE_SIZE = new ReverseComparator(SIZE);
+
+    
+    @Override
+    public int compare(Message<?> o1, Message<?> o2) {
+        return (int) (o1.getFullContentOctets() - o2.getFullContentOctets());
+    }
+
+    public Comparator<Message<?>> size(boolean reverse) {
+        if (reverse) {
+            return REVERSE_SIZE;
+        } else {
+            return SIZE;
+        }
+    }
+}

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java?rev=1131138&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
(added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
Fri Jun  3 18:54:59 2011
@@ -0,0 +1,48 @@
+/****************************************************************
+ * 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.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+
+import org.apache.james.mailbox.store.mail.model.Message;
+
+/**
+ * {@link Comparator} which compares {@link Message}'s with their {@link Message#getUid()}
value
+ *
+ */
+public class UidComparator implements Comparator<Message<?>>{
+
+
+    private final static Comparator<Message<?>> UID = new UidComparator();;
+    private final static Comparator<Message<?>> REVERSE_UID = new ReverseComparator(UID);
+
+    
+    @Override
+    public int compare(Message<?> o1, Message<?> o2) {
+        return (int) (o1.getUid() - o2.getUid());
+    }
+
+    public static Comparator<Message<?>> uid(boolean reverse){
+        if (reverse) {
+            return REVERSE_UID;
+        } else {
+            return UID;
+        }
+    }
+}



---------------------------------------------------------------------
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