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

File Suppliers.java

 

Coverage histogram

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

Code metrics

4
11
5
1
122
38
7
0.64
2.2
5
1.4

Classes

Class Line # Actions
Suppliers 11 11 0% 7 2
0.990%
 

Contributing tests

This file is covered by 33 tests. .

Source view

1    package guru.mikelue.foxglove.functional;
2   
3    import java.util.function.Supplier;
4   
5    import org.instancio.Instancio;
6    import org.instancio.generator.ValueSpec;
7   
8    /**
9    * Provides utility methods for {@link Supplier}.
10    */
 
11    public interface Suppliers {
12    /**
13    * Builds a {@link Supplier} which returns the default value based on dice
14    * rolling.
15    *
16    * <p>
17    *
18    * The returned {@link Supplier} will return the {@code defaultValue} when the dice rolls to 1;
19    * otherwise, it will return the value from the {@code baseSupplier}.
20    *
21    * @param <T> The type of supplied value
22    *
23    * @param baseSupplier The base {@link Supplier} to get value when the dice
24    * roll is not 1
25    * @param diceSides The number of sides of dice
26    * @param defaultValue The default value to return when the dice roll is 1
27    *
28    * @return The built {@link Supplier}
29    *
30    * @see #rollingSupplier(Supplier)
31    * @see #rollingSupplier(Supplier, int)
32    */
 
33  5 toggle static <T> Supplier<T> rollingSupplier(Supplier<? extends T> baseSupplier, int diceSides, T defaultValue) {
34  5 final ValueSpec<Integer> diceRollSpec = Instancio.gen().ints()
35    .range(1, diceSides);
36   
37  5 return () -> {
38  44 if (diceRollSpec.get() == 1) {
39  24 return defaultValue;
40    }
41   
42  20 return baseSupplier.get();
43    };
44    }
45   
46    /**
47    * Builds a {@link Supplier} which returns {@code null} based on dice rolling.
48    *
49    * <p>
50    *
51    * The returned {@link Supplier} will return the {@code null} when the dice rolls to 1;
52    * otherwise, it will return the value from the {@code baseSupplier}.
53    *
54    * @param <T> The type of supplied value
55    *
56    * @param baseSupplier The base {@link Supplier} to get value when the dice
57    * roll is not 1
58    * @param diceSides The number of sides of dice
59    *
60    * @return The built {@link Supplier}
61    *
62    * @see #rollingSupplier(Supplier, int, Object)
63    */
 
64  4 toggle static <T> Supplier<T> rollingSupplier(Supplier<? extends T> baseSupplier, int diceSides)
65    {
66  4 return rollingSupplier(baseSupplier, diceSides, null);
67    }
68   
69    /**
70    * Builds a {@link Supplier} which returns {@code null} based on dice rolling.
71    *
72    * The number of sides of dice is 6 by default.
73    *
74    * <p>
75    *
76    * The returned {@link Supplier} will return the {@code null} when the dice rolls to 1;
77    * otherwise, it will return the value from the {@code baseSupplier}.
78    *
79    * @param <T> The type of supplied value
80    *
81    * @param baseSupplier The base {@link Supplier} to get value when the dice
82    * roll is not 1
83    *
84    * @return The built {@link Supplier}
85    *
86    * @see #rollingSupplier(Supplier, int, Object)
87    */
 
88  0 toggle static <T> Supplier<T> rollingSupplier(Supplier<? extends T> baseSupplier)
89    {
90  0 return rollingSupplier(baseSupplier, 6, null);
91    }
92   
93    /**
94    * Builds a {@link Supplier} which initializes the target {@link Supplier} lazily.
95    *
96    * <p>
97    * The returned {@link Supplier} will invoke the {@code lazySupplier} only once
98    * to get the target {@link Supplier}. Subsequent invocations will use the
99    * target {@link Supplier} directly.
100    *
101    * @param <T> The type of supplied value
102    * @param lazySupplier The supplier of target {@link Supplier}
103    *
104    * @return The built {@link Supplier}
105    */
 
106  49 toggle static <T> Supplier<T> lazySupplier(Supplier<? extends Supplier<? extends T>> lazySupplier) {
107  49 return new Supplier<>() {
108    private Supplier<T> targetSupplier = null;
109   
 
110  939 toggle @Override
111    @SuppressWarnings("unchecked")
112    public T get()
113    {
114  939 if (targetSupplier == null) {
115  49 targetSupplier = (Supplier<T>)lazySupplier.get();
116    }
117   
118  939 return targetSupplier.get();
119    }
120    };
121    }
122    }