-1

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.

QuirkyBit
  • 654
  • 7
  • 20
  • What's the significance of having `public abstract Builder primaryTitle(List value);` overloaded with `abstract Optional> primaryTitle();`? What are they being used for? – Naman Sep 21 '19 at 02:39
  • This is how AutoValue works: https://github.com/google/auto/blob/master/value/userguide/builders-howto.md#optional – QuirkyBit Sep 21 '19 at 02:44

1 Answers1

1

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);
}
user7217806
  • 2,014
  • 2
  • 10
  • 12
  • 1
    Thank you very much! I was trying to use Supplier, but I couldn't make the parameters to work out. Consumer worked as a charm! Thank you very much again! – QuirkyBit Sep 21 '19 at 11:44