Initial code drop of netconf protocol implementation
[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     // TODO test
40
41     @Override
42     public int compareTo(HandlingPriority o) {
43         if (this == o)
44             return 0;
45         if (this == CANNOT_HANDLE)
46             return -1;
47         if (o == CANNOT_HANDLE)
48             return 1;
49
50         if (priority > o.priority)
51             return 1;
52         if (priority == o.priority)
53             return 0;
54         if (priority < o.priority)
55             return -1;
56
57         throw new IllegalStateException("Unexpected state");
58     }
59
60     @Override
61     public boolean equals(Object o) {
62         if (this == o)
63             return true;
64         if (!(o instanceof HandlingPriority))
65             return false;
66
67         HandlingPriority that = (HandlingPriority) o;
68
69         if (priority != null ? !priority.equals(that.priority) : that.priority != null)
70             return false;
71
72         return true;
73     }
74
75     @Override
76     public int hashCode() {
77         return priority != null ? priority.hashCode() : 0;
78     }
79 }