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 ExclusionPolicy API for field/type exclusion #349

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, field/type exclusion was possible through EnhancedRandomBuilder.exclude methods:

  • public EnhancedRandomBuilder excludeField(Predicate<Field> predicate)
  • public EnhancedRandomBuilder excludeType(Predicate<Class<?>> predicate)

Those methods take a predicate which can be something like:

FieldPredicates.named("name").and(ofType(String.class)).and(inClass(Human.class)

This field definition is static and independent from any randomization context.

The idea of this new feature is to introduce a strategy interface that allows for field/type exclusion dynamically depending on the randomization context. This new API could be something like:

/**
 * Strategy interface for field/type exclusion.
 */
public interface ExclusionPolicy {

    /**
     * Given the current randomization context, should the field be excluded?
     *
     * @param field the field to check
     * @param context the current randomization context
     * @return true if the field should be excluded, false otherwise
     */
    boolean shouldBeExcluded(final Field field, final RandomizerContext context);

    /**
     * Given the current randomization context, should the type be excluded?
     *
     * @param type the type to check
     * @param context the current randomization context
     * @return true if the type should be excluded, false otherwise
     */
    boolean shouldBeExcluded(final Class<?> type, final RandomizerContext context);

}

@benas benas added the feature label Mar 14, 2019

@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 8ae7150 .

@benas benas closed this Mar 14, 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.