Skip to content

Commit 547d097

Browse files
committed
Adds collect() and collectAtMost() with no predicate.
1 parent 8b19d81 commit 547d097

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/main/java/org/javimmutable/collections/IterableStreamable.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,41 @@ default Holder<T> first(@Nonnull Predicate<T> predicate)
171171
return Holders.of();
172172
}
173173

174+
/**
175+
* Add all elements to the collection.
176+
*
177+
* @param collection collection to accumulate the values
178+
* @return the collection after all elements have been processed
179+
*/
180+
default <C extends Insertable<T, C>> C collectAll(@Nonnull C collection)
181+
{
182+
for (T value : this) {
183+
collection = collection.insert(value);
184+
}
185+
return collection;
186+
}
187+
188+
/**
189+
* Add the first maxToCollect elements to the collection
190+
*
191+
* @param collection collection to accumulate the values
192+
* @return the collection after all elements have been processed
193+
*/
194+
default <C extends Insertable<T, C>> C collectAtMost(int maxToCollect,
195+
@Nonnull C collection)
196+
{
197+
final Iterator<T> iterator = iterator();
198+
for (int i = 0; i < maxToCollect && iterator.hasNext(); ++i) {
199+
collection = collection.insert(iterator.next());
200+
}
201+
return collection;
202+
}
203+
174204
/**
175205
* Apply the predicate to every element in iterator order and add any elements for which
176206
* predicate returns true to the collection.
177207
*
178-
* @param collection collection to accumulate the transformed values
208+
* @param collection collection to accumulate the values
179209
* @param predicate predicate applied to each element
180210
* @return the collection after all elements have been processed
181211
*/
@@ -195,7 +225,7 @@ default <C extends Insertable<T, C>> C collectAll(@Nonnull C collection,
195225
* predicate returns true to the collection. Iteration stops if maxToCollect values have
196226
* been added to collection.
197227
*
198-
* @param collection collection to accumulate the transformed values
228+
* @param collection collection to accumulate the values
199229
* @param predicate predicate applied to each element
200230
* @return the collection after all elements have been processed
201231
*/

src/test/java/org/javimmutable/collections/util/IterableStreamableTest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import org.javimmutable.collections.JImmutableList;
4343
import org.javimmutable.collections.MapEntry;
4444

45-
import static org.javimmutable.collections.util.JImmutables.list;
45+
import static org.javimmutable.collections.util.JImmutables.*;
4646

4747
public class IterableStreamableTest
4848
extends TestCase
@@ -87,6 +87,24 @@ public void testFirst()
8787
assertEquals(Holders.of(3), list(1, 3, 5).first(x -> x >= 3));
8888
}
8989

90+
public void testCollectAll()
91+
{
92+
assertSame(set(), list().collectAll(set()));
93+
assertEquals(set(1), list(1).collectAll(set()));
94+
assertEquals(set(1, 2, 3, 4, 5), list(1, 2, 3, 4, 5).collectAll(set()));
95+
}
96+
97+
public void testCollectAtMost()
98+
{
99+
assertSame(set(), list().collectAtMost(100, set()));
100+
assertSame(set(), list(1).collectAtMost(0, set()));
101+
assertEquals(set(1), list(1).collectAtMost(1, set()));
102+
assertEquals(set(1), list(1).collectAtMost(100, set()));
103+
assertEquals(set(1), list(1, 2, 3, 4, 5).collectAtMost(1, set()));
104+
assertEquals(set(1, 2, 3), list(1, 2, 3, 4, 5).collectAtMost(3, set()));
105+
assertEquals(set(1, 2, 3, 4, 5), list(1, 2, 3, 4, 5).collectAtMost(5, set()));
106+
}
107+
90108
public void testCollectAllMatching()
91109
{
92110
assertEquals(list(), list().collectAll(list(), x -> true));
@@ -104,26 +122,26 @@ public void testCollectAtMostMatching()
104122
assertEquals(list(1, 5), list(1, 3, 5, 7).collectAtMost(2, list(), x -> x != 3));
105123
}
106124

107-
public void testCollectAll()
125+
public void testTransformAll()
108126
{
109127
assertEquals(list(), list().transformAll(list(), x -> x));
110128
assertEquals(list(-1), list(1).transformAll(list(), x -> -x));
111129
assertEquals(list(-1, -3, -5), list(1, 3, 5).transformAll(list(), x -> -x));
112130
}
113131

114-
public void testCollectAtMost()
132+
public void testTransformAtMost()
115133
{
116134
assertEquals(list(), list().transformAtMost(10, list(), x -> x));
117135
assertEquals(list(-1, -3), list(1, 3, 5).transformAtMost(2, list(), x -> -x));
118136
}
119137

120-
public void testCollectSome()
138+
public void testTransformSome()
121139
{
122140
assertEquals(list(), list().transformSome(list(), x -> Holders.of(x)));
123141
assertEquals(list(9, -1, -5), list(1, 3, 5).transformSome(list(9), x -> x == 3 ? Holders.of() : Holders.of(-x)));
124142
}
125143

126-
public void testCollectAtMostSome()
144+
public void testTransformAtMostSome()
127145
{
128146
assertEquals(list(), list().transformAtMostSome(10, list(), x -> Holders.of(x)));
129147
assertEquals(list(9, -1, -5), list(1, 3, 5).transformAtMostSome(10, list(9), x -> x == 3 ? Holders.of() : Holders.of(-x)));
@@ -160,7 +178,7 @@ public void testInject()
160178

161179
public void testConversions()
162180
{
163-
assertEquals(JImmutables.set(3, 5), list(5, 3, 3, 5).transformAll(JImmutables.<Integer>sortedSet(), x -> x));
181+
assertEquals(set(3, 5), list(5, 3, 3, 5).transformAll(JImmutables.<Integer>sortedSet(), x -> x));
164182
assertEquals(JImmutables.multiset(3, 3, 5, 5), list(5, 3, 3, 5).transformAll(JImmutables.<Integer>sortedMultiset(), x -> x));
165183
assertEquals(JImmutables.map().assign(3, 6).assign(5, 10), list(5, 3, 3, 5).transformAll(JImmutables.sortedMap(), x -> MapEntry.of(x, 2 * x)));
166184
assertEquals(JImmutables.listMap().assign(3, list(3, 3)).assign(5, list(5)), list(3, 5, 3).transformAll(JImmutables.sortedListMap(), x -> MapEntry.of(x, x)));

0 commit comments

Comments
 (0)