blob: f58d65840dc13cc02b8c16aa95d2d70ba52d6e1a [file] [log] [blame]
Rob Vaterlausfeee3712013-09-30 11:24:19 -07001package org.projectfloodlight.openflow.types;
2
3import org.projectfloodlight.openflow.util.HexString;
4
Rob Vaterlaus57dde612013-10-18 10:02:28 -07005import com.google.common.primitives.Longs;
Andreas Wundsam72977ce2013-10-15 21:31:59 -07006import com.google.common.primitives.UnsignedLongs;
7
Rob Vaterlausfeee3712013-09-30 11:24:19 -07008/**
9 * Abstraction of a datapath ID that can be set and/or accessed as either a
10 * long value or a colon-separated string.
Andreas Wundsam72977ce2013-10-15 21:31:59 -070011 *
Rob Vaterlausfeee3712013-09-30 11:24:19 -070012 * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com>
13 */
Andreas Wundsam72977ce2013-10-15 21:31:59 -070014public class DatapathId implements Comparable<DatapathId> {
Rob Vaterlausfeee3712013-09-30 11:24:19 -070015
16 public static final DatapathId NONE = new DatapathId(0);
17
18 private final long rawValue;
19
20 private DatapathId(long rawValue) {
21 this.rawValue = rawValue;
22 }
23
24 public static DatapathId of(long rawValue) {
25 return new DatapathId(rawValue);
26 }
27
28 public static DatapathId of(String s) {
29 return new DatapathId(HexString.toLong(s));
30 }
31
Rob Vaterlaus57dde612013-10-18 10:02:28 -070032 public static DatapathId of(byte[] bytes) {
33 return new DatapathId(Longs.fromByteArray(bytes));
34 }
35
Rob Vaterlausfeee3712013-09-30 11:24:19 -070036 public long getLong() {
37 return rawValue;
38 }
39
40 public U64 getUnsignedLong() {
41 return U64.of(rawValue);
42 }
43
Rob Vaterlaus57dde612013-10-18 10:02:28 -070044 public byte[] getBytes() {
45 return Longs.toByteArray(rawValue);
46 }
47
Rob Vaterlausfeee3712013-09-30 11:24:19 -070048 @Override
49 public String toString() {
50 return HexString.toHexString(rawValue);
51 }
52
53 @Override
54 public int hashCode() {
55 final int prime = 31;
56 int result = 1;
57 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
58 return result;
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj)
64 return true;
65 if (obj == null)
66 return false;
67 if (getClass() != obj.getClass())
68 return false;
69 DatapathId other = (DatapathId) obj;
70 if (rawValue != other.rawValue)
71 return false;
72 return true;
73 }
Andreas Wundsam72977ce2013-10-15 21:31:59 -070074
75 @Override
76 public int compareTo(DatapathId o) {
77 return UnsignedLongs.compare(rawValue, o.rawValue);
78 }
Rob Vaterlausfeee3712013-09-30 11:24:19 -070079}