Update Tomas's name
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / api / operations / 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.server.api.operations;
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     public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority();
19     public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
20     public static final HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
21
22     private Integer priority;
23
24     public static HandlingPriority getHandlingPriority(final int priority) {
25         return new HandlingPriority(priority);
26     }
27
28     private HandlingPriority(final int priority) {
29         this.priority = priority;
30     }
31
32     private HandlingPriority() {
33     }
34
35     /**
36      * Get priority number.
37      *
38      * @return priority number or Optional.absent otherwise
39      */
40     public Optional<Integer> getPriority() {
41         return Optional.ofNullable(priority);
42     }
43
44     public HandlingPriority increasePriority(final int priorityIncrease) {
45         checkState(priority != null, "Unable to increase priority for %s", this);
46         checkArgument(priorityIncrease > 0, "Negative increase");
47         checkArgument(Long.valueOf(priority) + priorityIncrease < Integer.MAX_VALUE,
48                 "Resulting priority cannot be higher than %s", Integer.MAX_VALUE);
49         return getHandlingPriority(priority + priorityIncrease);
50     }
51
52     public boolean isCannotHandle() {
53         return equals(CANNOT_HANDLE);
54     }
55
56     @Override
57     @SuppressWarnings("checkstyle:parameterName")
58     public int compareTo(final HandlingPriority o) {
59         if (this == o) {
60             return 0;
61         }
62         if (isCannotHandle()) {
63             return -1;
64         }
65         if (o.isCannotHandle()) {
66             return 1;
67         }
68
69         if (priority > o.priority) {
70             return 1;
71         }
72         if (priority.equals(o.priority)) {
73             return 0;
74         }
75         if (priority < o.priority) {
76             return -1;
77         }
78
79         throw new IllegalStateException("Unexpected state comparing " + this + " with " + priority);
80     }
81
82     @Override
83     public boolean equals(final Object obj) {
84         return this == obj || obj instanceof HandlingPriority other && Objects.equals(priority, other.priority);
85     }
86
87     @Override
88     public int hashCode() {
89         return priority != null ? priority.hashCode() : 0;
90     }
91
92     @Override
93     public String toString() {
94         return MoreObjects.toStringHelper(this).add("priority", priority).toString();
95     }
96 }