Resolve Bug:593. Persister should communicate via OSGi SR instead of TCP.
[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     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         if (this == CANNOT_HANDLE)
57             return -1;
58         if (o == CANNOT_HANDLE)
59             return 1;
60
61         if (priority > o.priority)
62             return 1;
63         if (priority == o.priority)
64             return 0;
65         if (priority < o.priority)
66             return -1;
67
68         throw new IllegalStateException("Unexpected state");
69     }
70
71     @Override
72     public boolean equals(Object o) {
73         if (this == o)
74             return true;
75         if (!(o instanceof HandlingPriority))
76             return false;
77
78         HandlingPriority that = (HandlingPriority) o;
79
80         if (priority != null ? !priority.equals(that.priority) : that.priority != null)
81             return false;
82
83         return true;
84     }
85
86     @Override
87     public int hashCode() {
88         return priority != null ? priority.hashCode() : 0;
89     }
90 }