Skip to content

Commit

Permalink
Use more fluent streams constructs.
Browse files Browse the repository at this point in the history
Previously we avoided collectors like `toImmutableList()` because of Google-internal problems where sometimes we would end up with a version of Guava that didn't include those methods. Now we have copies of the methods in `MoreStreams` so we can use those.

RELNOTES=n/a
PiperOrigin-RevId: 381898090
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Jun 28, 2021
1 parent ba9f110 commit c68b2ff
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

import static com.google.auto.common.MoreElements.asExecutable;
import static com.google.auto.common.MoreElements.asPackage;
import static com.google.auto.common.MoreStreams.toImmutableMap;
import static com.google.auto.common.MoreStreams.toImmutableSet;
import static com.google.auto.common.SuperficialValidation.validateElement;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Multimaps.filterKeys;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static javax.lang.model.element.ElementKind.PACKAGE;
import static javax.tools.Diagnostic.Kind.ERROR;

Expand Down Expand Up @@ -162,14 +161,14 @@ private ImmutableSet<TypeElement> getSupportedAnnotationTypeElements() {
checkState(steps != null);
return steps.stream()
.flatMap(step -> getSupportedAnnotationTypeElements(step).stream())
.collect(collectingAndThen(toList(), ImmutableSet::copyOf));
.collect(toImmutableSet());
}

private ImmutableSet<TypeElement> getSupportedAnnotationTypeElements(Step step) {
return step.annotations().stream()
.map(elements::getTypeElement)
.filter(Objects::nonNull)
.collect(collectingAndThen(toList(), ImmutableSet::copyOf));
.collect(toImmutableSet());
}

/**
Expand All @@ -181,7 +180,7 @@ public final ImmutableSet<String> getSupportedAnnotationTypes() {
checkState(steps != null);
return steps.stream()
.flatMap(step -> step.annotations().stream())
.collect(collectingAndThen(toList(), ImmutableSet::copyOf));
.collect(toImmutableSet());
}

@Override
Expand Down Expand Up @@ -478,10 +477,8 @@ private static class ProcessingStepAsStep implements Step {
this.annotationsByName =
processingStep.annotations().stream()
.collect(
collectingAndThen(
toMap(
Class::getCanonicalName, (Class<? extends Annotation> aClass) -> aClass),
ImmutableMap::copyOf));
toImmutableMap(
Class::getCanonicalName, (Class<? extends Annotation> aClass) -> aClass));
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions common/src/main/java/com/google/auto/common/MoreElements.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.google.auto.common;

import static com.google.auto.common.MoreStreams.toImmutableSet;
import static javax.lang.model.element.ElementKind.PACKAGE;
import static javax.lang.model.element.Modifier.STATIC;

Expand Down Expand Up @@ -482,9 +483,9 @@ private static ImmutableSet<ExecutableElement> getAllMethods(
}
}
}
Set<ExecutableElement> methods = new LinkedHashSet<ExecutableElement>(methodMap.values());
methods.removeAll(overridden);
return ImmutableSet.copyOf(methods);
return methodMap.values().stream()
.filter(m -> !overridden.contains(m))
.collect(toImmutableSet());
}

// Add to `methods` the static and instance methods from `type`. This means all methods from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void testGwtArraysAreCloned() {
assertThat(arrays.ints()).asList().containsExactly(2, 3, 5).inOrder();
strings[0] = "Hyde";
ints[0] = -1;
assertThat(ImmutableList.copyOf(arrays.strings())).containsExactly("Jekyll");
assertThat(arrays.strings()).asList().containsExactly("Jekyll");
assertThat(arrays.ints()).asList().containsExactly(2, 3, 5).inOrder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package com.google.auto.value.processor;

import static com.google.auto.common.MoreElements.getLocalAndInheritedMethods;
import static com.google.auto.common.MoreStreams.toImmutableList;
import static com.google.auto.value.processor.ClassNames.AUTO_VALUE_NAME;
import static com.google.common.collect.Sets.difference;
import static com.google.common.collect.Sets.intersection;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

import com.google.auto.service.AutoService;
import com.google.auto.value.extension.AutoValueExtension;
Expand All @@ -33,7 +33,6 @@
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -100,10 +99,9 @@ public AutoValueProcessor(Iterable<? extends AutoValueExtension> extensions) {

@VisibleForTesting
static ImmutableList<AutoValueExtension> extensionsFromLoader(ClassLoader loader) {
return ImmutableList.copyOf(
Iterables.filter(
SimpleServiceLoader.load(AutoValueExtension.class, loader),
ext -> !ext.getClass().getName().equals(OLD_MEMOIZE_EXTENSION)));
return SimpleServiceLoader.load(AutoValueExtension.class, loader).stream()
.filter(ext -> !ext.getClass().getName().equals(OLD_MEMOIZE_EXTENSION))
.collect(toImmutableList());
}

@Override
Expand Down Expand Up @@ -428,9 +426,8 @@ private void defineVarsForType(
ImmutableMap<ExecutableElement, TypeMirror> propertyMethodsAndTypes,
Optional<BuilderSpec.Builder> maybeBuilder) {
ImmutableSet<ExecutableElement> propertyMethods = propertyMethodsAndTypes.keySet();
// We can't use ImmutableList.toImmutableList() for obscure Google-internal reasons.
vars.toBuilderMethods =
ImmutableList.copyOf(toBuilderMethods.stream().map(SimpleMethod::new).collect(toList()));
toBuilderMethods.stream().map(SimpleMethod::new).collect(toImmutableList());
vars.toBuilderConstructor = !vars.toBuilderMethods.isEmpty();
ImmutableListMultimap<ExecutableElement, AnnotationMirror> annotatedPropertyFields =
propertyFieldAnnotationMap(type, propertyMethods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.auto.common.GeneratedAnnotations.generatedAnnotation;
import static com.google.auto.common.MoreElements.getPackage;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static com.google.auto.common.MoreStreams.toImmutableList;
import static com.google.auto.common.MoreStreams.toImmutableSet;
import static com.google.auto.value.processor.ClassNames.AUTO_VALUE_PACKAGE_NAME;
import static com.google.auto.value.processor.ClassNames.COPY_ANNOTATIONS_NAME;
Expand Down Expand Up @@ -467,8 +468,9 @@ final void defineSharedVarsForType(
/** Returns the spelling to be used in the generated code for the given list of annotations. */
static ImmutableList<String> annotationStrings(List<? extends AnnotationMirror> annotations) {
// TODO(b/68008628): use ImmutableList.toImmutableList() when that works.
return ImmutableList.copyOf(
annotations.stream().map(AnnotationOutput::sourceFormForAnnotation).collect(toList()));
return annotations.stream()
.map(AnnotationOutput::sourceFormForAnnotation)
.collect(toImmutableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.auto.value.processor;

import static com.google.auto.common.MoreElements.getLocalAndInheritedMethods;
import static com.google.auto.common.MoreStreams.toImmutableSet;
import static com.google.auto.value.processor.AutoValueishProcessor.hasAnnotationMirror;
import static com.google.auto.value.processor.AutoValueishProcessor.nullableAnnotationFor;
import static com.google.auto.value.processor.ClassNames.AUTO_VALUE_BUILDER_NAME;
Expand All @@ -35,7 +36,6 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -330,15 +330,12 @@ void defineVars(AutoValueOrBuilderTemplateVars vars, BuilderMethodClassifier<?>
vars.builderPropertyBuilders =
ImmutableMap.copyOf(classifier.propertyNameToPropertyBuilder());

Set<Property> required = new LinkedHashSet<>(vars.props);
for (Property property : vars.props) {
if (property.isNullable()
|| property.getOptional() != null
|| vars.builderPropertyBuilders.containsKey(property.getName())) {
required.remove(property);
}
}
vars.builderRequiredProperties = ImmutableSet.copyOf(required);
vars.builderRequiredProperties =
vars.props.stream()
.filter(p -> !p.isNullable())
.filter(p -> p.getOptional() == null)
.filter(p -> !vars.builderPropertyBuilders.containsKey(p.getName()))
.collect(toImmutableSet());
}
}

Expand Down

0 comments on commit c68b2ff

Please sign in to comment.