Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
RangeIterable | 33 | 19 | 0% | 11 | 9 |
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.rest.internal; | |
21 | ||
22 | import java.util.Iterator; | |
23 | import java.util.List; | |
24 | ||
25 | /** | |
26 | * <p> | |
27 | * This class wraps a List<T> and provides a view of it as if had only the elements that were included in the specified | |
28 | * range (start, number) | |
29 | * </p> | |
30 | * | |
31 | * @version $Id: 24e5d8ff553b0f39b01cfc2a0a6ead921a5dcbae $ | |
32 | */ | |
33 | public class RangeIterable<T> implements Iterable<T> | |
34 | { | |
35 | /** | |
36 | * <p> | |
37 | * The wrapped list. | |
38 | * </p> | |
39 | */ | |
40 | private List<T> list; | |
41 | ||
42 | /** | |
43 | * <p> | |
44 | * The initial offset of the sublist to be exposed | |
45 | * </p> | |
46 | */ | |
47 | private int start; | |
48 | ||
49 | /** | |
50 | * <p> | |
51 | * The number of elements of the sublist to be exposed. | |
52 | * </p> | |
53 | */ | |
54 | private int number; | |
55 | ||
56 | 27 | public RangeIterable(List<T> list, int start, int number) |
57 | { | |
58 | 27 | this.list = list; |
59 | ||
60 | /* Adjust start offset if out of bounds. */ | |
61 | 27 | if (start < 0) { |
62 | 0 | start = 0; |
63 | } | |
64 | ||
65 | 27 | if (start > list.size()) { |
66 | 0 | start = list.size(); |
67 | } | |
68 | ||
69 | /* Adjust the number of elements to convenient values. */ | |
70 | 27 | if (number < 0) { |
71 | 27 | number = list.size(); |
72 | } | |
73 | ||
74 | 27 | if (start + number > list.size()) { |
75 | 0 | number = list.size() - start; |
76 | } | |
77 | ||
78 | 27 | this.start = start; |
79 | 27 | this.number = number; |
80 | ||
81 | } | |
82 | ||
83 | /** | |
84 | * <p> | |
85 | * Return an iterator that will iterate over the sublist defined by (start, number) | |
86 | * </p> | |
87 | */ | |
88 | 27 | @Override |
89 | public Iterator<T> iterator() | |
90 | { | |
91 | 27 | return new Iterator<T>() |
92 | { | |
93 | /** | |
94 | * <p> | |
95 | * The number of elements already returned | |
96 | * </p> | |
97 | */ | |
98 | private int i = 0; | |
99 | ||
100 | 107 | @Override |
101 | public boolean hasNext() | |
102 | { | |
103 | 107 | if (i < number) { |
104 | 80 | if (i + start < list.size()) { |
105 | 80 | return true; |
106 | } | |
107 | } | |
108 | ||
109 | 27 | return false; |
110 | } | |
111 | ||
112 | 80 | @Override |
113 | public T next() | |
114 | { | |
115 | 80 | T result = list.get(i + start); |
116 | 80 | i++; |
117 | ||
118 | 80 | return result; |
119 | } | |
120 | ||
121 | 0 | @Override |
122 | public void remove() | |
123 | { | |
124 | } | |
125 | }; | |
126 | } | |
127 | } |