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) { |
Pavlin Radoslavov | 165ff47 | 2014-09-02 12:58:41 -0700 | [diff] [blame] | 24 | long id = value.toLowerCase().startsWith("0x") |
Sho SHIMIZU | 15ed4fd | 2014-08-05 14:40:42 -0700 | [diff] [blame] | 25 | ? Long.parseLong(value.substring(2), HEX) |
| 26 | : Long.parseLong(value, DEC); |
| 27 | return new IntentId(id); |
| 28 | } |
| 29 | |
Sho SHIMIZU | 1674fb3 | 2014-08-20 14:44:31 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Constructor for serializer. |
| 32 | */ |
| 33 | protected IntentId() { |
| 34 | this.id = 0; |
| 35 | } |
Sho SHIMIZU | 15ed4fd | 2014-08-05 14:40:42 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Constructs the ID corresponding to a given long value. |
| 39 | * |
| 40 | * @param id the underlying value of this ID |
| 41 | */ |
| 42 | public IntentId(long id) { |
| 43 | this.id = id; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public int hashCode() { |
| 48 | return (int) (id ^ (id >>> 32)); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public boolean equals(Object obj) { |
| 53 | if (obj == this) { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | if (!(obj instanceof IntentId)) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | IntentId that = (IntentId) obj; |
| 62 | return this.id == that.id; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public String toString() { |
| 67 | return "0x" + Long.toHexString(id); |
| 68 | } |
| 69 | |
| 70 | } |