< prev index next >

core/org.openjdk.jmc.flightrecorder.rules/src/main/java/org/openjdk/jmc/flightrecorder/rules/util/RulesToolkit.java

Print this page

        

@@ -143,27 +143,58 @@
          */
         public enum EventAvailability {
                 /**
                  * The type has events available in the collection.
                  */
-                AVAILABLE,
+                                AVAILABLE(4),
                 /**
                  * The type was actively enabled in the collection.
                  */
-                ENABLED,
+                                ENABLED(3),
                 /**
                  * The type was actively disabled in the collection.
                  */
-                DISABLED,
+                                DISABLED(2),
                 /**
                  * The type is known in the collection, but no events were found.
                  */
-                NONE,
+                                NONE(1),
                 /**
                  * The type is unknown in the collection.
                  */
-                UNAVAILABLE
+                                UNKNOWN(0);
+
+                /*
+                 * Used to determine the ordering of availabilities.
+                 */
+                private final int availabilityScore;
+
+                EventAvailability(int availabilityScore) {
+                        this.availabilityScore = availabilityScore;
+                }
+
+                /**
+                 * 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.
+                 */
+                public boolean isMoreAvailableThan(EventAvailability availability) {
+                        return availabilityScore > availability.availabilityScore;
+                }
+
+                /**
+                 * 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.
+                 */
+                public boolean isLessAvailableThan(EventAvailability availability) {
+                        return availabilityScore < availability.availabilityScore;
+                }
         }
 
         /**
          * @return a least squares approximation of the increase in memory over the given time period,
          *         in mebibytes/second

@@ -384,11 +415,11 @@
          *            the {@link EventAvailability} to check
          * @return false if any {@link EventAvailability} is disabled or unavailable. Otherwise true.
          */
         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;
                         }
                 }
                 return true;
         }

@@ -434,11 +465,25 @@
                         return EventAvailability.DISABLED;
                 }
                 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;
         }
 
         /**
          * Checks if the event types are known in the collection. Note that it does not necessarily mean
          * that there are events of the event type.

@@ -506,11 +551,11 @@
                         return getNotApplicableResult(rule,
                                         MessageFormat.format(Messages.getString(Messages.RulesToolkit_RULE_REQUIRES_EVENT_TYPE),
                                                         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<String> quotedTypeIds = new ArrayList<>();
                         for (String typeId : typeIds) {
                                 quotedTypeIds.add("'" + typeId + "'"); //$NON-NLS-1$ //$NON-NLS-2$
                         }
< prev index next >