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

io.github.benas.randombeans.EnhancedRandomImpl Maven / Gradle / Ivy

There is a newer version: 3.9.0
Show newest version
/*
 * The MIT License
 *
 *   Copyright (c) 2016, Mahmoud Ben Hassine ([email protected])
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

package io.github.benas.randombeans;

import io.github.benas.randombeans.api.EnhancedRandom;
import io.github.benas.randombeans.api.ObjectGenerationException;
import io.github.benas.randombeans.api.Randomizer;
import io.github.benas.randombeans.api.RandomizerRegistry;
import io.github.benas.randombeans.randomizers.misc.EnumRandomizer;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import static io.github.benas.randombeans.util.ReflectionUtils.*;

/**
 * The core implementation of the {@link EnhancedRandom} abstract class.
 *
 * @author Mahmoud Ben Hassine ([email protected])
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
class EnhancedRandomImpl extends EnhancedRandom {

    private long seed;

    private final FieldPopulator fieldPopulator;

    private final ArrayPopulator arrayPopulator;

    private final CollectionPopulator collectionPopulator;

    private final MapPopulator mapPopulator;

    private final RandomizerProvider randomizerProvider;

    private final ObjectFactory objectFactory;

    private final FieldExclusionChecker fieldExclusionChecker;

    EnhancedRandomImpl(final Set registries) {
        objectFactory = new ObjectFactory();
        randomizerProvider = new RandomizerProvider(registries);
        arrayPopulator = new ArrayPopulator(this, randomizerProvider);
        collectionPopulator = new CollectionPopulator(this, objectFactory);
        mapPopulator = new MapPopulator(this, objectFactory);
        fieldPopulator = new FieldPopulator(this, randomizerProvider, arrayPopulator, collectionPopulator, mapPopulator);
        fieldExclusionChecker = new FieldExclusionChecker();
    }

    @Override
    public  T nextObject(final Class type, final String... excludedFields) {
        return doPopulateBean(type, new PopulatorContext(excludedFields));
    }

    @Override
    public  Stream objects(final Class type, final int streamSize, final String... excludedFields) {
        if (streamSize < 0) {
            throw new IllegalArgumentException("The stream size must be positive");
        }
        Stream.Builder streamBuilder = Stream.builder();
        for (int i = 0; i < streamSize; i++) {
            T bean = nextObject(type, excludedFields);
            streamBuilder.add(bean);
        }
        return streamBuilder.build();
    }

     T doPopulateBean(final Class type, final PopulatorContext context) {
        T result;
        try {

            Randomizer randomizer = randomizerProvider.getRandomizerByType(type);
            if (randomizer != null) {
                return (T) randomizer.getRandomValue();
            }

            // Collection types are randomized without introspection for internal fields
            if (!isIntrospectable(type)) {
                return randomize(type, context);
            }

            // If the type has been already randomized, return one cached instance to avoid recursion.
            if (context.hasRandomizedType(type)) {
                return (T) context.getPopulatedBean(type);
            }

            // create a new instance of the target type
            result = objectFactory.createInstance(type);

            // cache instance in the population context
            context.addPopulatedBean(type, result);

            // retrieve declared and inherited fields
            List fields = getDeclaredFields(result);
            fields.addAll(getInheritedFields(type));

            // populate fields with random data
            populateFields(fields, result, context);

            return result;
        } catch (InstantiationError | Exception e) {
            throw new ObjectGenerationException("Unable to generate a random instance of type " + type, e);
        }
    }

    private  T randomize(final Class type, final PopulatorContext context) {
        if (isEnumType(type)) {
            return (T) new EnumRandomizer(type, seed).getRandomValue();
        }
        if (isArrayType(type)) {
            return (T) arrayPopulator.getRandomArray(type, context);
        }
        if (isCollectionType(type)) {
            return (T) collectionPopulator.getEmptyImplementationForCollectionInterface(type);
        }
        if (isMapType(type)) {
            return (T) mapPopulator.getEmptyImplementationForMapInterface(type);
        }
        return null;
    }

    private  void populateFields(final List fields, final T result, final PopulatorContext context) throws IllegalAccessException {
        for (final Field field : fields) {
            populateField(field, result, context);
        }
    }

    private  void populateField(final Field field, final T result, final PopulatorContext context) throws IllegalAccessException {
        if (!fieldExclusionChecker.shouldBeExcluded(field, context)) {
            fieldPopulator.populateField(result, field, context);
        }
    }

    /*
     * Setters for optional parameters
     */

    public void setScanClasspathForConcreteTypes(final boolean scanClasspathForConcreteTypes) {
        fieldPopulator.setScanClasspathForConcreteTypes(scanClasspathForConcreteTypes);
        objectFactory.setScanClasspathForConcreteTypes(scanClasspathForConcreteTypes);
    }

    @Override
    public void setSeed(final long seed) {
        super.setSeed(seed);
        this.seed = seed;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy