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
9 package org.opendaylight.controller.netconf.mapping.api;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
14 public class HandlingPriority implements Comparable<HandlingPriority> {
16 public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority();
17 public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
18 public static final HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
20 private Integer priority;
22 public static HandlingPriority getHandlingPriority(int priority) {
23 return new HandlingPriority(priority);
26 private HandlingPriority(int priority) {
27 this.priority = priority;
30 private HandlingPriority() {
34 * @return priority number or Optional.absent otherwise
36 public Optional<Integer> getPriority() {
37 return Optional.fromNullable(priority);
40 public HandlingPriority increasePriority(int priorityIncrease) {
41 Preconditions.checkState(priority!=null, "Unable to increase priority for %s", this);
42 Preconditions.checkArgument(priorityIncrease > 0, "Negative increase");
43 Preconditions.checkArgument(Long.valueOf(priority) + priorityIncrease < Integer.MAX_VALUE,
44 "Resulting priority cannot be higher than %s", Integer.MAX_VALUE);
45 return getHandlingPriority(priority + priorityIncrease);
48 public boolean isCannotHandle() {
49 return this.equals(CANNOT_HANDLE);
53 public int compareTo(HandlingPriority o) {
56 if (this == CANNOT_HANDLE)
58 if (o == CANNOT_HANDLE)
61 if (priority > o.priority)
63 if (priority == o.priority)
65 if (priority < o.priority)
68 throw new IllegalStateException("Unexpected state");
72 public boolean equals(Object o) {
75 if (!(o instanceof HandlingPriority))
78 HandlingPriority that = (HandlingPriority) o;
80 if (priority != null ? !priority.equals(that.priority) : that.priority != null)
87 public int hashCode() {
88 return priority != null ? priority.hashCode() : 0;