Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8090739

Priority should implement Comparable

XMLWordPrintable

      javafx.scene.layout.Priority should implement Comparable to make it easier to work with multiple nodes that have different Priorities. This would allow the following code to work:
      Sequences.min(for (child in children) XContainer.getNodeHGrow(child));

      Here is a sample PriorityComparator that satisfies this requirement, and may be useful as a default implementation:
      public class PriorityComparator implements Comparator<Priority> {
          @Override
          public int compare(Priority p1, Priority p2) {
              int p1int = convertToInt(p1);
              int p2int = convertToInt(p2);
              return p1int - p2int;
          }
          private int convertToInt(Priority p) {
              return p == Priority.ALWAYS ? 2 :
                  p == Priority.SOMETIMES ? 1 : 0;
          }
      }

      And of course a test case (written in JavaFX using JFXtras Test):
      public class PriorityComparatorTest extends Test {}

      public function run() {
          perform("PriorityComparator should find", [
              for (p in Priority.values()) {
                  Test {
                      say: "{p} == {p}"
                      do: function() {PriorityComparator {}.compare(p, p)}
                      expect: equalTo(0)
                  }
              }
              for (p in Priority.values() where p != Priority.NEVER) {
                  Test {
                      say: "NEVER < {p}"
                      do: function() {PriorityComparator {}.compare(Priority.NEVER, p)}
                      expect: lessThan(0)
                  }
              }
              for (p in Priority.values() where p != Priority.ALWAYS) {
                  Test {
                      say: "ALWAYS > {p}"
                      do: function() {PriorityComparator {}.compare(Priority.ALWAYS, p)}
                      expect: greaterThan(0)
                  }
              }
          ]);
      }

            Unassigned Unassigned
            schin Stephen Chin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported: