All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.marksman.composablerandom.Collections Maven / Gradle / Ivy

package dev.marksman.composablerandom;

import dev.marksman.collectionviews.ImmutableNonEmptyVector;
import dev.marksman.collectionviews.ImmutableVector;
import dev.marksman.collectionviews.NonEmptyVector;

import java.util.*;

import static com.jnape.palatable.lambda.functions.builtin.fn2.Zip.zip;
import static dev.marksman.composablerandom.Generator.*;

class Collections {

    static  Generator> generateList(Generator g) {
        return sized(n -> buildArrayList(n, g));
    }

    static  Generator> generateNonEmptyList(Generator g) {
        return sized(n -> buildArrayList(Math.max(1, n), g));
    }

    static  Generator> generateListOfN(int n, Generator g) {
        if (n < 0) {
            throw new IllegalArgumentException("n must be >= 0");
        }
        return buildArrayList(n, g);
    }

    static  Generator> generateSet(Generator g) {
        return sized(n -> buildHashSet(n, g));
    }

    static  Generator> generateNonEmptySet(Generator g) {
        return sized(n -> buildHashSet(Math.max(1, n), g));
    }

    static  Generator> generateVector(Generator g) {
        return sized(n -> buildVector(n, g));
    }

    static  Generator> generateNonEmptyVector(Generator g) {
        return sized(n -> buildNonEmptyVector(Math.max(1, n), g));
    }

    static  Generator> generateVectorOfN(int n, Generator g) {
        if (n < 0) {
            throw new IllegalArgumentException("n must be >= 0");
        }
        return buildVector(n, g);
    }

    static  Generator> generateNonEmptyVectorOfN(int n, Generator g) {
        if (n < 1) {
            throw new IllegalArgumentException("n must be >= 1");
        }
        return buildNonEmptyVector(n, g);
    }

    static  Generator> generateMap(Generator keyGenerator,
                                                   Generator valueGenerator) {
        return Generator.sized(n -> generateMapOfN(n, keyGenerator, valueGenerator));
    }

    static  Generator> generateNonEmptyMap(Generator keyGenerator,
                                                           Generator valueGenerator) {
        return Generator.sized(n -> generateMapOfN(Math.max(1, n), keyGenerator, valueGenerator));
    }

    static  Generator> generateMap(Collection keys,
                                                   Generator valueGenerator) {
        return generateMapImpl(keys.size(), keys, valueGenerator);
    }

    static  Generator> generateMap(NonEmptyVector keys,
                                                   Generator valueGenerator) {
        return generateMapImpl((int) keys.size(), keys, valueGenerator);
    }

    private static  Generator> buildArrayList(int n, Generator generator) {
        return buildCollection(ArrayList::new, n, generator);
    }

    private static  Generator> buildHashSet(int n, Generator generator) {
        return buildCollection(HashSet::new, n, generator);
    }

    private static  Generator> generateMapImpl(int size,
                                                               Iterable keys,
                                                               Generator valueGenerator) {
        return generateListOfN(size, valueGenerator)
                .fmap(values -> {
                    HashMap result = new HashMap<>();
                    zip(keys, values)
                            .forEach(t -> result.put(t.getKey(), t.getValue()));
                    return result;
                });
    }

    private static  Generator> generateMapOfN(int n, Generator keyGenerator, Generator valueGenerator) {
        return generateListOfN(n, keyGenerator)
                .flatMap(keys -> generateMapImpl(keys.size(), keys, valueGenerator));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy