Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.projectfloodlight.openflow.util.HexString; |
| 4 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 5 | import com.google.common.primitives.Longs; |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 6 | import com.google.common.primitives.UnsignedLongs; |
| 7 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 8 | /** |
| 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 Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 11 | * |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 12 | * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com> |
| 13 | */ |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 14 | public class DatapathId implements Comparable<DatapathId> { |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 15 | |
| 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 Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 32 | public static DatapathId of(byte[] bytes) { |
| 33 | return new DatapathId(Longs.fromByteArray(bytes)); |
| 34 | } |
| 35 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 36 | public long getLong() { |
| 37 | return rawValue; |
| 38 | } |
| 39 | |
| 40 | public U64 getUnsignedLong() { |
| 41 | return U64.of(rawValue); |
| 42 | } |
| 43 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 44 | public byte[] getBytes() { |
| 45 | return Longs.toByteArray(rawValue); |
| 46 | } |
| 47 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 48 | @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 Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 74 | |
| 75 | @Override |
| 76 | public int compareTo(DatapathId o) { |
| 77 | return UnsignedLongs.compare(rawValue, o.rawValue); |
| 78 | } |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 79 | } |