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