Use java.util.Optional
[netconf.git] / netconf / netconf-mapping-api / src / main / java / org / opendaylight / 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 package org.opendaylight.netconf.mapping.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.base.MoreObjects;
14 import java.util.Objects;
15 import java.util.Optional;
16
17 public final class HandlingPriority implements Comparable<HandlingPriority> {
18
19     public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority();
20     public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
21     public static final HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
22
23     private Integer priority;
24
25     public static HandlingPriority getHandlingPriority(final int priority) {
26         return new HandlingPriority(priority);
27     }
28
29     private HandlingPriority(final int priority) {
30         this.priority = priority;
31     }
32
33     private HandlingPriority() {
34     }
35
36     /**
37      * Get priority number.
38      *
39      * @return priority number or Optional.absent otherwise
40      */
41     public Optional<Integer> getPriority() {
42         return Optional.ofNullable(priority);
43     }
44
45     public HandlingPriority increasePriority(final int priorityIncrease) {
46         checkState(priority != null, "Unable to increase priority for %s", this);
47         checkArgument(priorityIncrease > 0, "Negative increase");
48         checkArgument(Long.valueOf(priority) + priorityIncrease < Integer.MAX_VALUE,
49                 "Resulting priority cannot be higher than %s", Integer.MAX_VALUE);
50         return getHandlingPriority(priority + priorityIncrease);
51     }
52
53     public boolean isCannotHandle() {
54         return this.equals(CANNOT_HANDLE);
55     }
56
57     @Override
58     @SuppressWarnings("checkstyle:parameterName")
59     public int compareTo(final HandlingPriority o) {
60         if (this == o) {
61             return 0;
62         }
63         if (isCannotHandle()) {
64             return -1;
65         }
66         if (o.isCannotHandle()) {
67             return 1;
68         }
69
70         if (priority > o.priority) {
71             return 1;
72         }
73         if (priority.equals(o.priority)) {
74             return 0;
75         }
76         if (priority < o.priority) {
77             return -1;
78         }
79
80         throw new IllegalStateException("Unexpected state comparing " + this + " with " + priority);
81     }
82
83     @Override
84     public boolean equals(final Object obj) {
85         if (this == obj) {
86             return true;
87         }
88         if (!(obj instanceof HandlingPriority)) {
89             return false;
90         }
91
92         HandlingPriority that = (HandlingPriority) obj;
93
94         return Objects.equals(priority, that.priority);
95     }
96
97     @Override
98     public int hashCode() {
99         return priority != null ? priority.hashCode() : 0;
100     }
101
102     @Override
103     public String toString() {
104         return MoreObjects.toStringHelper(this)
105                 .add("priority", priority)
106                 .toString();
107     }
108 }