summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java
blob: 662b1bf9ede40e24367c43d440176c3501aba2a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package bjc.utils.funcutils;

import java.util.*;
import java.util.function.*;

import bjc.data.*;

/**
 * Utility methods for dealing with iterators.
 *
 * @author bjculkin
 *
 */
public class IteratorUtils {
	/**
	 * Convert an iterator to an iterable.
	 * 
	 * @param <E> The type being iterated over.
	 *
	 * @param itr
	 *            The iterator to convert.
	 *
	 * @return An iterable that gives back that iterator.
	 */
	public static <E> Iterable<E> I(Iterator<E> itr) {
		return () -> itr;
	}

	/**
	 * Convert an iterable to an iterator.
	 *
	 * @param <E> The type being iterated over.
	 * 
	 * @param itr
	 *            The iterable to convert.
	 *
	 * @return The iterator from that iterable
	 */
	public static <E> Iterator<E> I(Iterable<E> itr) {
		return itr.iterator();
	}

	/**
	 * Convert an array to an iterator.
	 *
	 * @param <E> The type being iterated over.
	 * 
	 * @param parms
	 *              The array to iterate over.
	 *
	 * @return An iterator over the provided array.
	 */
	@SafeVarargs
	public static <E> Iterator<E> I(E... parms) {
		return new ArrayIterator<>(parms);
	}

	/**
	 * Create a chain iterator.
	 * 
	 * @param <A> The initial type being iterated over.
	 * @param <B> The resulting type being iterated over.
	 *
	 * @param itrA
	 *             The iterator for input values.
	 *
	 * @param itrB
	 *             The transformation for output values.
	 *
	 * @return A chain iterator from the provided values.
	 */
	public static <A, B> Iterator<B> chain(Iterator<A> itrA,
			Function<A, Iterator<B>> itrB) {
		return new ChainIterator<>(itrA, itrB);
	}
	
	/**
	 * Perform a left-fold over an iterator.
	 * 
	 * @param <ElementType> The type of elements in the iterator.
	 * @param <ResultType> The result from the fold.
	 * 
	 * @param itr The items to iterate over.
	 * @param zero The initial element for the fold.
	 * @param folder The function that does the folding.
	 * 
	 * @return The result of folding over the iterator.
	 */
	public static <ElementType, ResultType> ResultType foldLeft(
			Iterable<ElementType> itr,
			ResultType zero,
			BiFunction<ElementType, ResultType, ResultType> folder)
	{
		ResultType state = zero;
		for (ElementType elem : itr) {
			state = folder.apply(elem, state);
		}
		return state;
	}
	
	/**
	 * Creates an 'entangled' pair of a consumer and an iterator.
	 * 
	 * @param <ElementType> The type of value involved.
	 * 
	 * @return A pair consisting of a consumer of values, and an iterator that
	 *         yields the consumed values.
	 */
	public static <ElementType>
	Pair<Consumer<ElementType>, Iterator<ElementType>> entangle()
	{
		Queue<ElementType> backer = new ArrayDeque<>();
		return Pair.pair(backer::add, new QueueBackedIterator<>(backer));
	}
}