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