1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.mail.internal.factory.usersandgroups

File AddressUserIterator.java

 

Coverage histogram

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

Code metrics

12
23
4
1
117
65
11
0.48
5.75
4
2.75

Classes

Class Line # Actions
AddressUserIterator 42 23 0% 11 7
0.8205128382.1%
 

Contributing tests

This file is covered by 3 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.factory.usersandgroups;
21   
22    import java.util.ArrayList;
23    import java.util.Iterator;
24    import java.util.List;
25    import java.util.NoSuchElementException;
26   
27    import javax.mail.Address;
28   
29    import org.xwiki.context.Execution;
30    import org.xwiki.model.reference.DocumentReferenceResolver;
31   
32    import com.xpn.xwiki.internal.plugin.rightsmanager.UserIterator;
33   
34    /**
35    * Iterates over passed user and group references and return user's email addresses, but also handles an extra list
36    * of email addresses to iterate over. Handles email duplication and an email exclusion list can be passed.
37    *
38    * @version $Id: f2e26cf09075f02873a919d2c58f8c1f3dd2ad48 $
39    * @since 6.4.2
40    * @since 7.0M2
41    */
 
42    public class AddressUserIterator extends UserIterator<Address>
43    {
44    private UsersAndGroupsSource usersAndGroupsSource;
45   
46    private Iterator<Address> addressIterator;
47   
48    private List<Address> excludedAddresses;
49   
50    private List<Address> processedAddresses = new ArrayList<>();
51   
52    private Address lookaheadAddress;
53   
54    /**
55    * @param usersAndGroupsSource the list of group and user references to iterate over along with a list of email
56    * addresses, with optional exclusion lists
57    * @param explicitDocumentReferenceResolver the resolver to use for transforming group member strings into
58    * {@link org.xwiki.model.reference.DocumentReference}
59    * @param execution the component used to access the {@link com.xpn.xwiki.XWikiContext} we use to call oldcore APIs
60    */
 
61  4 toggle public AddressUserIterator(UsersAndGroupsSource usersAndGroupsSource,
62    DocumentReferenceResolver<String> explicitDocumentReferenceResolver, Execution execution)
63    {
64  4 super(usersAndGroupsSource.getIncludedUserAndGroupReferences(),
65    usersAndGroupsSource.getExcludedUserAndGroupReferences(), new AddressUserDataExtractor(),
66    explicitDocumentReferenceResolver, execution);
67  4 this.addressIterator = usersAndGroupsSource.getIncludedAddresses().iterator();
68  4 this.excludedAddresses = usersAndGroupsSource.getExcludedAddresses();
69  4 this.usersAndGroupsSource = usersAndGroupsSource;
70    }
71   
 
72  7 toggle @Override
73    public boolean hasNext()
74    {
75  7 if (this.lookaheadAddress == null) {
76  7 this.lookaheadAddress = getNext();
77    }
78  7 return this.lookaheadAddress != null;
79    }
80   
 
81  5 toggle @Override
82    public Address next()
83    {
84  5 Address address = this.lookaheadAddress;
85  5 if (address != null) {
86  5 this.lookaheadAddress = null;
87    } else {
88  0 address = getNext();
89  0 if (address == null) {
90  0 throw new NoSuchElementException(String.format("No more addresses to extract from [%s]",
91    this.usersAndGroupsSource));
92    }
93    }
94  5 return address;
95    }
96   
 
97  7 toggle private Address getNext()
98    {
99  7 Address address;
100   
101  7 do {
102    // Are there still group and user references to process?
103    // If not, are there still email addresses to process?
104  9 if (super.hasNext()) {
105  6 address = super.next();
106  3 } else if (this.addressIterator.hasNext()) {
107  1 address = this.addressIterator.next();
108    } else {
109  2 return null;
110    }
111  7 } while (this.excludedAddresses.contains(address) || this.processedAddresses.contains(address));
112   
113  5 this.processedAddresses.add(address);
114   
115  5 return address;
116    }
117    }