blob: 455218771d0becbc8f5d8a1354cb43e069976f6d [file] [log] [blame]
Sho SHIMIZU15ed4fd2014-08-05 14:40:42 -07001package net.onrc.onos.api.newintent;
2
Sho SHIMIZU228140c2014-08-11 19:15:43 -07003import net.onrc.onos.api.batchoperation.BatchOperationTarget;
4
Sho SHIMIZU15ed4fd2014-08-05 14:40:42 -07005/**
6 * Intent identifier suitable as an external key.
7 *
8 * This class is immutable.
9 */
Sho SHIMIZU228140c2014-08-11 19:15:43 -070010public final class IntentId implements BatchOperationTarget {
Sho SHIMIZU15ed4fd2014-08-05 14:40:42 -070011
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 Radoslavov165ff472014-09-02 12:58:41 -070024 long id = value.toLowerCase().startsWith("0x")
Sho SHIMIZU15ed4fd2014-08-05 14:40:42 -070025 ? Long.parseLong(value.substring(2), HEX)
26 : Long.parseLong(value, DEC);
27 return new IntentId(id);
28 }
29
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070030 /**
31 * Constructor for serializer.
32 */
33 protected IntentId() {
34 this.id = 0;
35 }
Sho SHIMIZU15ed4fd2014-08-05 14:40:42 -070036
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}