001/*
002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.test.generator.internal.net.java.quickcheck.generator;
017
018import de.cuioss.test.generator.internal.net.java.quickcheck.ExtendibleGenerator;
019import de.cuioss.test.generator.internal.net.java.quickcheck.Generator;
020import de.cuioss.test.generator.internal.net.java.quickcheck.ObjectGenerator;
021import de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution.Distribution;
022import lombok.experimental.UtilityClass;
023
024import java.util.Date;
025import java.util.concurrent.TimeUnit;
026
027/**
028 * {@link PrimitiveGenerators} contains factory methods for primitive value
029 * generators. These can be used to build custom test case generators.
030 * <p>
031 * The default distribution for generators is {@link Distribution#UNIFORM}.
032 * </p>
033 */
034@SuppressWarnings({"WeakerAccess"})
035@UtilityClass
036public class PrimitiveGenerators {
037
038    /**
039     * Create a new string generator.<br>
040     *
041     * The characters are from the Basic Latin and Latin-1 Supplement unicode
042     * blocks.
043     */
044    public static ExtendibleGenerator<Character, String> strings() {
045        return Generators.strings();
046    }
047
048    /**
049     * Create a new string generator which generates strings of characters ranging
050     * from lo to hi.
051     *
052     * @param lo lower boundary character
053     * @param hi upper boundary character
054     */
055    public static ExtendibleGenerator<Character, String> strings(char lo, char hi) {
056        return Generators.strings(lo, hi);
057    }
058
059    /**
060     * Create a new string generator which generates strings of characters from the
061     * given string.
062     */
063    public static ExtendibleGenerator<Character, String> strings(String allowedCharacters) {
064        return Generators.strings(allowedCharacters);
065    }
066
067    /**
068     * Create a new string generator which generates strings of characters from the
069     * given string with a length between min and max.
070     */
071    public static ExtendibleGenerator<Character, String> strings(String allowedCharacters, int min, int max) {
072        return Generators.strings(allowedCharacters, min, max);
073    }
074
075    /**
076     * Creates a new String generator which generates strings whose length ranges
077     * from zero to given length.
078     */
079    public static ExtendibleGenerator<Character, String> strings(int max) {
080        return Generators.strings(max);
081    }
082
083    /**
084     * Create a new string generator which generates strings of sizes ranging from
085     * loLength to hiLength.
086     *
087     * @param min lower size boundary
088     * @param max upper size boundary
089     */
090    public static ExtendibleGenerator<Character, String> strings(int min, int max) {
091        return Generators.strings(min, max);
092    }
093
094    /**
095     * Create a new string generator which creates strings of characters generated
096     * by the given character generator with a length generated by the length
097     * generator.
098     *
099     */
100    public static ExtendibleGenerator<Character, String> strings(Generator<Integer> length,
101            Generator<Character> characters) {
102        return Generators.strings(length, characters);
103    }
104
105    /**
106     * Create a new string generator which creates strings of characters generated
107     * by the given character generator.
108     *
109     */
110    public static ExtendibleGenerator<Character, String> strings(Generator<Character> characterGenerator) {
111        return Generators.strings(characterGenerator);
112    }
113
114    /**
115     * Create a new string generator which creates strings of characters from a-z
116     * and A-Z.
117     */
118    public static ExtendibleGenerator<Character, String> letterStrings() {
119        return Generators.letterStrings();
120    }
121
122    /**
123     * Create a new string generator which creates strings with sizes ranging from
124     * loLengh to hiLength of characters from a-z and A-Z.
125     */
126    public static ExtendibleGenerator<Character, String> letterStrings(int min, int max) {
127        return Generators.letterStrings(min, max);
128    }
129
130    /**
131     * Create a new string generator which creates strings of characters generated
132     * by {@link PrimitiveGenerators#basicLatinCharacters()} and
133     * {@link PrimitiveGenerators#latin1SupplementCharacters()}.
134     */
135    public static ExtendibleGenerator<Character, String> printableStrings() {
136        return Generators.printableStrings();
137    }
138
139    /**
140     * Create a new string generator for strings that are not empty.
141     */
142    public static ExtendibleGenerator<Character, String> nonEmptyStrings() {
143        return Generators.nonEmptyStrings();
144    }
145
146    /**
147     * Create a new string generator for substrings of a base string.
148     *
149     * <p>
150     * base.contains(generated string) will always be true.
151     * </p>
152     */
153    public static Generator<String> substrings(String base) {
154        return Generators.substrings(base);
155    }
156
157    /**
158     * Create a new string generator for substrings of a base string.
159     *
160     * <p>
161     * base.contains(generated string) will always be true.
162     * </p>
163     *
164     * @param size of the generated string
165     */
166    public static Generator<String> substrings(String base, int size) {
167        return Generators.substrings(base, size);
168    }
169
170    /**
171     * Create a new string generator for substrings of a base string.
172     *
173     * <p>
174     * base.contains(generated string) will always be true.
175     * </p>
176     *
177     * @param minSize is the minimum size of the generated string
178     * @param maxSize is the maximum size of the generated string
179     */
180    public static Generator<String> substrings(String base, int minSize, int maxSize) {
181        return Generators.substrings(base, minSize, maxSize);
182    }
183
184    /**
185     * Create a new character generator which generates characters ranging from lo
186     * to hi.
187     */
188    public static Generator<Character> characters(char lo, char hi) {
189        return Generators.characters(lo, hi);
190    }
191
192    /**
193     * Create a new character generator.<br>
194     *
195     * The characters are from the Basic Latin and Latin-1 Supplement unicode
196     * blocks.
197     */
198    public static Generator<Character> characters() {
199        return Generators.characters();
200    }
201
202    /**
203     * Create a new character generator which generates characters from the given
204     * character array.
205     */
206    public static Generator<Character> characters(Character... chars) {
207        return Generators.characters(chars);
208    }
209
210    /**
211     * Create a new character generator which generates characters from the given
212     * string.
213     */
214    public static Generator<Character> characters(String string) {
215        return Generators.characters(string);
216    }
217
218    /**
219     * Create a new character generator which generates characters from the given
220     * characters.
221     */
222    public static Generator<Character> characters(Iterable<Character> chars) {
223        return Generators.characters(chars);
224    }
225
226    /**
227     * Create a new character generator which generates latin-1 supplement
228     * characters.
229     */
230    public static Generator<Character> latin1SupplementCharacters() {
231        return Generators.latin1SupplementCharacters();
232    }
233
234    /**
235     * Create a new character generator which generates latin characters.
236     */
237    public static Generator<Character> basicLatinCharacters() {
238        return Generators.basicLatinCharacters();
239    }
240
241    /**
242     * Create a new integer generator which creates integers ranging from
243     * {@link Integer#MIN_VALUE} to {@link Integer#MAX_VALUE}.
244     *
245     */
246    public static Generator<Integer> integers() {
247        return Generators.integers();
248    }
249
250    /**
251     * Create a new integer generator which creates integers that are at equal or
252     * greater than low.
253     */
254    public static Generator<Integer> integers(int low) {
255        return Generators.integers(low);
256    }
257
258    /**
259     * Create a new integer generator which creates integers ranging from lo to hi.
260     */
261    public static Generator<Integer> integers(int lo, int hi) {
262        return Generators.integers(lo, hi);
263    }
264
265    /**
266     * Create a new integer generator which creates integers ranging from {@code lo}
267     * to {@code hi} based on the given {@link Distribution}.
268     */
269    public static Generator<Integer> integers(int lo, int hi, Distribution distribution) {
270        return Generators.integers(lo, hi, distribution);
271    }
272
273    /**
274     * Create a new integer generator which creates integers ranging from {@code 1}
275     * to {@link Integer#MAX_VALUE}.
276     */
277    public static Generator<Integer> positiveIntegers() {
278        return Generators.positiveIntegers();
279    }
280
281    /**
282     * Create a new integer generator which creates integers ranging from {@code 1}
283     * to {@code max} (which must be at least 1).
284     */
285    public static Generator<Integer> positiveIntegers(int hi) {
286        return Generators.positiveIntegers(hi);
287    }
288
289    /**
290     * Create a new byte generator which creates byte values ranging from
291     * {@link Byte#MIN_VALUE} to {@link Byte#MAX_VALUE}.
292     */
293    public static Generator<Byte> bytes() {
294        return Generators.bytes();
295    }
296
297    /**
298     * Create a new byte generator which creates byte values ranging from lo to hi.
299     */
300    public static Generator<Byte> bytes(byte lo, byte hi) {
301        return Generators.bytes(lo, hi);
302    }
303
304    /**
305     * Create a new integer generator which creates integers ranging from lo to hi
306     * based on the given {@link Distribution}.
307     */
308    public static Generator<Byte> bytes(byte lo, byte hi, Distribution distribution) {
309        return Generators.bytes(lo, hi, distribution);
310    }
311
312    /**
313     * Create a new long generator which creates longs ranging from
314     * {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE}.
315     */
316    public static Generator<Long> longs() {
317        return Generators.longs();
318    }
319
320    /**
321     * Create a new long generator which creates longs ranging from lo to hi.
322     */
323    public static Generator<Long> longs(long lo, long hi) {
324        return Generators.longs(lo, hi);
325    }
326
327    /**
328     * Create a new long generator which creates longs ranging from lo to hi based
329     * on the given {@link Distribution}.
330     */
331    public static Generator<Long> longs(long lo, long hi, Distribution distribution) {
332        return Generators.longs(lo, hi, distribution);
333    }
334
335    /**
336     * Create a new long generator which creates long values ranging from 1 to
337     * {@link Long#MAX_VALUE}.
338     */
339    public static Generator<Long> positiveLongs() {
340        return Generators.positiveLongs();
341    }
342
343    /**
344     * Create a new long generator which creates long values ranging from 1 to hi.
345     */
346    public static Generator<Long> positiveLongs(long hi) {
347        return Generators.positiveLongs(hi);
348    }
349
350    /**
351     * Create a new double generator which creates doubles ranging from
352     * {@link Double#MIN_VALUE} to {@link Double#MAX_VALUE}.
353     */
354    public static Generator<Double> doubles() {
355        return Generators.doubles();
356    }
357
358    /**
359     * Create a new double generator which creates doubles ranging from lo to hi.
360     */
361    public static Generator<Double> doubles(double lo, double hi) {
362        return Generators.doubles(lo, hi);
363    }
364
365    /**
366     * Create a new double generator which creates doubles ranging from lo to hi
367     * based on the given {@link Distribution}.
368     */
369    public static Generator<Double> doubles(double lo, double hi, Distribution distribution) {
370        return Generators.doubles(lo, hi, distribution);
371    }
372
373    /**
374     * Create a generator for boolean values.
375     */
376    public static Generator<Boolean> booleans() {
377        return Generators.booleans();
378    }
379
380    /**
381     * Create a generator for null values.
382     */
383    public static <T> Generator<T> nulls() {
384        return Generators.nulls();
385    }
386
387    /**
388     * Create a generator for date values.
389     */
390    public static Generator<Date> dates() {
391        return Generators.dates();
392    }
393
394    /**
395     * Create a generator for date values with the given precision.
396     */
397    public static Generator<Date> dates(TimeUnit precision) {
398        return Generators.dates(precision);
399    }
400
401    /**
402     * Create a generator for date values from low to high.
403     */
404    public static Generator<Date> dates(Date low, Date high) {
405        return Generators.dates(low, high);
406    }
407
408    /**
409     * Create a generator for date values from low to high.
410     */
411    public static Generator<Date> dates(long low, long high) {
412        return Generators.dates(low, high);
413    }
414
415    /**
416     * Create a generator for date values from low to high with the given precision.
417     */
418    public static Generator<Date> dates(Long low, Long high, TimeUnit precision) {
419        return Generators.dates(low, high, precision);
420    }
421
422    /**
423     * Create a generator for fixed value generator.
424     */
425    public static <T> Generator<T> fixedValues(T value) {
426        return Generators.fixedValues(value);
427    }
428
429    /**
430     * Create a fixed value generator return values array.
431     */
432    @SafeVarargs
433    public static <T> Generator<T> fixedValues(T... values) {
434        return Generators.fixedValues(values);
435    }
436
437    /**
438     * Create a fixed value generator return values.
439     */
440    public static <T> Generator<T> fixedValues(Iterable<T> values) {
441        return Generators.fixedValues(values);
442    }
443
444    /**
445     * A cloning generator which uses object serialization to create clones of the
446     * prototype object. For each call a new copy of the prototype will be
447     * generated.
448     */
449    public static <T> Generator<T> clonedValues(T prototype) {
450        return Generators.clonedValues(prototype);
451    }
452
453    /**
454     * Create a generator of enumeration values.
455     *
456     * @param <T>       Type of enumerations
457     * @param enumClass class of enumeration
458     * @return generator of enum values
459     */
460    public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass) {
461        return Generators.enumValues(enumClass);
462    }
463
464    /**
465     * Create a generator of enumeration values.
466     *
467     * @param <T>       Type of enumerations
468     * @param enumClass class of enumeration
469     * @param excluded  excluded values of enumeration
470     * @return generator of enum values
471     */
472    @SafeVarargs
473    public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass, T... excluded) {
474        return Generators.enumValues(enumClass, excluded);
475    }
476
477    /**
478     * Create a generator of enumeration values.
479     *
480     * @param <T>            Type of enumerations
481     * @param enumClass      class of enumeration
482     * @param excludedValues excluded values of enumeration
483     * @return generator of enum values
484     */
485    public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass, Iterable<T> excludedValues) {
486        return Generators.enumValues(enumClass, excludedValues);
487    }
488
489    /**
490     * Create a generator for {@link Object java.lang.Object} instances.
491     * <p>
492     * Note: every invocation of {@link Generator#next()} creates a new instance.
493     * </p>
494     */
495    public static Generator<Object> objects() {
496        return Generators.objects();
497    }
498
499    /**
500     * Create a generator from a {@link ObjectGenerator declarative object generator
501     * definition}.
502     */
503    public static <T> ObjectGenerator<T> objects(Class<T> objectType) {
504        return Generators.objects(objectType);
505    }
506
507    /**
508     * Create a generator from a {@link ObjectGenerator declarative object generator
509     * definition}.
510     * <p>
511     * Default values will be used for all {@link ObjectGenerator#on(Object)
512     * undefined methods}.
513     * </p>
514     */
515    public static <T> ObjectGenerator<T> defaultObjects(Class<T> objectType) {
516        return Generators.defaultObjects(objectType);
517    }
518}