Bug 460 - Fix warning throughout netconf subsystem
[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.Optional;
12 import com.google.common.base.Preconditions;
13
14 public final class HandlingPriority implements Comparable<HandlingPriority> {
15
16     public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority();
17     public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
18     public static final HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
19
20     private Integer priority;
21
22     public static HandlingPriority getHandlingPriority(int priority) {
23         return new HandlingPriority(priority);
24     }
25
26     private HandlingPriority(int priority) {
27         this.priority = priority;
28     }
29
30     private HandlingPriority() {
31     }
32
33     /**
34      * @return priority number or Optional.absent otherwise
35      */
36     public Optional<Integer> getPriority() {
37         return Optional.fromNullable(priority);
38     }
39
40     public HandlingPriority increasePriority(int priorityIncrease) {
41         Preconditions.checkState(priority!=null, "Unable to increase priority for %s", this);
42         Preconditions.checkArgument(priorityIncrease > 0, "Negative increase");
43         Preconditions.checkArgument(Long.valueOf(priority) + priorityIncrease < Integer.MAX_VALUE,
44                 "Resulting priority cannot be higher than %s", Integer.MAX_VALUE);
45         return getHandlingPriority(priority + priorityIncrease);
46     }
47
48     public boolean isCannotHandle() {
49         return this.equals(CANNOT_HANDLE);
50     }
51
52     @Override
53     public int compareTo(HandlingPriority o) {
54         if (this == o){
55             return 0;
56         }
57         if (this.equals(CANNOT_HANDLE)){
58             return -1;
59         }
60         if (o.equals(CANNOT_HANDLE)){
61             return 1;
62         }
63
64         if (priority > o.priority){
65             return 1;
66         }
67         if (priority.equals(o.priority)){
68             return 0;
69         }
70         if (priority < o.priority){
71             return -1;
72         }
73         throw new IllegalStateException("Unexpected state");
74     }
75
76     @Override
77     public boolean equals(Object o) {
78         if (this == o){
79             return true;
80         }
81         if (!(o instanceof HandlingPriority)){
82             return false;
83         }
84
85         HandlingPriority that = (HandlingPriority) o;
86
87         if (priority != null ? !priority.equals(that.priority) : that.priority != null){
88             return false;
89         }
90
91         return true;
92     }
93
94     @Override
95     public int hashCode() {
96         return priority != null ? priority.hashCode() : 0;
97     }
98 }