@@ -37,7 +37,7 @@ protected LazyFluentIterable() {
37
37
}
38
38
39
39
/**
40
- * Adds a filter operation to the operation chain and returns a new iterable .
40
+ * Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy the predicate .
41
41
* @param predicate the condition to test with for the filtering. If the test
42
42
* is negative, the tested object is removed by the iterator.
43
43
* @return a new FluentIterable object that decorates the source iterable
@@ -50,53 +50,33 @@ public Iterator<TYPE> iterator() {
50
50
return new DecoratingIterator <TYPE >(iterable .iterator ()) {
51
51
@ Override
52
52
public TYPE computeNext () {
53
- while (true ) {
54
- if (fromIterator .hasNext ()) {
55
- TYPE candidate = fromIterator .next ();
56
- if (!predicate .test (candidate )) {
57
- continue ;
58
- }
59
- return candidate ;
53
+ while (fromIterator .hasNext ()) {
54
+ TYPE candidate = fromIterator .next ();
55
+ if (!predicate .test (candidate )) {
56
+ continue ;
60
57
}
61
-
62
- return null ;
58
+ return candidate ;
63
59
}
60
+
61
+ return null ;
64
62
}
65
63
};
66
64
}
67
65
};
68
66
}
69
67
70
- /**
71
- * Uses the Iterable interface's forEach method to apply a given function
72
- * for each object of the iterator. Is a terminating operation.
73
- * @param action the action for each object
74
- */
75
- @ Override
76
- public void forEachDo (Consumer <? super TYPE > action ) {
77
- Iterator <TYPE > newIterator = iterable .iterator ();
78
- while (newIterator .hasNext ()) {
79
- action .accept (newIterator .next ());
80
- }
81
- }
82
-
83
68
/**
84
69
* Can be used to collect objects from the iteration. Is a terminating operation.
85
- * @return an option of the first object of the iteration
70
+ * @return an Optional containing the first object of this Iterable
86
71
*/
87
72
@ Override
88
73
public Optional <TYPE > first () {
89
- Optional result = Optional .empty ();
90
- List <TYPE > list = first (1 ).asList ();
91
- if (!list .isEmpty ()) {
92
- result = Optional .of (list .get (0 ));
93
- }
94
-
95
- return result ;
74
+ Iterator <TYPE > resultIterator = first (1 ).iterator ();
75
+ return resultIterator .hasNext () ? Optional .of (resultIterator .next ()) : Optional .empty ();
96
76
}
97
77
98
78
/**
99
- * Can be used to collect objects from the iteration. Is a terminating operation.
79
+ * Can be used to collect objects from the iteration.
100
80
* @param count defines the number of objects to return
101
81
* @return the same FluentIterable with a collection decimated to a maximum of 'count' first objects.
102
82
*/
@@ -126,21 +106,18 @@ public TYPE computeNext() {
126
106
127
107
/**
128
108
* Can be used to collect objects from the iteration. Is a terminating operation.
129
- * @return an option of the last object of the iteration
109
+ * @return an Optional containing the last object of this Iterable
130
110
*/
131
111
@ Override
132
112
public Optional <TYPE > last () {
133
- Optional result = Optional .empty ();
134
- List <TYPE > list = last (1 ).asList ();
135
- if (!list .isEmpty ()) {
136
- result = Optional .of (list .get (0 ));
137
- }
138
-
139
- return result ;
113
+ Iterator <TYPE > resultIterator = last (1 ).iterator ();
114
+ return resultIterator .hasNext () ? Optional .of (resultIterator .next ()) : Optional .empty ();
140
115
}
141
116
142
117
/**
143
- * Can be used to collect objects from the iteration. Is a terminating operation.
118
+ * Can be used to collect objects from the Iterable. Is a terminating operation.
119
+ * This operation is memory intensive, because the contents of this Iterable
120
+ * are collected into a List, when the next object is requested.
144
121
* @param count defines the number of objects to return
145
122
* @return the same FluentIterable with a collection decimated to a maximum of 'count' last objects
146
123
*/
@@ -193,13 +170,11 @@ public Iterator<NEW_TYPE> iterator() {
193
170
Iterator <TYPE > oldTypeIterator = iterable .iterator ();
194
171
@ Override
195
172
public NEW_TYPE computeNext () {
196
- while (true ) {
197
- if (oldTypeIterator .hasNext ()) {
198
- TYPE candidate = oldTypeIterator .next ();
199
- return function .apply (candidate );
200
- }
201
- return null ;
173
+ while (oldTypeIterator .hasNext ()) {
174
+ TYPE candidate = oldTypeIterator .next ();
175
+ return function .apply (candidate );
202
176
}
177
+ return null ;
203
178
}
204
179
};
205
180
}
0 commit comments