1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.repository.result

File AggregatedIterableResult.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

4
19
6
1
118
58
8
0.42
3.17
6
1.33

Classes

Class Line # Actions
AggregatedIterableResult 34 19 0% 8 29
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.extension.repository.result;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Iterator;
25    import java.util.List;
26   
27    /**
28    * Make several iterable results look like one.
29    *
30    * @param <T> the type
31    * @version $Id: f9a5cc2e3e0bff83ca4644a5bcbbdd7b2fcc940c $
32    * @since 4.0M1
33    */
 
34    public class AggregatedIterableResult<T> implements IterableResult<T>
35    {
36    /**
37    * The aggregated iterable results.
38    */
39    private List<IterableResult<T>> results = new ArrayList<IterableResult<T>>();
40   
41    /**
42    * @see #getOffset()
43    */
44    private int offset;
45   
46    /**
47    * Cached aggregated total hits.
48    */
49    private Integer totalHits;
50   
51    /**
52    * Cached aggregated size.
53    */
54    private Integer size;
55   
56    /**
57    * @param offset the initial offset
58    */
 
59  0 toggle public AggregatedIterableResult(int offset)
60    {
61  0 this.offset = offset;
62    }
63   
64    /**
65    * @param result a iterable result instance to append
66    */
 
67  0 toggle public void addSearchResult(IterableResult<T> result)
68    {
69  0 this.results.add(result);
70   
71    // Reset caches
72  0 this.totalHits = null;
73  0 this.size = null;
74    }
75   
 
76  0 toggle @Override
77    public Iterator<T> iterator()
78    {
79  0 Collection<Iterator<T>> resultIterators = new ArrayList<Iterator<T>>();
80  0 for (IterableResult<T> result : this.results) {
81  0 resultIterators.add(result.iterator());
82    }
83   
84  0 return new AggregatedIterator<T>(resultIterators.iterator());
85    }
86   
 
87  0 toggle @Override
88    public int getTotalHits()
89    {
90  0 if (this.totalHits == null) {
91  0 this.totalHits = 0;
92  0 for (IterableResult<T> result : this.results) {
93  0 this.totalHits += result.getTotalHits();
94    }
95    }
96   
97  0 return this.totalHits;
98    }
99   
 
100  0 toggle @Override
101    public int getOffset()
102    {
103  0 return this.offset;
104    }
105   
 
106  0 toggle @Override
107    public int getSize()
108    {
109  0 if (this.size == null) {
110  0 this.size = 0;
111  0 for (IterableResult<T> result : this.results) {
112  0 this.size += result.getTotalHits();
113    }
114    }
115   
116  0 return this.size;
117    }
118    }