From 220a510d656b8b0ecd7404fdd013c03e80630c50 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 15 Jul 2016 08:47:38 -0400 Subject: [PATCH] Make Priority an enum Today we have an abstraction Priority for representing priorities. Ideally, these values are a fixed set of constants with a well-defined ordering which sounds perfect for an enum. This commit changes Priority so that it is an enum instead of a class. --- .../org/elasticsearch/common/Priority.java | 97 +++---------------- .../elasticsearch/common/PriorityTests.java | 11 +-- .../concurrent/PrioritizedExecutorsTests.java | 5 +- 3 files changed, 19 insertions(+), 94 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/Priority.java b/core/src/main/java/org/elasticsearch/common/Priority.java index e7faaba2b66..4b588d65484 100644 --- a/core/src/main/java/org/elasticsearch/common/Priority.java +++ b/core/src/main/java/org/elasticsearch/common/Priority.java @@ -16,28 +16,29 @@ * specific language governing permissions and limitations * under the License. */ + package org.elasticsearch.common; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -/** - * - */ -public final class Priority implements Comparable { +public enum Priority { + + IMMEDIATE((byte) -1), + URGENT((byte) 0), + HIGH((byte) 1), + NORMAL((byte) 2), + LOW((byte) 3), + LANGUID((byte) 4); public static Priority readFrom(StreamInput input) throws IOException { return fromByte(input.readByte()); } public static void writeTo(Priority priority, StreamOutput output) throws IOException { - byte b = priority.value; - output.writeByte(b); + output.writeByte(priority.value); } public static Priority fromByte(byte b) { @@ -53,90 +54,18 @@ public final class Priority implements Comparable { } } - public static final Priority IMMEDIATE = new Priority((byte) -1); - public static final Priority URGENT = new Priority((byte) 0); - public static final Priority HIGH = new Priority((byte) 1); - public static final Priority NORMAL = new Priority((byte) 2); - public static final Priority LOW = new Priority((byte) 3); - public static final Priority LANGUID = new Priority((byte) 4); - private static final List VALUES = - Collections.unmodifiableList(Arrays.asList(IMMEDIATE, URGENT, HIGH, NORMAL, LOW, LANGUID)); - private final byte value; - private Priority(byte value) { + Priority(byte value) { this.value = value; } - /** - * All priorities, sorted from highest priority to lowest priority. The returned list is - * unmodifiable. - * - * @return an unmodifiable list of priorities, sorted from highest priority to lowest priority. - */ - public static List values() { - return VALUES; - } - - @Override - public int compareTo(Priority p) { - return (this.value < p.value) ? -1 : ((this.value > p.value) ? 1 : 0); - } - public boolean after(Priority p) { - return value > p.value; + return this.compareTo(p) > 0; } public boolean sameOrAfter(Priority p) { - return value >= p.value; + return this.compareTo(p) >= 0; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || Priority.class != o.getClass()) return false; - - Priority priority = (Priority) o; - - if (value != priority.value) return false; - - return true; - } - - @Override - public int hashCode() { - return (int) value; - } - - @Override - public String toString() { - switch (value) { - case (byte) -1: return "IMMEDIATE"; - case (byte) 0: return "URGENT"; - case (byte) 1: return "HIGH"; - case (byte) 2: return "NORMAL"; - case (byte) 3: return "LOW"; - default: - return "LANGUID"; - } - } - - public static Priority valueOf(String value) { - switch (value) { - case "IMMEDIATE": - return IMMEDIATE; - case "URGENT": - return URGENT; - case "HIGH": - return HIGH; - case "NORMAL": - return NORMAL; - case "LOW": - return LOW; - case "LANGUID": - return LANGUID; - default: - throw new IllegalArgumentException("no such priority: " + value); - } - } } diff --git a/core/src/test/java/org/elasticsearch/common/PriorityTests.java b/core/src/test/java/org/elasticsearch/common/PriorityTests.java index a2acc0fdc42..6fb4cead054 100644 --- a/core/src/test/java/org/elasticsearch/common/PriorityTests.java +++ b/core/src/test/java/org/elasticsearch/common/PriorityTests.java @@ -22,7 +22,6 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.test.ESTestCase; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -37,7 +36,7 @@ public class PriorityTests extends ESTestCase { IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { Priority.valueOf("foobar"); }); - assertEquals("no such priority: foobar", exception.getMessage()); + assertEquals("No enum constant org.elasticsearch.common.Priority.foobar", exception.getMessage()); } public void testToString() { @@ -47,7 +46,7 @@ public class PriorityTests extends ESTestCase { assertEquals("LOW", Priority.LOW.toString()); assertEquals("URGENT", Priority.URGENT.toString()); assertEquals("NORMAL", Priority.NORMAL.toString()); - assertEquals(6, Priority.values().size()); + assertEquals(6, Priority.values().length); } public void testSerialization() throws IOException { @@ -63,7 +62,7 @@ public class PriorityTests extends ESTestCase { assertSame(Priority.LOW, Priority.fromByte((byte) 3)); assertSame(Priority.NORMAL, Priority.fromByte((byte) 2)); assertSame(Priority.URGENT,Priority.fromByte((byte) 0)); - assertEquals(6, Priority.values().size()); + assertEquals(6, Priority.values().length); } public void testCompareTo() { @@ -82,11 +81,11 @@ public class PriorityTests extends ESTestCase { for (Priority p : Priority.values()) { assertEquals(0, p.compareTo(p)); } - List shuffeledAndSorted = new ArrayList<>(Priority.values()); + List shuffeledAndSorted = Arrays.asList(Priority.values()); Collections.shuffle(shuffeledAndSorted, random()); Collections.sort(shuffeledAndSorted); for (List priorities : Arrays.asList(shuffeledAndSorted, - Priority.values())) { // #values() guarantees order! + Arrays.asList(Priority.values()))) { // #values() guarantees order! assertSame(Priority.IMMEDIATE, priorities.get(0)); assertSame(Priority.URGENT, priorities.get(1)); assertSame(Priority.HIGH, priorities.get(2)); diff --git a/core/src/test/java/org/elasticsearch/common/util/concurrent/PrioritizedExecutorsTests.java b/core/src/test/java/org/elasticsearch/common/util/concurrent/PrioritizedExecutorsTests.java index ae9fd990364..933a46de510 100644 --- a/core/src/test/java/org/elasticsearch/common/util/concurrent/PrioritizedExecutorsTests.java +++ b/core/src/test/java/org/elasticsearch/common/util/concurrent/PrioritizedExecutorsTests.java @@ -41,16 +41,13 @@ import java.util.concurrent.atomic.AtomicBoolean; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -/** - * - */ public class PrioritizedExecutorsTests extends ESTestCase { private final ThreadContext holder = new ThreadContext(Settings.EMPTY); public void testPriorityQueue() throws Exception { PriorityBlockingQueue queue = new PriorityBlockingQueue<>(); - List priorities = new ArrayList<>(Priority.values()); + List priorities = Arrays.asList(Priority.values()); Collections.shuffle(priorities, random()); for (Priority priority : priorities) {