Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.registry; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 2 | |
| 3 | import org.codehaus.jackson.annotate.JsonProperty; |
| 4 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 5 | public class ControllerRegistryEntry implements Comparable<ControllerRegistryEntry> { |
Pavlin Radoslavov | 60c9b8f | 2014-04-09 16:25:01 -0700 | [diff] [blame] | 6 | // |
| 7 | // TODO: Refactor the implementation and decide whether controllerId |
| 8 | // is needed. If "yes", we might need to consider it inside the |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 9 | // compareTo(), equals() and hashCode() implementations. |
Pavlin Radoslavov | 60c9b8f | 2014-04-09 16:25:01 -0700 | [diff] [blame] | 10 | // |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 11 | private final String controllerId; |
| 12 | private final int sequenceNumber; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 13 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 14 | public ControllerRegistryEntry(String controllerId, int sequenceNumber) { |
| 15 | this.controllerId = controllerId; |
| 16 | this.sequenceNumber = sequenceNumber; |
| 17 | } |
| 18 | |
| 19 | @JsonProperty("controllerId") |
| 20 | public String getControllerId() { |
| 21 | return controllerId; |
| 22 | } |
| 23 | |
Pavlin Radoslavov | 60c9b8f | 2014-04-09 16:25:01 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Compares this object with the specified object for order. |
| 26 | * NOTE: the test is based on ControllerRegistryEntry sequence numbers, |
| 27 | * and doesn't include the controllerId. |
| 28 | * |
| 29 | * @param o the object to be compared. |
| 30 | * @return a negative integer, zero, or a positive integer as this object |
| 31 | * is less than, equal to, or greater than the specified object. |
| 32 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | @Override |
| 34 | public int compareTo(ControllerRegistryEntry o) { |
Pavlin Radoslavov | 60c9b8f | 2014-04-09 16:25:01 -0700 | [diff] [blame] | 35 | return this.sequenceNumber - o.sequenceNumber; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 36 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 37 | |
Pavlin Radoslavov | 60c9b8f | 2014-04-09 16:25:01 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Test whether some other object is "equal to" this one. |
| 40 | * NOTE: the test is based on ControllerRegistryEntry sequence numbers, |
| 41 | * and doesn't include the controllerId. |
| 42 | * |
| 43 | * @param obj the reference object with which to compare. |
| 44 | * @return true if this object is the same as the obj argument; false |
| 45 | * otherwise. |
| 46 | */ |
| 47 | @Override |
| 48 | public boolean equals(Object obj) { |
| 49 | if (obj instanceof ControllerRegistryEntry) { |
| 50 | ControllerRegistryEntry other = (ControllerRegistryEntry) obj; |
| 51 | return this.sequenceNumber == other.sequenceNumber; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the hash code for the object. |
| 58 | * NOTE: the computation is based on ControllerRegistryEntry sequence |
| 59 | * numbers, and doesn't include the controller ID. |
| 60 | * |
| 61 | * @return a hash code value for this object. |
| 62 | */ |
| 63 | @Override |
| 64 | public int hashCode() { |
| 65 | return Integer.valueOf(this.sequenceNumber).hashCode(); |
| 66 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 67 | } |