All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.regnosys.rosetta.translate.GeneratorUtil Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate;

import com.google.common.collect.Iterables;
import com.regnosys.rosetta.rosetta.RosettaExternalSynonymSource;
import com.regnosys.rosetta.rosetta.RosettaSynonymSource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;

@SuppressWarnings("all")
public class GeneratorUtil {
  public static List synonymSourceNames(final Collection sources) {
    final Function1 _function = (RosettaSynonymSource it) -> {
      return it.getName();
    };
    return ListExtensions.map(GeneratorUtil.synonymSources(sources), _function);
  }

  public static List externalSynonymSources(final Collection sources) {
    return IterableExtensions.toList(Iterables.filter(GeneratorUtil.synonymSources(sources), RosettaExternalSynonymSource.class));
  }

  private static List synonymSources(final Collection sources) {
    final ArrayList collected = CollectionLiterals.newArrayList();
    final Consumer _function = (RosettaSynonymSource it) -> {
      GeneratorUtil.traverse(it, collected);
    };
    sources.forEach(_function);
    return IterableExtensions.toList(collected);
  }

  /**
   * Recursively follow super synonym sources to build a hierarchy from bottom to top.
   */
  private static  void traverse(final RosettaSynonymSource source, final List collected) {
    if ((source != null)) {
      GeneratorUtil.addLast(collected, source);
      final Consumer _function = (RosettaSynonymSource it) -> {
        GeneratorUtil.traverse(it, collected);
      };
      GeneratorUtil.getSuperSources(source).forEach(_function);
    }
  }

  /**
   * Adds element to list.  If list already contains element, remove and add as the last element.
   */
  private static void addLast(final List list, final RosettaSynonymSource source) {
    boolean _contains = list.contains(source);
    if (_contains) {
      list.remove(source);
    }
    list.add(source);
  }

  /**
   * Only external synonyms allow inheritance.
   */
  private static List getSuperSources(final RosettaSynonymSource source) {
    if ((source instanceof RosettaExternalSynonymSource)) {
      return ((RosettaExternalSynonymSource)source).getSuperSources();
    }
    return CollectionLiterals.emptyList();
  }
}