2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.mapping.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
13 import com.google.common.base.MoreObjects;
14 import java.util.Objects;
15 import java.util.Optional;
17 public final class HandlingPriority implements Comparable<HandlingPriority> {
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);
23 private Integer priority;
25 public static HandlingPriority getHandlingPriority(final int priority) {
26 return new HandlingPriority(priority);
29 private HandlingPriority(final int priority) {
30 this.priority = priority;
33 private HandlingPriority() {
37 * Get priority number.
39 * @return priority number or Optional.absent otherwise
41 public Optional<Integer> getPriority() {
42 return Optional.ofNullable(priority);
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);
53 public boolean isCannotHandle() {
54 return this.equals(CANNOT_HANDLE);
58 @SuppressWarnings("checkstyle:parameterName")
59 public int compareTo(final HandlingPriority o) {
63 if (isCannotHandle()) {
66 if (o.isCannotHandle()) {
70 if (priority > o.priority) {
73 if (priority.equals(o.priority)) {
76 if (priority < o.priority) {
80 throw new IllegalStateException("Unexpected state comparing " + this + " with " + priority);
84 public boolean equals(final Object obj) {
88 if (!(obj instanceof HandlingPriority)) {
92 HandlingPriority that = (HandlingPriority) obj;
94 return Objects.equals(priority, that.priority);
98 public int hashCode() {
99 return priority != null ? priority.hashCode() : 0;
103 public String toString() {
104 return MoreObjects.toStringHelper(this)
105 .add("priority", priority)