Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 8e5bab2 | 2013-04-02 04:19:41 +0000 | [diff] [blame] | 3 | import java.math.BigInteger; |
| 4 | |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.util.serializers.FlowIdDeserializer; |
| 6 | import net.onrc.onos.core.util.serializers.FlowIdSerializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | |
Pavlin Radoslavov | f74271f | 2013-11-25 18:22:47 -0800 | [diff] [blame] | 8 | import org.codehaus.jackson.annotate.JsonIgnore; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 9 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 10 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 11 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 12 | /** |
| 13 | * The class representing a Flow ID. |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 14 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 15 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 16 | @JsonDeserialize(using = FlowIdDeserializer.class) |
| 17 | @JsonSerialize(using = FlowIdSerializer.class) |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 18 | public final class FlowId implements Comparable<FlowId> { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 19 | private static final long INVALID = -1; |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 20 | private final long value; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Default constructor. |
| 24 | */ |
| 25 | public FlowId() { |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 26 | this.value = FlowId.INVALID; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Constructor from an integer value. |
| 31 | * |
| 32 | * @param value the value to use. |
| 33 | */ |
| 34 | public FlowId(long value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | this.value = value; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 39 | * Constructor from a string. |
| 40 | * |
| 41 | * @param value the value to use. |
| 42 | */ |
| 43 | public FlowId(String value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | // |
| 45 | // Use the help of BigInteger to parse strings representing |
| 46 | // large unsigned hex long values. |
| 47 | // |
| 48 | char c = 0; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 49 | if (value.length() > 2) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | c = value.charAt(1); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 51 | } |
| 52 | if ((c == 'x') || (c == 'X')) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | this.value = new BigInteger(value.substring(2), 16).longValue(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 54 | } else { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | this.value = Long.decode(value); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 56 | } |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 60 | * Get the value of the Flow ID. |
| 61 | * |
| 62 | * @return the value of the Flow ID. |
| 63 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | public long value() { |
| 65 | return value; |
| 66 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
Pavlin Radoslavov | f74271f | 2013-11-25 18:22:47 -0800 | [diff] [blame] | 69 | * Test whether the Flow ID is valid. |
| 70 | * |
| 71 | * @return true if the Flow ID is valid, otherwise false. |
| 72 | */ |
| 73 | @JsonIgnore |
| 74 | public boolean isValid() { |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 75 | return (this.value() != FlowId.INVALID); |
Pavlin Radoslavov | f74271f | 2013-11-25 18:22:47 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 79 | * Convert the Flow ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 80 | * |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 81 | * @return the Flow ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 82 | */ |
| 83 | @Override |
| 84 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | return "0x" + Long.toHexString(this.value); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 86 | } |
Toshio Koide | fff6642 | 2013-06-26 13:57:24 -0700 | [diff] [blame] | 87 | |
Toshio Koide | d8651e7 | 2013-06-27 11:28:11 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Compare two FlowId objects numerically using their Flow IDs. |
| 90 | * |
| 91 | * @return the value 0 if the Flow ID is equal to the argument's Flow ID; |
Pavlin Radoslavov | 60c9b8f | 2014-04-09 16:25:01 -0700 | [diff] [blame] | 92 | * a value less than 0 if the Flow ID is numerically less than the |
| 93 | * argument's Flow ID; and a value greater than 0 if the Flow ID is |
| 94 | * numerically greater than the argument's Flow ID. |
Toshio Koide | d8651e7 | 2013-06-27 11:28:11 -0700 | [diff] [blame] | 95 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | @Override |
| 97 | public int compareTo(FlowId o) { |
| 98 | return Long.valueOf(this.value).compareTo(o.value()); |
| 99 | } |
Pavlin Radoslavov | 60c9b8f | 2014-04-09 16:25:01 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Test whether some other object is "equal to" this one. |
| 103 | * |
| 104 | * @param obj the reference object with which to compare. |
| 105 | * @return true if this object is the same as the obj argument; false |
| 106 | * otherwise. |
| 107 | */ |
| 108 | @Override |
| 109 | public boolean equals(Object obj) { |
| 110 | if (obj instanceof FlowId) { |
| 111 | FlowId other = (FlowId) obj; |
| 112 | return (this.value == other.value); |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get the hash code for the object. |
| 119 | * |
| 120 | * @return a hash code value for this object. |
| 121 | */ |
| 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return Long.valueOf(this.value).hashCode(); |
| 125 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 126 | } |