
package de.ivu.elect.business.system.entity;

import java.lang.management.ThreadInfo;
import java.util.Comparator;
import java.util.Locale;

import de.ivu.elect.util.CaseAndDiacriticIgnoringComparator;
import de.ivu.elect.util.NullComparator;

public class ThreadInfoByNameComparator implements Comparator<ThreadInfo> {

  private static final Comparator<Object> NULL_COMPARATOR = NullComparator.NULL_BEFORE_NON_NULL_COMPARATOR;

  private static final Comparator<String> STRING_COMPARATOR = new CaseAndDiacriticIgnoringComparator(Locale.ENGLISH);

  public ThreadInfoByNameComparator() {
    super();
  }

  @Override
  public int compare(ThreadInfo o1, ThreadInfo o2) {
    if (o1 != null && o2 != null) {
      if (o1.getThreadName() != null && o2.getThreadName() != null) {
        return STRING_COMPARATOR.compare(o1.getThreadName(), o2.getThreadName());
      }

      return Long.compare(o1.getThreadId(), o2.getThreadId());
    }

    return NULL_COMPARATOR.compare(o1, o2);
  }
}
