2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.mapping.api;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
15 public final class HandlingPriority implements Comparable<HandlingPriority> {
17 public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority();
18 public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
19 public static final HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
21 private Integer priority;
23 public static HandlingPriority getHandlingPriority(int priority) {
24 return new HandlingPriority(priority);
27 private HandlingPriority(int priority) {
28 this.priority = priority;
31 private HandlingPriority() {
35 * @return priority number or Optional.absent otherwise
37 public Optional<Integer> getPriority() {
38 return Optional.fromNullable(priority);
41 public HandlingPriority increasePriority(int priorityIncrease) {
42 Preconditions.checkState(priority!=null, "Unable to increase priority for %s", this);
43 Preconditions.checkArgument(priorityIncrease > 0, "Negative increase");
44 Preconditions.checkArgument(Long.valueOf(priority) + priorityIncrease < Integer.MAX_VALUE,
45 "Resulting priority cannot be higher than %s", Integer.MAX_VALUE);
46 return getHandlingPriority(priority + priorityIncrease);
49 public boolean isCannotHandle() {
50 return this.equals(CANNOT_HANDLE);
54 public int compareTo(HandlingPriority o) {
58 if (isCannotHandle()) {
61 if (o.isCannotHandle()) {
65 if (priority > o.priority){
68 if (priority.equals(o.priority)){
71 if (priority < o.priority){
75 throw new IllegalStateException("Unexpected state comparing " + this + " with " + o);
79 public boolean equals(Object o) {
83 if (!(o instanceof HandlingPriority)){
87 HandlingPriority that = (HandlingPriority) o;
89 if (priority != null ? !priority.equals(that.priority) : that.priority != null){
97 public int hashCode() {
98 return priority != null ? priority.hashCode() : 0;
102 public String toString() {
103 return MoreObjects.toStringHelper(this)
104 .add("priority", priority)