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
13 public class HandlingPriority implements Comparable<HandlingPriority> {
14
15     public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority();
16     public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
17     public static final HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
18
19     private Integer priority;
20
21     public static HandlingPriority getHandlingPriority(int priority) {
22         return new HandlingPriority(priority);
23     }
24
25     private HandlingPriority(int priority) {
26         this.priority = priority;
27     }
28
29     private HandlingPriority() {
30     }
31
32     /**
33      * @return priority number or Optional.absent otherwise
34      */
35     public Optional<Integer> getPriority() {
36         return Optional.of(priority).or(Optional.<Integer> absent());
37     }
38
39     @Override
40     public int compareTo(HandlingPriority o) {
41         if (this == o)
42             return 0;
43         if (this == CANNOT_HANDLE)
44             return -1;
45         if (o == CANNOT_HANDLE)
46             return 1;
47
48         if (priority > o.priority)
49             return 1;
50         if (priority == o.priority)
51             return 0;
52         if (priority < o.priority)
53             return -1;
54
55         throw new IllegalStateException("Unexpected state");
56     }
57
58     @Override
59     public boolean equals(Object o) {
60         if (this == o)
61             return true;
62         if (!(o instanceof HandlingPriority))
63             return false;
64
65         HandlingPriority that = (HandlingPriority) o;
66
67         if (priority != null ? !priority.equals(that.priority) : that.priority != null)
68             return false;
69
70         return true;
71     }
72
73     @Override
74     public int hashCode() {
75         return priority != null ? priority.hashCode() : 0;
76     }
77 }