HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.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 | |
HIGUCHI Yuta | f086d8a | 2013-06-12 15:26:35 -0700 | [diff] [blame] | 5 | import net.onrc.onos.ofcontroller.util.serializers.FlowEntryIdDeserializer; |
| 6 | import net.onrc.onos.ofcontroller.util.serializers.FlowEntryIdSerializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 8 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 9 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 10 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 11 | /** |
| 12 | * The class representing a Flow Entry ID. |
| 13 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 14 | @JsonDeserialize(using=FlowEntryIdDeserializer.class) |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 15 | @JsonSerialize(using=FlowEntryIdSerializer.class) |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 16 | public class FlowEntryId { |
| 17 | private long value; |
| 18 | |
| 19 | /** |
| 20 | * Default constructor. |
| 21 | */ |
| 22 | public FlowEntryId() { |
| 23 | this.value = 0; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Constructor from an integer value. |
| 28 | * |
| 29 | * @param value the value to use. |
| 30 | */ |
| 31 | public FlowEntryId(long value) { |
| 32 | this.value = value; |
| 33 | } |
| 34 | |
| 35 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 36 | * Constructor from a string. |
| 37 | * |
| 38 | * @param value the value to use. |
| 39 | */ |
| 40 | public FlowEntryId(String value) { |
Pavlin Radoslavov | 8e5bab2 | 2013-04-02 04:19:41 +0000 | [diff] [blame] | 41 | // |
| 42 | // Use the help of BigInteger to parse strings representing |
| 43 | // large unsigned hex long values. |
| 44 | // |
| 45 | char c = 0; |
| 46 | if (value.length() > 2) |
| 47 | c = value.charAt(1); |
| 48 | if ((c == 'x') || (c == 'X')) |
| 49 | this.value = new BigInteger(value.substring(2), 16).longValue(); |
| 50 | else |
| 51 | this.value = Long.decode(value); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 55 | * Get the value of the Flow Entry ID. |
| 56 | * |
| 57 | * @return the value of the Flow Entry ID. |
| 58 | */ |
| 59 | public long value() { return value; } |
| 60 | |
| 61 | /** |
| 62 | * Set the value of the Flow Entry ID. |
| 63 | * |
| 64 | * @param value the value to set. |
| 65 | */ |
| 66 | public void setValue(long value) { |
| 67 | this.value = value; |
| 68 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Returns true of the object is another Flow Entry ID with |
| 72 | * the same value; otherwise, returns false. |
| 73 | * |
| 74 | * @param Object to compare |
| 75 | */ |
| 76 | @Override |
| 77 | public boolean equals(Object obj){ |
| 78 | if(obj.getClass() == this.getClass()) { |
| 79 | FlowEntryId entry = (FlowEntryId) obj; |
| 80 | return this.value() == entry.value(); |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Return the hash code of the Flow Entry ID |
| 87 | */ |
| 88 | @Override |
| 89 | public int hashCode() { |
| 90 | return Long.valueOf(value).hashCode(); |
| 91 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 92 | |
| 93 | /** |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 94 | * Convert the Flow Entry ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 95 | * |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 96 | * @return the Flow Entry ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 97 | */ |
| 98 | @Override |
| 99 | public String toString() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 100 | return "0x" + Long.toHexString(this.value); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 101 | } |
| 102 | } |