1. Project Clover database Wed Nov 12 2025 05:07:35 UTC
  2. Package guru.mikelue.foxglove.functional

File RoundRobinValueSupplier.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
53% of files have more coverage

Code metrics

0
14
5
1
93
45
5
0.36
2.8
5
1

Classes

Class Line # Actions
RoundRobinValueSupplier 13 14 0% 5 3
0.8421052784.2%
 

Contributing tests

This file is covered by 35 tests. .

Source view

1    package guru.mikelue.foxglove.functional;
2   
3    import java.util.List;
4    import java.util.stream.Stream;
5   
6    import org.apache.commons.lang3.Validate;
7   
8    /**
9    * Supplies values in round-robin manner from the given list.
10    *
11    * @param <T> The type of value supplied
12    */
 
13    public class RoundRobinValueSupplier<T> implements StatefulSupplier<T> {
14    /**
15    * Constructs the supplier with at least two values.
16    *
17    * @param <T> The type of value supplied
18    * @param values The values to be supplied in round-robin manner
19    *
20    * @return The constructed supplier
21    *
22    * @see #of(List)
23    */
 
24  21 toggle @SuppressWarnings("unchecked")
25    public static <T> RoundRobinValueSupplier<T> of(T... values)
26    {
27  21 Validate.notEmpty(values, "Values for round-robin supplier cannot be null or empty");
28   
29  21 var newArray = (T[])new Object[values.length];
30  21 System.arraycopy(values, 0, newArray, 0, values.length);
31   
32  21 return new RoundRobinValueSupplier<>(newArray);
33    }
34   
35    /**
36    * Constructs the supplier with at least two values from the given stream.
37    *
38    * @param <T> The type of value supplied
39    * @param values The stream of values
40    *
41    * @return The constructed supplier
42    *
43    * @see #of(List)
44    */
 
45  0 toggle @SuppressWarnings("unchecked")
46    public static <T> RoundRobinValueSupplier<T> of(Stream<? extends T> values)
47    {
48  0 Validate.notNull(values, "Values for round-robin supplier cannot be null");
49   
50  0 return new RoundRobinValueSupplier<>(
51    (T[])values.toArray()
52    );
53    }
54   
55    /**
56    * Constructs the supplier with at least two values from the given list.
57    *
58    * @param <T> The type of value supplied
59    * @param values The list of values
60    *
61    * @return The constructed supplier
62    *
63    * @see #of(Object...)
64    */
 
65  44 toggle @SuppressWarnings("unchecked")
66    public static <T> RoundRobinValueSupplier<T> of(List<? extends T> values)
67    {
68  44 Validate.notNull(values, "Values for round-robin supplier cannot be null");
69   
70  44 return new RoundRobinValueSupplier<>(
71    (T[])values.toArray()
72    );
73    }
74   
75    private final T[] values;
76    private int currentIndex = 0;
77   
 
78  65 toggle private RoundRobinValueSupplier(T[] values)
79    {
80  65 Validate.notNull(values, "Values for round-robin supplier cannot be null");
81  65 Validate.notEmpty(values, "At least one value are required for round-robin supplier");
82   
83  65 this.values = values;
84    }
85   
 
86  966 toggle @Override
87    public T get()
88    {
89  966 T value = values[currentIndex];
90  966 currentIndex = (currentIndex + 1) % values.length;
91  966 return value;
92    }
93    }