blob: 5c69a7c7173c1aff1c771f8f68302f4512cb5540 [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -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.openflow.types;
19
20import java.math.BigInteger;
21
Andreas Wundsam880a2a82013-08-22 07:55:14 -070022import org.jboss.netty.buffer.ChannelBuffer;
23import org.openflow.protocol.Writeable;
24
25public class U64 implements Writeable, OFValueType<U64> {
Andreas Wundsam16b9d872013-07-18 19:58:10 -070026 private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070027
28 private final long raw;
29
Yotam Harchol6fccde62013-08-15 12:04:52 -070030 protected U64(final long raw) {
Andreas Wundsam40e14f72013-05-06 14:49:08 -070031 this.raw = raw;
32 }
33
Andreas Wundsam880a2a82013-08-22 07:55:14 -070034 public static U64 of(long raw) {
35 return ofRaw(raw);
36 }
37
38 public static U64 ofRaw(final long raw) {
Andreas Wundsam40e14f72013-05-06 14:49:08 -070039 return new U64(raw);
40 }
41
Andreas Wundsamf4abdd32013-08-01 22:12:06 -070042 public static U64 parseHex(String hex) {
43 return new U64(new BigInteger(hex, 16).longValue());
44 }
45
Andreas Wundsam40e14f72013-05-06 14:49:08 -070046 public long getValue() {
47 return raw;
48 }
49
50 public BigInteger getBigInteger() {
Andreas Wundsam16b9d872013-07-18 19:58:10 -070051 BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
52 if (raw < 0) {
53 bigInt = bigInt.setBit(Long.SIZE - 1);
54 }
55 return bigInt;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070056 }
57
58 @Override
59 public String toString() {
60 return getBigInteger().toString();
61 }
62
Andreas Wundsam16b9d872013-07-18 19:58:10 -070063 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;
Andreas Wundsam40e14f72013-05-06 14:49:08 -070069 }
70
71 public static long t(final BigInteger l) {
72 return l.longValue();
73 }
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070074
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
Andreas Wundsam880a2a82013-08-22 07:55:14 -070097 @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
Andreas Wundsam40e14f72013-05-06 14:49:08 -0700112}