| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| RowIndexToValueSupplier | 12 | 6 | 0% | 4 | 2 |
| 1 | package guru.mikelue.foxglove.functional; | |
| 2 | ||
| 3 | import java.util.function.Supplier; | |
| 4 | ||
| 5 | /** | |
| 6 | * This is a stateful {@link Supplier}, which need client code to implements {@link RowIndexToValue#apply} method. | |
| 7 | * | |
| 8 | * The value of row index is maintained internally which is fed to {@link RowIndexToValue#apply}. | |
| 9 | * | |
| 10 | * @param <T> Type of value to be provided | |
| 11 | */ | |
| 12 | public class RowIndexToValueSupplier<T> implements StatefulSupplier<T> { | |
| 13 | private final RowIndexToValue<T> toValueFunc; | |
| 14 | private int currentRow = 0; | |
| 15 | ||
| 16 | /** | |
| 17 | * Creates the supplier with given function to get value at specified row index. | |
| 18 | * | |
| 19 | * @param <T> The type of value supplied | |
| 20 | * @param toValueFunc The function to get value at specified row index | |
| 21 | * | |
| 22 | * @return The created supplier | |
| 23 | */ | |
| 24 | 4 | public static <T> RowIndexToValueSupplier<T> of(RowIndexToValue<T> toValueFunc) |
| 25 | { | |
| 26 | 4 | return new RowIndexToValueSupplier<>(toValueFunc); |
| 27 | } | |
| 28 | ||
| 29 | 4 | private RowIndexToValueSupplier(RowIndexToValue<T> toValueFunc) |
| 30 | { | |
| 31 | 4 | this.toValueFunc = toValueFunc; |
| 32 | } | |
| 33 | ||
| 34 | /** | |
| 35 | * Gets the value at current row index, then advances the row index by one. | |
| 36 | * | |
| 37 | * @return The value at current row index | |
| 38 | */ | |
| 39 | 19 | @Override |
| 40 | public T get() | |
| 41 | { | |
| 42 | 19 | var value = toValueFunc.apply(currentRow); |
| 43 | 19 | currentRow++; |
| 44 | ||
| 45 | 19 | return value; |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Gets the the row index to be used for next time calling of {@link #get()} . | |
| 50 | * | |
| 51 | * @return The value at specified row index | |
| 52 | */ | |
| 53 | 0 | public int getCurrentIndex() |
| 54 | { | |
| 55 | 0 | return currentRow; |
| 56 | } | |
| 57 | } |