blob: a889fb32490a23f7b35fa44e4dee3a87356c5aee [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Pavlin Radoslavov8e5bab22013-04-02 04:19:41 +00003import java.math.BigInteger;
4
Jonathan Hart23701d12014-04-03 10:45:48 -07005import net.onrc.onos.core.util.serializers.FlowIdDeserializer;
6import net.onrc.onos.core.util.serializers.FlowIdSerializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -08008import org.codehaus.jackson.annotate.JsonIgnore;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08009import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080010import org.codehaus.jackson.map.annotate.JsonSerialize;
11
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012/**
13 * The class representing a Flow ID.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070014 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015 */
Ray Milkey269ffb92014-04-03 14:43:30 -070016@JsonDeserialize(using = FlowIdDeserializer.class)
17@JsonSerialize(using = FlowIdSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070018public final class FlowId implements Comparable<FlowId> {
Ray Milkeyec838942014-04-09 11:28:43 -070019 private static final long INVALID = -1;
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070020 private final long value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080021
22 /**
23 * Default constructor.
24 */
25 public FlowId() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070026 this.value = FlowId.INVALID;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080027 }
28
29 /**
30 * Constructor from an integer value.
31 *
32 * @param value the value to use.
33 */
34 public FlowId(long value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 this.value = value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080036 }
37
38 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080039 * Constructor from a string.
40 *
41 * @param value the value to use.
42 */
43 public FlowId(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070044 //
45 // Use the help of BigInteger to parse strings representing
46 // large unsigned hex long values.
47 //
48 char c = 0;
Ray Milkeyb29e6262014-04-09 16:02:14 -070049 if (value.length() > 2) {
Ray Milkey269ffb92014-04-03 14:43:30 -070050 c = value.charAt(1);
Ray Milkeyb29e6262014-04-09 16:02:14 -070051 }
52 if ((c == 'x') || (c == 'X')) {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 this.value = new BigInteger(value.substring(2), 16).longValue();
Ray Milkeyb29e6262014-04-09 16:02:14 -070054 } else {
Ray Milkey269ffb92014-04-03 14:43:30 -070055 this.value = Long.decode(value);
Ray Milkeyb29e6262014-04-09 16:02:14 -070056 }
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080057 }
58
59 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080060 * Get the value of the Flow ID.
61 *
62 * @return the value of the Flow ID.
63 */
Ray Milkey269ffb92014-04-03 14:43:30 -070064 public long value() {
65 return value;
66 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080067
68 /**
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -080069 * 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 Radoslavov29a2a882014-04-08 17:40:54 -070075 return (this.value() != FlowId.INVALID);
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -080076 }
77
78 /**
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080079 * Convert the Flow ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080080 *
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080081 * @return the Flow ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080082 */
83 @Override
84 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return "0x" + Long.toHexString(this.value);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080086 }
Toshio Koidefff66422013-06-26 13:57:24 -070087
Toshio Koided8651e72013-06-27 11:28:11 -070088 /**
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 Radoslavov60c9b8f2014-04-09 16:25:01 -070092 * 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 Koided8651e72013-06-27 11:28:11 -070095 */
Ray Milkey269ffb92014-04-03 14:43:30 -070096 @Override
97 public int compareTo(FlowId o) {
98 return Long.valueOf(this.value).compareTo(o.value());
99 }
Pavlin Radoslavov60c9b8f2014-04-09 16:25:01 -0700100
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 Radoslavov5363c2a2013-02-18 09:55:42 -0800126}