
package de.ivu.elect.business.gebietsbaum.control;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;

import de.ivu.elect.business.gebietsbaum.entity.Gebiet;
import de.ivu.elect.util.NullComparator;

public class GebietByPositionComparator implements Comparator<Gebiet>, Serializable {

  private static final long serialVersionUID = 1L;

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

  public static final Comparator<Gebiet> ASCENDING = new GebietByPositionComparator();

  public static final Comparator<Gebiet> DESCENDING = Collections.reverseOrder(ASCENDING);

  public GebietByPositionComparator() {
    super();
  }

  @Override
  public int compare(Gebiet one, Gebiet another) {
    if (one == null || another == null) {
      return NULL_COMPARATOR.compare(one, another);
    }

    return comparePosition(one, another);
  }

  private int comparePosition(Gebiet one, Gebiet another) {
    int result = one.getPosition().compareTo(another.getPosition());

    if (result == 0) {
      result = one.getPk().compareTo(another.getPk());
    }

    return result;
  }
}
