blob: 79fa14f12c353952bfe7cd0df9b16cf1d3d1739f [file] [log] [blame]
alshabib1f44e8e2014-08-14 15:19:57 -07001package org.projectfloodlight.openflow.types;
2
3import org.projectfloodlight.openflow.annotations.Immutable;
4import org.projectfloodlight.openflow.util.HexString;
5
6import com.google.common.hash.PrimitiveSink;
7import com.google.common.primitives.Longs;
8import com.google.common.primitives.UnsignedLongs;
9
10/**
11 * Abstraction of a datapath ID that can be set and/or accessed as either a
12 * long value or a colon-separated string. Immutable
13 *
14 * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com>
15 */
16@Immutable
17public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> {
18
19 public static final DatapathId NONE = new DatapathId(0);
20
21 private final long rawValue;
22
23 private DatapathId(long rawValue) {
24 this.rawValue = rawValue;
25 }
26
27 public static DatapathId of(long rawValue) {
28 return new DatapathId(rawValue);
29 }
30
31 public static DatapathId of(String s) {
32 return new DatapathId(HexString.toLong(s));
33 }
34
35 public static DatapathId of(byte[] bytes) {
36 return new DatapathId(Longs.fromByteArray(bytes));
37 }
38
39 public long getLong() {
40 return rawValue;
41 }
42
43 public U64 getUnsignedLong() {
44 return U64.of(rawValue);
45 }
46
47 public byte[] getBytes() {
48 return Longs.toByteArray(rawValue);
49 }
50
51 @Override
52 public String toString() {
53 return HexString.toHexString(rawValue);
54 }
55
56 @Override
57 public int hashCode() {
58 final int prime = 31;
59 int result = 1;
60 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
61 return result;
62 }
63
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj)
67 return true;
68 if (obj == null)
69 return false;
70 if (getClass() != obj.getClass())
71 return false;
72 DatapathId other = (DatapathId) obj;
73 if (rawValue != other.rawValue)
74 return false;
75 return true;
76 }
77
78 @Override
79 public void putTo(PrimitiveSink sink) {
80 sink.putLong(rawValue);
81 }
82
83 @Override
84 public int compareTo(DatapathId o) {
85 return UnsignedLongs.compare(rawValue, o.rawValue);
86 }
87}