diff --git a/src/main/java/org/monarchinitiative/gregor/pedigree/Pedigree.java b/src/main/java/org/monarchinitiative/gregor/pedigree/Pedigree.java index c93b7e3..760d0a9 100644 --- a/src/main/java/org/monarchinitiative/gregor/pedigree/Pedigree.java +++ b/src/main/java/org/monarchinitiative/gregor/pedigree/Pedigree.java @@ -1,11 +1,6 @@ package org.monarchinitiative.gregor.pedigree; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; +import java.util.*; // TODO(holtgrem): Test me! @@ -25,12 +20,17 @@ public final class Pedigree { /** * the pedigree's members */ - private final ImmutableList members; + private final List members; /** * mapping from member name to member */ - private final ImmutableMap nameToMember; + private final Map nameToMember; + + /** + * List of names of all members + */ + private final List names; /** * Initialize the object with the given values @@ -40,13 +40,14 @@ public final class Pedigree { */ public Pedigree(String name, Collection members) { this.name = name; - this.members = ImmutableList.copyOf(members); + this.members = List.copyOf(members); - ImmutableMap.Builder mapBuilder = new ImmutableMap.Builder(); + Map map = new LinkedHashMap<>(); int i = 0; for (Person person : members) - mapBuilder.put(person.getName(), new IndexedPerson(i++, person)); - this.nameToMember = mapBuilder.build(); + map.put(person.getName(), new IndexedPerson(i++, person)); + this.nameToMember = Collections.unmodifiableMap(map); + this.names = new ArrayList<>(); } /** @@ -78,14 +79,14 @@ public String getName() { /** * @return the pedigree's members */ - public ImmutableList getMembers() { + public List getMembers() { return members; } /** * @return mapping from member name to member */ - public ImmutableMap getNameToMember() { + public Map getNameToMember() { return nameToMember; } @@ -98,13 +99,12 @@ public ImmutableMap getNameToMember() { * @return Pedigree with the members from names in the given order */ public Pedigree subsetOfMembers(Collection names) { - HashSet nameSet = new HashSet(); - nameSet.addAll(names); + Set nameSet = new HashSet<>(names); ArrayList tmpMembers = new ArrayList(); for (String name : names) if (hasPerson(name)) { - Person p = nameToMember.get(name).getPerson(); + Person p = nameToMember.get(name).person(); Person father = nameSet.contains(p.getFather().getName()) ? p.getFather() : null; Person mother = nameSet.contains(p.getMother().getName()) ? p.getMother() : null; @@ -118,7 +118,7 @@ public Pedigree subsetOfMembers(Collection names) { */ public static Pedigree constructSingleSamplePedigree(String sampleName) { final Person person = new Person(sampleName, null, null, Sex.UNKNOWN, Disease.AFFECTED); - return new Pedigree("pedigree", ImmutableList.of(person)); + return new Pedigree("pedigree", List.of(person)); } /** @@ -131,11 +131,10 @@ public boolean hasPerson(String name) { /** * @return list of members, in the same order as in {@link #members}. */ - public ImmutableList getNames() { - ImmutableList.Builder builder = new ImmutableList.Builder(); + public List getNames() { for (Person p : members) - builder.add(p.getName()); - return builder.build(); + names.add(p.getName()); + return Collections.unmodifiableList(names); } @Override @@ -146,28 +145,23 @@ public String toString() { /** * Helper class, used in the name to member map. */ - public static class IndexedPerson { - private final int idx; - private final Person person; - - public IndexedPerson(int idx, Person person) { - this.idx = idx; - this.person = person; - } + public record IndexedPerson(int idx, Person person) { /** * @return numeric index of person in pedigree */ - public int getIdx() { - return idx; - } + @Override + public int idx() { + return idx; + } - /** - * @return the wrapped {@link Person} - */ - public Person getPerson() { - return person; + /** + * @return the wrapped {@link Person} + */ + @Override + public Person person() { + return person; + } } - } }