Rich Lane | 2d1c5c7 | 2013-12-03 13:02:10 -0800 | [diff] [blame^] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | |
| 5 | import com.google.common.hash.PrimitiveSink; |
| 6 | |
| 7 | public class OFChecksum128 implements OFValueType<OFChecksum128> { |
| 8 | |
| 9 | static final int LENGTH = 16; |
| 10 | |
| 11 | private final long raw1; // MSBs |
| 12 | private final long raw2; // LSBs |
| 13 | |
| 14 | public static final OFChecksum128 ZERO = new OFChecksum128(0, 0); |
| 15 | |
| 16 | private OFChecksum128(long raw1, long raw2) { |
| 17 | this.raw1 = raw1; |
| 18 | this.raw2 = raw2; |
| 19 | } |
| 20 | |
| 21 | public static OFChecksum128 of(long raw1, long raw2) { |
| 22 | if (raw1 == 0 && raw2 == 0) |
| 23 | return ZERO; |
| 24 | return new OFChecksum128(raw1, raw2); |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public int getLength() { |
| 29 | return LENGTH; |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public OFChecksum128 applyMask(OFChecksum128 mask) { |
| 34 | return of(this.raw1 & mask.raw1, this.raw2 & mask.raw2); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public boolean equals(Object obj) { |
| 39 | if (!(obj instanceof OFChecksum128)) |
| 40 | return false; |
| 41 | OFChecksum128 other = (OFChecksum128)obj; |
| 42 | return (other.raw1 == this.raw1 && other.raw2 == this.raw2); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public int hashCode() { |
| 47 | return (int)(31 * raw1 + raw2); |
| 48 | } |
| 49 | |
| 50 | public void write16Bytes(ChannelBuffer cb) { |
| 51 | cb.writeLong(raw1); |
| 52 | cb.writeLong(raw2); |
| 53 | } |
| 54 | |
| 55 | public static OFChecksum128 read16Bytes(ChannelBuffer cb) { |
| 56 | long raw1 = cb.readLong(); |
| 57 | long raw2 = cb.readLong(); |
| 58 | return of(raw1, raw2); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public String toString() { |
| 63 | return String.format("0x%016x%016x", raw1, raw2); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int compareTo(OFChecksum128 o) { |
| 68 | long c = this.raw1 - o.raw1; |
| 69 | if (c != 0) |
| 70 | return Long.signum(c); |
| 71 | return Long.signum(this.raw2 - o.raw2); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void putTo(PrimitiveSink sink) { |
| 76 | sink.putLong(raw1); |
| 77 | sink.putLong(raw2); |
| 78 | } |
| 79 | |
| 80 | } |