BUG-693 Config module for netconf client dispatcher.
[controller.git] / opendaylight / netconf / netconf-mapping-api / src / main / java / org / opendaylight / controller / netconf / mapping / api / HandlingPriority.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.mapping.api;
10
11 import com.google.common.base.Objects;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14
15 public final class HandlingPriority implements Comparable<HandlingPriority> {
16
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);
20
21     private Integer priority;
22
23     public static HandlingPriority getHandlingPriority(int priority) {
24         return new HandlingPriority(priority);
25     }
26
27     private HandlingPriority(int priority) {
28         this.priority = priority;
29     }
30
31     private HandlingPriority() {
32     }
33
34     /**
35      * @return priority number or Optional.absent otherwise
36      */
37     public Optional<Integer> getPriority() {
38         return Optional.fromNullable(priority);
39     }
40
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);
47     }
48
49     public boolean isCannotHandle() {
50         return this.equals(CANNOT_HANDLE);
51     }
52
53     @Override
54     public int compareTo(HandlingPriority o) {
55         if (this == o) {
56             return 0;
57         }
58         if (isCannotHandle()) {
59             return -1;
60         }
61         if (o.isCannotHandle()) {
62             return 1;
63         }
64
65         if (priority > o.priority){
66             return 1;
67         }
68         if (priority.equals(o.priority)){
69             return 0;
70         }
71         if (priority < o.priority){
72             return -1;
73         }
74
75         throw new IllegalStateException("Unexpected state comparing " + this + " with " + o);
76     }
77
78     @Override
79     public boolean equals(Object o) {
80         if (this == o){
81             return true;
82         }
83         if (!(o instanceof HandlingPriority)){
84             return false;
85         }
86
87         HandlingPriority that = (HandlingPriority) o;
88
89         if (priority != null ? !priority.equals(that.priority) : that.priority != null){
90             return false;
91         }
92
93         return true;
94     }
95
96     @Override
97     public int hashCode() {
98         return priority != null ? priority.hashCode() : 0;
99     }
100
101     @Override
102     public String toString() {
103         return Objects.toStringHelper(this)
104                 .add("priority", priority)
105                 .toString();
106     }
107 }