--- old/core/org.openjdk.jmc.flightrecorder.rules/src/main/java/org/openjdk/jmc/flightrecorder/rules/util/RulesToolkit.java 2018-07-02 15:02:07.000000000 +0200 +++ new/core/org.openjdk.jmc.flightrecorder.rules/src/main/java/org/openjdk/jmc/flightrecorder/rules/util/RulesToolkit.java 2018-07-02 15:02:07.000000000 +0200 @@ -142,26 +142,57 @@ * Knowledge about the state of affairs of an event type in an IItemCollection. */ public enum EventAvailability { - /** - * The type has events available in the collection. - */ - AVAILABLE, - /** - * The type was actively enabled in the collection. - */ - ENABLED, - /** - * The type was actively disabled in the collection. + /** + * The type has events available in the collection. + */ + AVAILABLE(4), + /** + * The type was actively enabled in the collection. + */ + ENABLED(3), + /** + * The type was actively disabled in the collection. + */ + DISABLED(2), + /** + * The type is known in the collection, but no events were found. + */ + NONE(1), + /** + * The type is unknown in the collection. + */ + UNKNOWN(0); + + /* + * Used to determine the ordering of availabilities. */ - DISABLED, + private final int availabilityScore; + + EventAvailability(int availabilityScore) { + this.availabilityScore = availabilityScore; + } + /** - * The type is known in the collection, but no events were found. + * Returns true if this EventAvailability is more available than the provided one. + * + * @param availability + * the {@link EventAvailability} to compare to. + * @return true if this EventAvailability is more available than the provided one. */ - NONE, + public boolean isMoreAvailableThan(EventAvailability availability) { + return availabilityScore > availability.availabilityScore; + } + /** - * The type is unknown in the collection. + * Returns true if this EventAvailability is less available than the provided one. + * + * @param availability + * the {@link EventAvailability} to compare to. + * @return true if this EventAvailability is less available than the provided one. */ - UNAVAILABLE + public boolean isLessAvailableThan(EventAvailability availability) { + return availabilityScore < availability.availabilityScore; + } } /** @@ -386,7 +417,7 @@ */ public static boolean isEventsEnabled(EventAvailability ... eventAvailabilities) { for (EventAvailability availability : eventAvailabilities) { - if (availability == EventAvailability.DISABLED || availability == EventAvailability.UNAVAILABLE) { + if (availability == EventAvailability.DISABLED || availability == EventAvailability.UNKNOWN) { return false; } } @@ -436,7 +467,21 @@ if (isEventsKnown(items, typeIds)) { return EventAvailability.NONE; } - return EventAvailability.UNAVAILABLE; + return EventAvailability.UNKNOWN; + } + + /** + * Returns the lowest availability from the ones provided. See {@link EventAvailability}. + */ + public static EventAvailability getLeastAvailable(EventAvailability ... availabilites) { + EventAvailability lowest = EventAvailability.AVAILABLE; + + for (EventAvailability availability : availabilites) { + if (availability.isLessAvailableThan(lowest)) { + lowest = availability; + } + } + return lowest; } /** @@ -508,7 +553,7 @@ disabledEventTypeNames), MessageFormat.format(Messages.getString(Messages.RulesToolkit_RULE_REQUIRES_EVENT_TYPE_LONG), rule.getName(), disabledEventTypeNames)); - case UNAVAILABLE: + case UNKNOWN: // Can't get type names if the event type is unavailable List quotedTypeIds = new ArrayList<>(); for (String typeId : typeIds) {