Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RandomizerProvider API for randomizer selection #350

Closed
benas opened this issue Mar 14, 2019 · 1 comment

Comments

1 participant
@benas
Copy link
Collaborator

commented Mar 14, 2019

As of v3.9, setting a custom randomizer to be used for a given field was possible through the EnhancedRandomBuilder.randomize(Predicate<Field> predicate, Randomizer<?> randomizer) method or by specifying a custom RandomizerRegistry. Both approaches are static and independent from any randomization context.

The idea of this new feature is to introduce a strategy interface that allows for randomizer selection dynamically based on the randomization context. This new API could be something like:

/**
 * Strategy interface to provide randomizers for field/type based on the current context.
 * Implementations may (or may not) use registries to provide randomizers.
 *
 * The added value of this interface compared to a simple {@link RandomizerRegistry} is that
 * it gives access to the current context and allows fine grained ranomizer selection based on that context.
 */
public interface RandomizerProvider {

    /**
     * Return a randomizer for the given field in the current context.
     *
     * @param field for which a randomizer should be returned
     * @param context current randomization context
     * @return a randomizer for the given field in the current context.
     */
    default Randomizer<?> getRandomizerByField(final Field field, final RandomizerContext context) {
        return null;
    }

    /**
     * Return a randomizer for the given type in the current context.
     *
     * @param type for which a randomizer should be returned
     * @param context current randomization context
     * @param <T> generic type
     * @return a randomizer for the given type in the current context.
     */
    default <T> Randomizer<T> getRandomizerByType(final Class<T> type, final RandomizerContext context) {
        return null;
    }

    /**
     * Set randomizer registries.
     *
     * @param randomizerRegistries to set
     */
    default void setRandomizerRegistries(Set<RandomizerRegistry> randomizerRegistries) {

    }

}

@benas benas added this to the 4.0.0.RC1 milestone Mar 14, 2019

@benas

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 14, 2019

Implemented in f3498af .

@benas benas closed this Mar 14, 2019

@benas benas added the feature label Mar 19, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.