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

marcel.lang.util.Comparators Maven / Gradle / Ivy

package marcel.lang.util;

import marcel.lang.util.function.CharComparator;
import marcel.lang.util.function.DoubleComparator;
import marcel.lang.util.function.FloatComparator;
import marcel.lang.util.function.IntComparator;
import marcel.lang.util.function.LongComparator;

import java.util.Comparator;

public class Comparators {

  public static IntComparator asIntComparator(final Comparator c) {
    if (c == null || c instanceof IntComparator) return (IntComparator) c;
    return new IntComparator() {
      @Override
      public int compare(int x, int y) { return c.compare(Integer.valueOf(x), Integer.valueOf(y)); }
      @Override
      public int compare(Integer x, Integer y) { return c.compare(x, y); }
    };
  }
  public static LongComparator asLongComparator(final Comparator c) {
    if (c == null || c instanceof LongComparator) return (LongComparator) c;
    return new LongComparator() {
      @Override
      public int compare(long x, long y) { return c.compare(Long.valueOf(x), Long.valueOf(y)); }
      @Override
      public int compare(Long x, Long y) { return c.compare(x, y); }
    };
  }
  public static FloatComparator asFloatComparator(final Comparator c) {
    if (c == null || c instanceof FloatComparator) return (FloatComparator) c;
    return new FloatComparator() {
      @Override
      public int compare(float x, float y) { return c.compare(Float.valueOf(x), Float.valueOf(y)); }
      @Override
      public int compare(Float x, Float y) { return c.compare(x, y); }
    };
  }
  public static CharComparator asCharComparator(final Comparator c) {
    if (c == null || c instanceof CharComparator) return (CharComparator) c;
    return new CharComparator() {
      @Override
      public int compare(char x, char y) { return c.compare(Character.valueOf(x), Character.valueOf(y)); }
      @Override
      public int compare(Character x, Character y) { return c.compare(x, y); }
    };
  }

  public static DoubleComparator asDoubleComparator(final Comparator c) {
    if (c == null || c instanceof DoubleComparator) return (DoubleComparator) c;
    return new DoubleComparator() {
      @Override
      public int compare(double x, double y) { return c.compare(Double.valueOf(x), Double.valueOf(y)); }
      @Override
      public int compare(Double x, Double y) { return c.compare(x, y); }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy