Simplify boolean expressions
[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
9 package org.opendaylight.netconf.mapping.api;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import java.util.Objects;
15
16 public final class HandlingPriority implements Comparable<HandlingPriority> {
17
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(int priority) {
25         return new HandlingPriority(priority);
26     }
27
28     private HandlingPriority(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.fromNullable(priority);
42     }
43
44     public HandlingPriority increasePriority(int priorityIncrease) {
45         Preconditions.checkState(priority != null, "Unable to increase priority for %s", this);
46         Preconditions.checkArgument(priorityIncrease > 0, "Negative increase");
47         Preconditions.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 this.equals(CANNOT_HANDLE);
54     }
55
56     @Override
57     public int compareTo(HandlingPriority priority) {
58         if (this == priority) {
59             return 0;
60         }
61         if (isCannotHandle()) {
62             return -1;
63         }
64         if (priority.isCannotHandle()) {
65             return 1;
66         }
67
68         if (this.priority > priority.priority) {
69             return 1;
70         }
71         if (this.priority.equals(priority.priority)) {
72             return 0;
73         }
74         if (this.priority < priority.priority) {
75             return -1;
76         }
77
78         throw new IllegalStateException("Unexpected state comparing " + this + " with " + priority);
79     }
80
81     @Override
82     public boolean equals(Object obj) {
83         if (this == obj) {
84             return true;
85         }
86         if (!(obj instanceof HandlingPriority)) {
87             return false;
88         }
89
90         HandlingPriority that = (HandlingPriority) obj;
91
92         return Objects.equals(priority, that.priority);
93     }
94
95     @Override
96     public int hashCode() {
97         return priority != null ? priority.hashCode() : 0;
98     }
99
100     @Override
101     public String toString() {
102         return MoreObjects.toStringHelper(this)
103                 .add("priority", priority)
104                 .toString();
105     }
106 }