blob: f480c4769f4985ca5b6576be8ccfe6b6c2adcc3c [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001/**
2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3 * University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
17
18package org.projectfloodlight.openflow.types;
19
20import java.math.BigInteger;
21
22import org.jboss.netty.buffer.ChannelBuffer;
xinwuf08ef682013-12-05 18:29:20 -080023import org.projectfloodlight.openflow.exceptions.OFParseError;
24import org.projectfloodlight.openflow.protocol.OFMessageReader;
Yotam Harcholf3f11152013-09-05 16:47:16 -070025import org.projectfloodlight.openflow.protocol.Writeable;
26
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070027import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -070028import com.google.common.primitives.UnsignedLongs;
29
Yotam Harcholf3f11152013-09-05 16:47:16 -070030public class U64 implements Writeable, OFValueType<U64> {
31 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070032 private final static long ZERO_VAL = 0;
33 public final static U64 ZERO = new U64(ZERO_VAL);
Yotam Harcholf3f11152013-09-05 16:47:16 -070034
35 private final long raw;
36
37 protected U64(final long raw) {
38 this.raw = raw;
39 }
40
41 public static U64 of(long raw) {
42 return ofRaw(raw);
43 }
44
45 public static U64 ofRaw(final long raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070046 if(raw == ZERO_VAL)
47 return ZERO;
Yotam Harcholf3f11152013-09-05 16:47:16 -070048 return new U64(raw);
49 }
50
51 public static U64 parseHex(String hex) {
52 return new U64(new BigInteger(hex, 16).longValue());
53 }
54
55 public long getValue() {
56 return raw;
57 }
58
59 public BigInteger getBigInteger() {
60 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
61 if (raw < 0) {
62 bigInt = bigInt.setBit(Long.SIZE - 1);
63 }
64 return bigInt;
65 }
66
67 @Override
68 public String toString() {
69 return getBigInteger().toString();
70 }
71
72 public static BigInteger f(final long value) {
73 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
74 if (value < 0) {
75 bigInt = bigInt.setBit(Long.SIZE - 1);
76 }
77 return bigInt;
78 }
79
80 public static long t(final BigInteger l) {
81 return l.longValue();
82 }
83
84 @Override
85 public int hashCode() {
86 final int prime = 31;
87 int result = 1;
88 result = prime * result + (int) (raw ^ (raw >>> 32));
89 return result;
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj)
95 return true;
96 if (obj == null)
97 return false;
98 if (getClass() != obj.getClass())
99 return false;
100 U64 other = (U64) obj;
101 if (raw != other.raw)
102 return false;
103 return true;
104 }
105
106 @Override
107 public int getLength() {
108 return 8;
109 }
110
111 @Override
112 public U64 applyMask(U64 mask) {
113 return ofRaw(raw & mask.raw);
114 }
115
116 @Override
117 public void writeTo(ChannelBuffer bb) {
118 bb.writeLong(raw);
119 }
120
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700121 @Override
122 public int compareTo(U64 o) {
123 return UnsignedLongs.compare(raw, o.raw);
124 }
125
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700126 @Override
127 public void putTo(PrimitiveSink sink) {
128 sink.putLong(raw);
129 }
xinwuf08ef682013-12-05 18:29:20 -0800130
131 public final static Reader READER = new Reader();
132
133 private static class Reader implements OFMessageReader<U64> {
134 @Override
135 public U64 readFrom(ChannelBuffer bb) throws OFParseError {
136 return U64.ofRaw(bb.readLong());
137 }
138 }
xinwu9b8989c2013-12-06 16:01:32 -0800139}