package org.pharmgkb.pharmcat.haplotype.model;

import java.util.Set;
import java.util.TreeSet;
import javax.annotation.Nonnull;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.ObjectUtils;
import org.pharmgkb.pharmcat.definition.model.NamedAllele;


/**
 * This represents a haplotype and the sequences that matched it.
 *
 * @author Mark Woon
 */
public class HaplotypeMatch implements Comparable<HaplotypeMatch> {
  @SerializedName("name")
  @Expose
  private String m_name;
  private NamedAllele m_haplotype;
  @Expose
  private Set<String> m_sequences = new TreeSet<>();


  public HaplotypeMatch(@Nonnull NamedAllele haplotype) {
    m_haplotype = haplotype;
    m_name = m_haplotype.getName();
  }


  public String getName() {
    return m_name;
  }

  public @Nonnull NamedAllele getHaplotype() {
    return m_haplotype;
  }


  public boolean match(@Nonnull String seq) {
    if (m_haplotype.getPermutations().matcher(seq).matches()) {
      m_sequences.add(seq);
      return true;
    }
    return false;
  }

  public Set<String> getSequences() {
    return m_sequences;
  }


  @Override
  public int compareTo(@Nonnull HaplotypeMatch o) {

    int rez = ObjectUtils.compare(m_haplotype, o.getHaplotype());
    if (rez != 0) {
      return rez;
    }
    if (m_sequences == null && o.getSequences() == null) {
      return 0;
    }
    if (m_sequences == null) {
      return -1;
    }
    if (o.getSequences() == null) {
      return 1;
    }
    return ObjectUtils.compare(m_sequences.size(), o.getSequences().size());
  }

  @Override
  public String toString() {
    return m_haplotype.toString();
  }
}

@@ -58,16 +58,22 @@
 va.util.
+Sorted
 Set;%0Aimp
@@ -494,16 +494,26 @@
 atch%3E %7B%0A
+  @Expose%0A
   @Seria
@@ -530,26 +530,16 @@
 %22name%22)%0A
-  @Expose%0A
   privat
@@ -600,16 +600,47 @@
 @Expose%0A
+  @SerializedName(%22sequences%22)%0A
   privat
@@ -641,16 +641,22 @@
 private 
+Sorted
 Set%3CStri
@@ -1145,16 +1145,31 @@
  public 
+@Nonnull Sorted
 Set%3CStri
@@ -1398,201 +1398,8 @@
   %7D%0A
-    if (m_sequences == null && o.getSequences() == null) %7B%0A      return 0;%0A    %7D%0A    if (m_sequences == null) %7B%0A      return -1;%0A    %7D%0A    if (o.getSequences() == null) %7B%0A      return 1;%0A    %7D%0A
     

Remove unnecessary null checks.