Sho SHIMIZU | 15ed4fd | 2014-08-05 14:40:42 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.newintent; |
| 2 | |
Sho SHIMIZU | 228140c | 2014-08-11 19:15:43 -0700 | [diff] [blame] | 3 | import net.onrc.onos.api.batchoperation.BatchOperationTarget; |
| 4 | |
Sho SHIMIZU | 15ed4fd | 2014-08-05 14:40:42 -0700 | [diff] [blame] | 5 | /** |
| 6 | * Intent identifier suitable as an external key. |
| 7 | * |
| 8 | * This class is immutable. |
| 9 | */ |
Sho SHIMIZU | 228140c | 2014-08-11 19:15:43 -0700 | [diff] [blame] | 10 | public final class IntentId implements BatchOperationTarget { |
Sho SHIMIZU | 15ed4fd | 2014-08-05 14:40:42 -0700 | [diff] [blame] | 11 | |
| 12 | private static final int DEC = 10; |
| 13 | private static final int HEX = 16; |
| 14 | |
| 15 | private final long id; |
| 16 | |
| 17 | /** |
| 18 | * Creates an intent identifier from the specified string representation. |
| 19 | * |
| 20 | * @param value long value |
| 21 | * @return intent identifier |
| 22 | */ |
| 23 | public static IntentId valueOf(String value) { |
| 24 | long id = value.startsWith("0x") |
| 25 | ? Long.parseLong(value.substring(2), HEX) |
| 26 | : Long.parseLong(value, DEC); |
| 27 | return new IntentId(id); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Constructs the ID corresponding to a given long value. |
| 33 | * |
| 34 | * @param id the underlying value of this ID |
| 35 | */ |
| 36 | public IntentId(long id) { |
| 37 | this.id = id; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public int hashCode() { |
| 42 | return (int) (id ^ (id >>> 32)); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public boolean equals(Object obj) { |
| 47 | if (obj == this) { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | if (!(obj instanceof IntentId)) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | IntentId that = (IntentId) obj; |
| 56 | return this.id == that.id; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String toString() { |
| 61 | return "0x" + Long.toHexString(id); |
| 62 | } |
| 63 | |
| 64 | } |