blob: 5de62dc3e8c23fdaf67f39e516382ff154e3dff1 [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
25public class U64 implements Writeable, OFValueType<U64> {
26 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
27
28 private final long raw;
29
30 protected U64(final long raw) {
31 this.raw = raw;
32 }
33
34 public static U64 of(long raw) {
35 return ofRaw(raw);
36 }
37
38 public static U64 ofRaw(final long raw) {
39 return new U64(raw);
40 }
41
42 public static U64 parseHex(String hex) {
43 return new U64(new BigInteger(hex, 16).longValue());
44 }
45
46 public long getValue() {
47 return raw;
48 }
49
50 public BigInteger getBigInteger() {
51 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
52 if (raw < 0) {
53 bigInt = bigInt.setBit(Long.SIZE - 1);
54 }
55 return bigInt;
56 }
57
58 @Override
59 public String toString() {
60 return getBigInteger().toString();
61 }
62
63 public static BigInteger f(final long value) {
64 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
65 if (value < 0) {
66 bigInt = bigInt.setBit(Long.SIZE - 1);
67 }
68 return bigInt;
69 }
70
71 public static long t(final BigInteger l) {
72 return l.longValue();
73 }
74
75 @Override
76 public int hashCode() {
77 final int prime = 31;
78 int result = 1;
79 result = prime * result + (int) (raw ^ (raw >>> 32));
80 return result;
81 }
82
83 @Override
84 public boolean equals(Object obj) {
85 if (this == obj)
86 return true;
87 if (obj == null)
88 return false;
89 if (getClass() != obj.getClass())
90 return false;
91 U64 other = (U64) obj;
92 if (raw != other.raw)
93 return false;
94 return true;
95 }
96
97 @Override
98 public int getLength() {
99 return 8;
100 }
101
102 @Override
103 public U64 applyMask(U64 mask) {
104 return ofRaw(raw & mask.raw);
105 }
106
107 @Override
108 public void writeTo(ChannelBuffer bb) {
109 bb.writeLong(raw);
110 }
111
112}