Merge "Take advantage of MultipartTransactionAware"
[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 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     @Override
49     public int compareTo(HandlingPriority o) {
50         if (this == o)
51             return 0;
52         if (this == CANNOT_HANDLE)
53             return -1;
54         if (o == CANNOT_HANDLE)
55             return 1;
56
57         if (priority > o.priority)
58             return 1;
59         if (priority == o.priority)
60             return 0;
61         if (priority < o.priority)
62             return -1;
63
64         throw new IllegalStateException("Unexpected state");
65     }
66
67     @Override
68     public boolean equals(Object o) {
69         if (this == o)
70             return true;
71         if (!(o instanceof HandlingPriority))
72             return false;
73
74         HandlingPriority that = (HandlingPriority) o;
75
76         if (priority != null ? !priority.equals(that.priority) : that.priority != null)
77             return false;
78
79         return true;
80     }
81
82     @Override
83     public int hashCode() {
84         return priority != null ? priority.hashCode() : 0;
85     }
86 }