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

org.meanbean.factories.collections.MapFactoryBase Maven / Gradle / Ivy

Go to download

Mean Bean is an open source Java test library that tests equals and hashCode contract compliance, as well as JavaBean/POJO getter and setter methods.

There is a newer version: 2.0.3
Show newest version
package org.meanbean.factories.collections;

import java.util.Map;

import org.meanbean.factories.basic.RandomFactoryBase;
import org.meanbean.lang.Factory;
import org.meanbean.util.RandomValueGenerator;

/**
 * Abstract Factory that creates Maps of objects of the specified type.
 * 
 * @param 
 *            The data type of the keys the Maps created by this Factory use.
 * @param 
 *            The data type of the values the Maps created by this Factory contain.
 */
public abstract class MapFactoryBase extends RandomFactoryBase> {

    /** Unique version ID of this Serializable class. */
    private static final long serialVersionUID = 1L;
    
    /** Factory used to create each Map key. */
    private final Factory keyFactory;
    
    /** Factory used to create each Map value. */
    private final Factory valueFactory;
    
    /**
     * Construct a new Map object Factory.
     * 
     * @param randomValueGenerator
     *            A random value generator used by the Factory to generate random values.
     * @param keyFactory
     *            Factory used to create each Map key.
     * @param valueFactory
     *            Factory used to create each Map value.
     */
    public MapFactoryBase(RandomValueGenerator randomValueGenerator, Factory keyFactory, Factory valueFactory) {
        super(randomValueGenerator);
        validationHelper.ensureExists("keyFactory", "construct MapFactory", keyFactory);
        validationHelper.ensureExists("valueFactory", "construct MapFactory", valueFactory);
        this.keyFactory = keyFactory;
        this.valueFactory = valueFactory;
    }

    /**
     * Create a new Map of objects of the specified type.
     * 
     * @return A new Map of object of the specified type.
     */
    public Map create() {
        // Basis to randomly decide size of Map
        double randomSize = getRandomValueGenerator().nextDouble();
        int size = (int)(100.0 * randomSize);
        Map result = createMap();
        for (int idx = 0; idx < size; idx++) {
            result.put(keyFactory.create(), valueFactory.create());
        }
        return result;
    }

	/**
	 * Create a new concrete Map instance that this Factory will return populated.
	 * 
	 * @return A concrete Map of the type created by this Factory.
	 */
	protected abstract Map createMap();
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy