Search code examples
javalambdarefactoringgeneric-programmingcode-duplication

Pass a function with lamba expression to make a generic function


I am using google's AutoValue to generate some configs, but before generating the configs, I would like to make sure that the entries are sanitized and default values are added to the list.

The AutoValue part looks like:

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder primaryName(List<String> value);

        public abstract Optional<List<String>> primaryName();

        public abstract Builder primaryTitle(List<String> value);

        abstract Optional<List<String>> primaryTitle();

        abstract Config autoBuild();

        public Config build() {

            normalizePriorities();
            EntitySourcePrioritizationConfig config = autoBuild();
            return config;
        }

I have the following code that is repeated in normalizePriorities():

        private void normalizePriorities()
        {
            normalizeName();
            normalizeTitle();
        }

        private void normalizeName() {
            if (!primaryName().isPresent()) {
                primaryName(defaultPrimaryNames());
            } else {
                List<String> providedConfigEntries = primaryName().get();
                List<String> cleanConfig = sanitizeConfig(providedConfigEntries);
                primaryName(cleanConfig);
            }
        }

        private void normalizeTitle() {
            if (!primaryTitle().isPresent()) {
                primaryTitle(defaultPrimaryTitles());
            } else {
                List<String> providedConfigEntries = primaryTitle().get();
                List<String> cleanConfig = sanitizeConfig(providedConfigEntries);
                primaryTitle(cleanConfig);
            }
        }

I was wondering how I could use lambda expressions in order to reduce the deduplication of code.

The default names and titles are just a list of Strings as follows that could be passed as parameters:

    public static ImmutableList<String> defaultPrimaryTitles() {
        return ImmutableList.of(
                "BBA",
                "TNN");
    }

    public static ImmutableList<String> defaultPrimaryNames() {
        return ImmutableList.of(
                "SNS Inc.",
                "ABC Corp.");
    }

I have tried to generify the function like so:

normalize(primaryAlias(), defaultPrimaryTitles());

private void normalize(Optional<List<String>> configList, List<String> defaultConfig){

...            
}

Unfortunately, I am not too sure how to generify and pass public abstract Builder primaryTitle(List<String> value) into the method.


Solution

  • You could pass a Consumer that accepts a list of strings and calls the builder methods, e.g.:

    Create the consumer and pass it to the normalize method:

    Consumer<List<String>> builderConsumer = (x) -> Builder.primaryName(x);
    normalize(primaryAlias(), defaultPrimaryTitles(), builderConsumer);
    

    And the normalize method:

    private void normalize(ImmutableList<String> configList, List<String> defaultConfig, Consumer<List<String>> builderConsumer) {
        List<String> cleanConfig = new ArrayList<>();
        // ...
        builderConsumer.accept(cleanConfig);
    }