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

File ByteArraySpec.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
20
8
1
115
60
13
0.65
2.5
8
1.62

Classes

Class Line # Actions
ByteArraySpec 12 20 0% 13 0
1.0100%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package guru.mikelue.foxglove.instancio;
2   
3    import java.util.function.Supplier;
4   
5    import org.instancio.Instancio;
6    import org.instancio.generator.ValueSpec;
7    import org.instancio.generator.specs.ByteSpec;
8   
9    /**
10    * Provides a {@link ValueSpec} for byte array({@code byte[]}).
11    */
 
12    public class ByteArraySpec implements ValueSpec<byte[]> {
13    private boolean nullable = false;
14    private ByteSpec byteSpec = Instancio.gen().bytes();
15    private int minLength = 16;
16    private int maxLength = 128;
17    private int length = -1;
18   
19    private Supplier<Byte> byteSupplier = byteSpec;
20   
21    /**
22    * Constructs the byte array spec.
23    */
 
24  31 toggle public ByteArraySpec() {}
25   
26    /**
27    * Sets the length of the array.
28    *
29    * @param length the exact length of the array
30    *
31    * @return this instance
32    */
 
33  17 toggle public ByteArraySpec length(int length)
34    {
35  17 this.length = length;
36  17 return this;
37    }
38   
39    /**
40    * Sets the minimum length of the array (inclusive).
41    *
42    * @param min minimum length
43    *
44    * @return this instance
45    */
 
46  13 toggle public ByteArraySpec minLength(int min)
47    {
48  13 this.minLength = min;
49  13 return this;
50    }
51   
52    /**
53    * Sets the maximum length of the array (inclusive).
54    *
55    * @param max maximum length
56    *
57    * @return this instance
58    */
 
59  13 toggle public ByteArraySpec maxLength(int max)
60    {
61  13 this.maxLength = max;
62  13 return this;
63    }
64   
65    /**
66    * Sets whether zero elements (0x00) should be generated.
67    *
68    * @return this instance
69    */
 
70  1 toggle public ByteArraySpec zeroElements()
71    {
72  1 this.byteSpec = this.byteSpec.nullable();
73  1 this.byteSupplier = this::nullableByte;
74   
75  1 return this;
76    }
77   
78    private final static ValueSpec<Integer> diceSpec = Instancio.gen().ints().range(1, 6);
79   
80    /**
81    * {@inheritDoc}
82    */
 
83  696 toggle @Override
84    public byte[] get()
85    {
86  696 if (nullable && diceSpec.get() == 1) {
87  1 return null;
88    }
89   
90  695 int arrayLength = length > 0 ? length : Instancio.gen().ints().range(minLength, maxLength).get();
91  695 byte[] result = new byte[arrayLength];
92  230433 for (int i = 0; i < arrayLength; i++) {
93  229738 result[i] = byteSupplier.get();
94    }
95   
96  695 return result;
97    }
98   
99    /**
100    * {@inheritDoc}
101    */
 
102  1 toggle @Override
103    public ValueSpec<byte[]> nullable()
104    {
105  1 nullable = true;
106  1 return this;
107    }
108   
 
109  512 toggle private byte nullableByte()
110    {
111  512 Byte v = byteSpec.get();
112  512 return v != null ? v : 0x00;
113    }
114    }
115