Compare Java enum values

Is there a way to check if an enum value is 'greater/equal' to another value?

I want to check if an error level is 'error or above'.


Solution 1:

All Java enums implements Comparable: http://java.sun.com/javase/6/docs/api/java/lang/Enum.html

You can also use the ordinal method to turn them into ints, then comparison is trivial.

if (ErrorLevel.ERROR.compareTo(someOtherLevel) <= 0) {
  ...
}

Solution 2:

A version which is much more expressive would be

myError.isErrorOrAbove(otherError)

or

myError.isWorseThan(otherError)

This way you can define the ordering inside the Enum and can change the implementation internally if you like. Now the clients of the Enum can compare values without knowing any details about it.

A possible implementation whould be

public enum ErrorLevel {

    INFO(0),
    WARN(1),
    ERROR(2),
    FATAL(3);

    private Integer severity;

    ErrorLevel(int severity) {
        this.severity = severity;
    }

    public boolean isWorseThan(ErrorLevel other) {
        return this.severity > other.severity;
    }
}

I also would not recommend using the ordinal() method for comparison, because when somebody changes the order the Enum values are defined you could get unexpected behaviour.

Solution 3:

Java enum has already build in compareTo(..) method, which uses the enum position (aka ordinal) to compare one object to other. The position is determined based on the order in which the enum constants are declared , where the first constant is assigned an ordinal of zero.
If that arrangement is unsuitable, you may need to define you own comparator by adding internal field(s) as shown below:

import java.util.Comparator;

public enum Day {
  MONDAY(1, 3),
  TUESDAY(2, 6),
  WEDNESDAY(3, 5),
  THURSDAY(4, 4),
  FRIDAY(5, 2),
  SATURDAY(6, 1),
  SUNDAY(0, 0);

  private final int calendarPosition;
  private final int workLevel;

  Day(int position, int level) {
    calendarPosition = position;
    workLevel = level;
  }

  int getCalendarPosition(){ return calendarPosition; }  
  int getWorkLevel() { return workLevel;  }

  public static Comparator<Day> calendarPositionComparator = new Comparator<Day>() {
    public int compare(Day d1, Day d2) {
      return d1.getCalendarPosition() - d2.getCalendarPosition();
    }
  };

  public static Comparator<Day> workLevelComparator = new Comparator<Day>() {
    public int compare(Day d1, Day d2) {
      // descending order, harder first
      return d2.getWorkLevel() - d1.getWorkLevel();
    }
  };        
}

Driver to check if all works:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class EnumTest
{
  public static void main (String[] args) {
     List<Day> allDays = Arrays.asList(Day.values());
     System.out.println("===\nListing days in order of calendar position:");
     Collections.sort(allDays, Day.calendarPositionComparator);
     showItems(allDays);
     System.out.println("===\nListing days in order of work level:");
     Collections.sort(allDays, Day.workLevelComparator);
     showItems(allDays);
  }

  public static void showItems(List<Day> days) {
    for (Day day : days) {
      System.out.println(day.name());
    }
  }
}

Solution 4:

Assuming you've defined them in order of severity, you can compare the ordinals of each value. The ordinal is its position in its enum declaration, where the initial constant is assigned an ordinal of zero.

You get the ordinal by calling the ordinal() method of the value.

Solution 5:

I want to check if an error level is 'error or above'.

Such an enum should have a level associated with it. So to find equals or greater you should compare the levels.

Using ordinal relies on the order the enum values appear. If you rely on this you should document it otherwise such a dependency can lead to brittle code.