<commit_before>/*
   Copyright 2014 Immutables Authors and Contributors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.immutables.value.processor.meta;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import org.immutables.value.Value;
import org.immutables.value.processor.meta.Proto.DeclaringPackage;
import org.immutables.value.processor.meta.Proto.DeclaringType;
import org.immutables.value.processor.meta.Proto.Protoclass;
import org.immutables.value.processor.meta.Proto.Protoclass.Kind;

@Value.Immutable
public abstract class Round {
  public abstract ProcessingEnvironment processing();

  public abstract RoundEnvironment round();

  public abstract Set<TypeElement> annotations();

  public abstract Set<String> customImmutableAnnotations();

  private final Proto.Interning interners = new Proto.Interning();

  @Value.Derived
  ValueTypeComposer composer() {
    return new ValueTypeComposer();
  }

  @Value.Lazy
  public Proto.Environment environment() {
    return ImmutableProto.Environment.of(processing(), this);
  }

  public Multimap<DeclaringPackage, ValueType> collectValues() {
    ImmutableList<Protoclass> protoclasses = collectProtoclasses();
    Map<DeclaringType, ValueType> enclosingTypes = Maps.newHashMap();

    ImmutableMultimap.Builder<DeclaringPackage, ValueType> builder = ImmutableMultimap.builder();

    // Collect enclosing
    for (Protoclass protoclass : protoclasses) {
      if (protoclass.kind().isEnclosing()) {
        ValueType type = composeValue(protoclass);
        enclosingTypes.put(protoclass.declaringType().get(), type);
      }
    }
    // Collect remaining and attach if nested
    for (Protoclass protoclass : protoclasses) {
      @Nullable ValueType current = null;
      if (protoclass.kind().isNested()) {
        @Nullable ValueType enclosing = enclosingTypes.get(protoclass.enclosingOf().get());
        if (enclosing != null) {
          current = composeValue(protoclass);
          // Attach nested to enclosing
          enclosing.addNested(current);
        }
      }
      // getting the ValueType if it was alredy created and put into enclosingTypes
      if (current == null && protoclass.kind().isEnclosing()) {
        current = enclosingTypes.get(protoclass.declaringType().get());
      }
      // If none then we just create it
      if (current == null) {
        current = composeValue(protoclass);
      }
      // We put all enclosing and nested values by the package
      builder.put(protoclass.packageOf(), current);
    }

    return builder.build();
  }

  public ImmutableList<Protoclass> protoclassesFrom(Iterable<? extends Element> elements) {
    ProtoclassCollecter collecter = new ProtoclassCollecter();
    collecter.collect(elements);
    return collecter.builder.build();
  }

  public ImmutableList<Protoclass> collectProtoclasses() {
    ProtoclassCollecter collecter = new ProtoclassCollecter();
    collecter.collect(allAnnotatedElements());
    return collecter.builder.build();
  }

  private Set<Element> allAnnotatedElements() {
    Set<Element> elements = Sets.newHashSetWithExpectedSize(100);
    for (TypeElement annotation : annotations()) {
      Set<? extends Element> annotatedElements = round().getElementsAnnotatedWith(annotation);
      checkAnnotation(annotation, annotatedElements);
      elements.addAll(annotatedElements);
    }
    return elements;
  }

  private void checkAnnotation(TypeElement annotation, Set<? extends Element> annotatedElements) {
    if (annotation.getQualifiedName().contentEquals(ValueUmbrellaMirror.qualifiedName())) {
      for (Element element : annotatedElements) {
        Reporter.from(processing())
            .withElement(element)
            .annotationNamed(ValueUmbrellaMirror.simpleName())<commit_msg>Fix nondeterminism in code generation of nested values (#677)<commit_after>/*
   Copyright 2014 Immutables Authors and Contributors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.immutables.value.processor.meta;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import org.immutables.value.Value;
import org.immutables.value.processor.meta.Proto.DeclaringPackage;
import org.immutables.value.processor.meta.Proto.DeclaringType;
import org.immutables.value.processor.meta.Proto.Protoclass;
import org.immutables.value.processor.meta.Proto.Protoclass.Kind;

@Value.Immutable
public abstract class Round {
  public abstract ProcessingEnvironment processing();

  public abstract RoundEnvironment round();

  public abstract Set<TypeElement> annotations();

  public abstract Set<String> customImmutableAnnotations();

  private final Proto.Interning interners = new Proto.Interning();

  @Value.Derived
  ValueTypeComposer composer() {
    return new ValueTypeComposer();
  }

  @Value.Lazy
  public Proto.Environment environment() {
    return ImmutableProto.Environment.of(processing(), this);
  }

  public Multimap<DeclaringPackage, ValueType> collectValues() {
    ImmutableList<Protoclass> protoclasses = collectProtoclasses();
    Map<DeclaringType, ValueType> enclosingTypes = Maps.newHashMap();

    ImmutableMultimap.Builder<DeclaringPackage, ValueType> builder = ImmutableMultimap.builder();

    // Collect enclosing
    for (Protoclass protoclass : protoclasses) {
      if (protoclass.kind().isEnclosing()) {
        ValueType type = composeValue(protoclass);
        enclosingTypes.put(protoclass.declaringType().get(), type);
      }
    }
    // Collect remaining and attach if nested
    for (Protoclass protoclass : protoclasses) {
      @Nullable ValueType current = null;
      if (protoclass.kind().isNested()) {
        @Nullable ValueType enclosing = enclosingTypes.get(protoclass.enclosingOf().get());
        if (enclosing != null) {
          current = composeValue(protoclass);
          // Attach nested to enclosing
          enclosing.addNested(current);
        }
      }
      // getting the ValueType if it was alredy created and put into enclosingTypes
      if (current == null && protoclass.kind().isEnclosing()) {
        current = enclosingTypes.get(protoclass.declaringType().get());
      }
      // If none then we just create it
      if (current == null) {
        current = composeValue(protoclass);
      }
      // We put all enclosing and nested values by the package
      builder.put(protoclass.packageOf(), current);
    }

    return builder.build();
  }

  public ImmutableList<Protoclass> protoclassesFrom(Iterable<? extends Element> elements) {
    ProtoclassCollecter collecter = new ProtoclassCollecter();
    collecter.collect(elements);
    return collecter.builder.build();
  }

  public ImmutableList<Protoclass> collectProtoclasses() {
    ProtoclassCollecter collecter = new ProtoclassCollecter();
    collecter.collect(allAnnotatedElements());
    return collecter.builder.build();
  }

  private Set<Element> allAnnotatedElements() {
    Set<Element> elements = Sets.newLinkedHashSetWithExpectedSize(100);
    for (TypeElement annotation : annotations()) {
      Set<? extends Element> annotatedElements = round().getElementsAnnotatedWith(annotation);
      checkAnnotation(annotation, annotatedElements);
      elements.addAll(annotatedElements);
    }
    return elements;
  }

  private void checkAnnotation(TypeElement annotation, Set<? extends Element> annotatedElements) {
    if (annotation.getQualifiedName().contentEquals(ValueUmbrellaMirror.qualifiedName())) {
      for (Element element : annotatedElements) {
        Reporter.from(processing())
            .withElement(element)
            .annotationNamed(ValueUmbrellaMirror.simpleName())
{'fraction_non_alphanumeric': 0.09568948313709838, 'fraction_numerical': 0.0024945120734384354, 'mean_word_length': 4.169159360495101, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4627404', 'n_tokens_mistral': 2834, 'n_tokens_neox': 2681, 'n_words': 677}