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

org.molgenis.data.icd10.Icd10ClassExpanderImpl Maven / Gradle / Ivy

There is a newer version: 8.4.5
Show newest version
package org.molgenis.data.icd10;

import static java.util.stream.Collectors.toList;

import com.google.common.collect.Streams;
import com.google.common.collect.TreeTraverser;
import java.util.Collection;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.molgenis.data.Entity;
import org.springframework.stereotype.Component;

@Component
public class Icd10ClassExpanderImpl implements Icd10ClassExpander {
  private static final String DISEASE_CLASS_CHILDREN_ATTRIBUTE_NAME = "children";

  @Override
  public Collection expandClasses(Collection diseaseClasses) {
    return diseaseClasses
        .stream()
        .flatMap(this::expandClass)
        .map(this::toDiseaseClass)
        .distinct()
        .map(this::toEntity)
        .collect(toList());
  }

  private Stream expandClass(Entity diseaseClass) {
    Iterable diseaseClasses =
        new DiseaseClassTreeTraverser().postOrderTraversal(diseaseClass);
    return Streams.stream(diseaseClasses);
  }

  private DiseaseClass toDiseaseClass(Entity entity) {
    return new DiseaseClass(entity);
  }

  private Entity toEntity(DiseaseClass diseaseClass) {
    return diseaseClass.getEntity();
  }

  /** Disease class entity wrapper with equals and hashCode implementation. */
  private static class DiseaseClass {
    private final Entity entity;

    private DiseaseClass(Entity entity) {
      this.entity = entity;
    }

    private Entity getEntity() {
      return entity;
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;

      DiseaseClass that = (DiseaseClass) o;

      return entity.getIdValue().equals(that.entity.getIdValue());
    }

    @Override
    public int hashCode() {
      return entity.getIdValue().hashCode();
    }
  }

  private static class DiseaseClassTreeTraverser extends TreeTraverser {
    @Override
    public Iterable children(@Nonnull Entity diseaseClassEntity) {
      return diseaseClassEntity.getEntities(DISEASE_CLASS_CHILDREN_ATTRIBUTE_NAME);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy