1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal

File MemoryMailStatusResult.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
19
10
2
139
81
14
0.74
1.9
5
1.4

Classes

Class Line # Actions
MemoryMailStatusResult 39 7 0% 7 2
0.8571428785.7%
MemoryMailStatusResult.AbstractMailStatusIterator 41 12 0% 7 4
0.809523881%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.mail.internal;
21   
22    import java.util.Iterator;
23    import java.util.LinkedHashMap;
24    import java.util.Map;
25    import java.util.NoSuchElementException;
26   
27    import org.xwiki.mail.MailState;
28    import org.xwiki.mail.MailStatus;
29   
30    /**
31    * Implementation that saves all mail statuses in a Map in memory.
32    *
33    * This implementation is not meant for scalability. Don't use it if you're sending a large number of emails. Instead
34    * use a Database Mail Listener for example.
35    *
36    * @version $Id: e21c16cfc8b2b2450f198137fd89c16f831f397b $
37    * @since 6.4M3
38    */
 
39    public class MemoryMailStatusResult extends AbstractMailStatusResult
40    {
 
41    private abstract class AbstractMailStatusIterator implements Iterator<MailStatus>
42    {
43    private final Iterator<MailStatus> it = statusMap.values().iterator();
44    private MailStatus nextStatus;
45   
46    abstract boolean match(MailStatus status);
47   
 
48  33 toggle @Override
49    public boolean hasNext()
50    {
51  62 while (nextStatus == null && it.hasNext()) {
52  29 nextStatus = it.next();
53  29 if (!match(nextStatus)) {
54  16 nextStatus = null;
55    }
56    }
57  33 return (nextStatus != null);
58    }
59   
 
60  13 toggle @Override
61    public MailStatus next()
62    {
63  13 hasNext();
64  13 if (nextStatus == null) {
65  0 throw new NoSuchElementException();
66    }
67  13 try {
68  13 return nextStatus;
69    } finally {
70  13 nextStatus = null;
71    }
72    }
73   
 
74  0 toggle @Override
75    public void remove()
76    {
77  0 throw new UnsupportedOperationException();
78    }
79    }
80   
81    /**
82    * The Map's key is the unique message ID.
83    *
84    * Note that we keep the order in which messages are passed (i.e. the first status result will contain the first
85    * mail sent, etc).
86    */
87    private Map<String, MailStatus> statusMap = new LinkedHashMap<>();
88   
89    /**
90    * Retrieve the status for the given message identifier.
91    *
92    * @param uniqueMessageId the unique id of the message.
93    * @return the mail status for the given message, or null if none were found.
94    */
 
95  2 toggle public MailStatus getStatus(String uniqueMessageId)
96    {
97  2 return this.statusMap.get(uniqueMessageId);
98    }
99   
100    /**
101    * Changes the status for the message referenced in the passed status object.
102    *
103    * @param status the new status. Also contains the message id representing the target message
104    */
 
105  33 toggle public void setStatus(MailStatus status)
106    {
107  33 this.statusMap.put(status.getMessageId(), status);
108    }
109   
 
110  0 toggle @Override
111    public Iterator<MailStatus> getAll()
112    {
113  0 return this.statusMap.values().iterator();
114    }
115   
 
116  1 toggle @Override
117    public Iterator<MailStatus> getAllErrors()
118    {
119  1 return new AbstractMailStatusIterator() {
 
120  5 toggle @Override
121    boolean match(MailStatus status)
122    {
123  5 return status.getState().endsWith("_error");
124    }
125    };
126    }
127   
 
128  15 toggle @Override
129    public Iterator<MailStatus> getByState(final MailState state)
130    {
131  15 return new AbstractMailStatusIterator() {
 
132  24 toggle @Override
133    boolean match(MailStatus status)
134    {
135  24 return MailState.parse(status.getState()).equals(state);
136    }
137    };
138    }
139    }