blob: 8cd577f2bd3a87cfc01a05900da411fccb22ea18 [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;
23import org.projectfloodlight.openflow.protocol.Writeable;
24
Andreas Wundsam85c961f2013-09-29 21:22:12 -070025import com.google.common.primitives.UnsignedLongs;
26
Yotam Harcholf3f11152013-09-05 16:47:16 -070027public class U64 implements Writeable, OFValueType<U64> {
28 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070029 private final static long ZERO_VAL = 0;
30 public final static U64 ZERO = new U64(ZERO_VAL);
Yotam Harcholf3f11152013-09-05 16:47:16 -070031
32 private final long raw;
33
34 protected U64(final long raw) {
35 this.raw = raw;
36 }
37
38 public static U64 of(long raw) {
39 return ofRaw(raw);
40 }
41
42 public static U64 ofRaw(final long raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070043 if(raw == ZERO_VAL)
44 return ZERO;
Yotam Harcholf3f11152013-09-05 16:47:16 -070045 return new U64(raw);
46 }
47
48 public static U64 parseHex(String hex) {
49 return new U64(new BigInteger(hex, 16).longValue());
50 }
51
52 public long getValue() {
53 return raw;
54 }
55
56 public BigInteger getBigInteger() {
57 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
58 if (raw < 0) {
59 bigInt = bigInt.setBit(Long.SIZE - 1);
60 }
61 return bigInt;
62 }
63
64 @Override
65 public String toString() {
66 return getBigInteger().toString();
67 }
68
69 public static BigInteger f(final long value) {
70 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
71 if (value < 0) {
72 bigInt = bigInt.setBit(Long.SIZE - 1);
73 }
74 return bigInt;
75 }
76
77 public static long t(final BigInteger l) {
78 return l.longValue();
79 }
80
81 @Override
82 public int hashCode() {
83 final int prime = 31;
84 int result = 1;
85 result = prime * result + (int) (raw ^ (raw >>> 32));
86 return result;
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj)
92 return true;
93 if (obj == null)
94 return false;
95 if (getClass() != obj.getClass())
96 return false;
97 U64 other = (U64) obj;
98 if (raw != other.raw)
99 return false;
100 return true;
101 }
102
103 @Override
104 public int getLength() {
105 return 8;
106 }
107
108 @Override
109 public U64 applyMask(U64 mask) {
110 return ofRaw(raw & mask.raw);
111 }
112
113 @Override
114 public void writeTo(ChannelBuffer bb) {
115 bb.writeLong(raw);
116 }
117
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700118 @Override
119 public int compareTo(U64 o) {
120 return UnsignedLongs.compare(raw, o.raw);
121 }
122
Yotam Harcholf3f11152013-09-05 16:47:16 -0700123}