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

com.github.geekonjava.query.order.OrderComparator Maven / Gradle / Ivy

The newest version!
package com.github.geekonjava.query.order;

import java.util.Comparator;

import com.github.geekonjava.reflection.Phanton;

public class OrderComparator implements Comparator {

  private final String method;

  public OrderComparator(String method) {
    this.method = method;
  }

  @Override
  @SuppressWarnings("unchecked")
  public int compare(T one, T other) {
    Object oneValue = Phanton.from(one).call(method);
    Object otherValue = Phanton.from(other).call(method);
    if (oneValue == null || otherValue == null) {
      return 0;
    }
    if (oneValue instanceof Comparable) {
      return ((Comparable) oneValue).compareTo(otherValue);
    } else {
      return 0;
    }
  }

}