java.lang.Enum.compareTo()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(5.5k)|赞(0)|评价(0)|浏览(210)

本文整理了Java中java.lang.Enum.compareTo()方法的一些代码示例,展示了Enum.compareTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Enum.compareTo()方法的具体详情如下:
包路径:java.lang.Enum
类名称:Enum
方法名:compareTo

Enum.compareTo介绍

[英]Compares this object to the specified enum object to determine their relative order. This method compares the object's ordinal values, that is, their position in the enum declaration.
[中]将此对象与指定的枚举对象进行比较,以确定它们的相对顺序。此方法比较对象的序数值,即它们在枚举声明中的位置。

代码示例

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> between(E start, E end, Inclusion inclusion) {
  return toEntityPredicate(e -> {
    switch (inclusion) {
      case START_EXCLUSIVE_END_EXCLUSIVE:
        return e.compareTo(start) >  0 && e.compareTo(end) <  0;
      case START_EXCLUSIVE_END_INCLUSIVE:
        return e.compareTo(start) >  0 && e.compareTo(end) <= 0;
      case START_INCLUSIVE_END_EXCLUSIVE:
        return e.compareTo(start) >= 0 && e.compareTo(end) <  0;
      case START_INCLUSIVE_END_INCLUSIVE:
        return e.compareTo(start) >= 0 && e.compareTo(end) <= 0;
      default : throw new UnsupportedOperationException(
        "Unknown inclusion '" + inclusion + "'."
      );
    }
  });
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> between(E start, E end, Inclusion inclusion) {
  return toEntityPredicate(e -> {
    switch (inclusion) {
      case START_EXCLUSIVE_END_EXCLUSIVE:
        return e.compareTo(start) >  0 && e.compareTo(end) <  0;
      case START_EXCLUSIVE_END_INCLUSIVE:
        return e.compareTo(start) >  0 && e.compareTo(end) <= 0;
      case START_INCLUSIVE_END_EXCLUSIVE:
        return e.compareTo(start) >= 0 && e.compareTo(end) <  0;
      case START_INCLUSIVE_END_INCLUSIVE:
        return e.compareTo(start) >= 0 && e.compareTo(end) <= 0;
      default : throw new UnsupportedOperationException(
        UNKNOWN_INCLUSION + " '" + inclusion + "'."
      );
    }
  });
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> notBetween(E start, E end, Inclusion inclusion) {
  return toEntityPredicate(e -> {
    switch (inclusion) {
      case START_EXCLUSIVE_END_EXCLUSIVE:
        return e.compareTo(start) <= 0 || e.compareTo(end) >= 0;
      case START_EXCLUSIVE_END_INCLUSIVE:
        return e.compareTo(start) <= 0 || e.compareTo(end) >  0;
      case START_INCLUSIVE_END_EXCLUSIVE:
        return e.compareTo(start) <  0 || e.compareTo(end) >= 0;
      case START_INCLUSIVE_END_INCLUSIVE:
        return e.compareTo(start) <  0 || e.compareTo(end) >  0;
      default : throw new UnsupportedOperationException(
        "Unknown inclusion '" + inclusion + "'."
      );
    }
  });
}

代码示例来源:origin: stackoverflow.com

enum MyEnum
{
  DOG("woof"),
  CAT("meow");

  String sound;    
  MyEnum(String s) { sound = s; }
}

class MyEnumComparator implements Comparator<MyEnum>
{
  public int compare(MyEnum o1, MyEnum o2)
  {
    return -o1.compareTo(o2); // this flips the order
    return o1.sound.length() - o2.sound.length(); // this compares length
  }
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> notEqual(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) != 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> lessThan(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) < 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> equal(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) == 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> lessThan(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) < 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> lessOrEqual(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) <= 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> greaterThan(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) > 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> greaterThan(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) > 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> equal(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) == 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> greaterOrEqual(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) >= 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> notEqual(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) != 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> lessOrEqual(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) <= 0);
}

代码示例来源:origin: speedment/speedment

@Override
public SpeedmentPredicate<ENTITY> greaterOrEqual(E value) {
  return toEntityPredicate(e -> e != null && e.compareTo(value) >= 0);
}

代码示例来源:origin: speedment/speedment

@Override
public int compare(ENTITY first, ENTITY second) {
  final E f = get(first);
  final E s = get(second);
  if (f == null && s == null) return 0;
  else if (f == null) return 1;
  else if (s == null) return -1;
  else return f.compareTo(s);
}

代码示例来源:origin: speedment/speedment

@Override
default int compare(T first, T second) {
  final E a = apply(first);
  final E b = apply(second);
  if (a == null) {
    return (b == null) ? -1 : 0;
  } else {
    return a.compareTo(b);
  }
}

代码示例来源:origin: speedment/speedment

@Override
public int compare(ENTITY first, ENTITY second) {
  final E f = get(first);
  final E s = get(second);
  if (f == null && s == null) return 0;
  else if (f == null) return 1;
  else if (s == null) return -1;
  else return f.compareTo(s);
}

代码示例来源:origin: speedment/speedment

@Override
default int compare(T first, T second) {
  return apply(first).compareTo(apply(second));
}

相关文章