Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / netconf / netconf-mapping-api / src / main / java / org / opendaylight / controller / netconf / mapping / api / HandlingPriority.java
index 176cf2d28c02d54dbaeedc39abe23357e27d8324..1f7eed250f5b61725ee32c01aa2b1ce1a3c46f37 100644 (file)
@@ -8,9 +8,11 @@
 
 package org.opendaylight.controller.netconf.mapping.api;
 
+import com.google.common.base.Objects;
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 
-public class HandlingPriority implements Comparable<HandlingPriority> {
+public final class HandlingPriority implements Comparable<HandlingPriority> {
 
     public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority();
     public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
@@ -33,39 +35,60 @@ public class HandlingPriority implements Comparable<HandlingPriority> {
      * @return priority number or Optional.absent otherwise
      */
     public Optional<Integer> getPriority() {
-        return Optional.of(priority).or(Optional.<Integer> absent());
+        return Optional.fromNullable(priority);
+    }
+
+    public HandlingPriority increasePriority(int priorityIncrease) {
+        Preconditions.checkState(priority!=null, "Unable to increase priority for %s", this);
+        Preconditions.checkArgument(priorityIncrease > 0, "Negative increase");
+        Preconditions.checkArgument(Long.valueOf(priority) + priorityIncrease < Integer.MAX_VALUE,
+                "Resulting priority cannot be higher than %s", Integer.MAX_VALUE);
+        return getHandlingPriority(priority + priorityIncrease);
+    }
+
+    public boolean isCannotHandle() {
+        return this.equals(CANNOT_HANDLE);
     }
 
     @Override
     public int compareTo(HandlingPriority o) {
-        if (this == o)
+        if (this == o) {
             return 0;
-        if (this == CANNOT_HANDLE)
+        }
+        if (isCannotHandle()) {
             return -1;
-        if (o == CANNOT_HANDLE)
+        }
+        if (o.isCannotHandle()) {
             return 1;
+        }
 
-        if (priority > o.priority)
+        if (priority > o.priority){
             return 1;
-        if (priority == o.priority)
+        }
+        if (priority.equals(o.priority)){
             return 0;
-        if (priority < o.priority)
+        }
+        if (priority < o.priority){
             return -1;
+        }
 
-        throw new IllegalStateException("Unexpected state");
+        throw new IllegalStateException("Unexpected state comparing " + this + " with " + o);
     }
 
     @Override
     public boolean equals(Object o) {
-        if (this == o)
+        if (this == o){
             return true;
-        if (!(o instanceof HandlingPriority))
+        }
+        if (!(o instanceof HandlingPriority)){
             return false;
+        }
 
         HandlingPriority that = (HandlingPriority) o;
 
-        if (priority != null ? !priority.equals(that.priority) : that.priority != null)
+        if (priority != null ? !priority.equals(that.priority) : that.priority != null){
             return false;
+        }
 
         return true;
     }
@@ -74,4 +97,11 @@ public class HandlingPriority implements Comparable<HandlingPriority> {
     public int hashCode() {
         return priority != null ? priority.hashCode() : 0;
     }
+
+    @Override
+    public String toString() {
+        return Objects.toStringHelper(this)
+                .add("priority", priority)
+                .toString();
+    }
 }