1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.criteria.impl

File Range.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

12
28
6
1
145
60
13
0.46
4.67
6
2.17

Classes

Class Line # Actions
Range 28 28 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 16 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 com.xpn.xwiki.criteria.impl;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    /**
26    * Immutable continuous integer range. It can be used for pagination
27    */
 
28    public class Range
29    {
30    /**
31    * The start of the interval. It can be both positive and negative. A negative start is usually associated with an
32    * interval which is relative to the end of the list it is applied to.
33    */
34    private int start;
35   
36    /**
37    * The size of the interval. It can be both positive and negative. A negative size is usually associated with an
38    * interval which is relative to the end of the list it is applied to.
39    */
40    private int size;
41   
42    /**
43    * Creates a new interval having the specified start and size.
44    *
45    * @param start The start of the interval
46    * @param size The size of the interval
47    */
 
48  60 toggle public Range(int start, int size)
49    {
50  60 this.start = start;
51  60 this.size = size;
52    }
53   
54    /**
55    * @see #start
56    */
 
57  21 toggle public int getStart()
58    {
59  21 return this.start;
60    }
61   
62    /**
63    * @return The absolute value (nonnegative) of this interval's start
64    */
 
65  21 toggle public int getAbsoluteStart()
66    {
67  21 return Math.abs(this.start);
68    }
69   
70    /**
71    * @see #size
72    */
 
73  21 toggle public int getSize()
74    {
75  21 return this.size;
76    }
77   
78    /**
79    * @return The absolute value (nonnegative) of this interval's size
80    */
 
81  44 toggle public int getAbsoluteSize()
82    {
83  44 return Math.abs(this.size);
84    }
85   
86    /**
87    * Copy the given list and return a (smart) subList corresponding to this range. If the absolute size of this range
88    * is 0 (ALL) it returns an unmodified copy.
89    * <p>
90    * Considering this 9 elements list : [0, 1, 2, 3, 4, 5, 6, 7, 8]
91    * <ul>
92    * <li>Range 0 / 4 : will return [0, 1, 2, 3]</li>
93    * <li>Range -2 / 4 : will return [7, 8] (not enough elements for the given size)</li>
94    * <li>Range -2 / -4 : will return [3, 4, 5, 6]</li>
95    * <li>Range 2 / -4 : will return [0, 1] (not enough elements for the given size)</li>
96    * <li>Range 0 / -4 : will return [5, 6, 7, 8]</li>
97    * </ul>
98    *
99    * @param list the list from which the sublist will be extracted
100    * @return a sublist of the given list computed from this range
101    */
 
102  23 toggle public List<String> subList(List<String> list)
103    {
104  23 List<String> results = new ArrayList<String>();
105  23 results.addAll(list);
106   
107  23 if (getAbsoluteSize() > 0) {
108  15 int min = 0;
109  15 int max = 0;
110   
111  15 min = this.start;
112   
113    // negative start : relative to the end of the list
114  15 if (min < 0) {
115  2 min = (list.size()) + this.start;
116    }
117   
118  15 max = min + this.size;
119   
120    // negative size with start 0 : 0 represents to the end of the list
121  15 if (min == 0 && max < 0) {
122  2 min = list.size() + max;
123  2 max = list.size();
124    }
125   
126    // with both start and size negative the maximum becomes the mininum & vice versa
127  15 if (min > max) {
128  9 int oldmax = max;
129  9 max = min;
130  9 min = oldmax;
131    }
132   
133    // out of bounds "sanitization"
134  15 if (min < 0) {
135  8 min = 0;
136    }
137  15 if (max > list.size()) {
138  2 max = list.size();
139    }
140   
141  15 results = results.subList(min, max);
142    }
143  23 return results;
144    }
145    }