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; |
xinwu | f08ef68 | 2013-12-05 18:29:20 -0800 | [diff] [blame] | 23 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 24 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 26 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 27 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 28 | import com.google.common.primitives.UnsignedLongs; |
| 29 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 30 | public class U64 implements Writeable, OFValueType<U64> { |
| 31 | private static final long UNSIGNED_MASK = 0x7fffffffffffffffL; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 32 | private final static long ZERO_VAL = 0; |
| 33 | public final static U64 ZERO = new U64(ZERO_VAL); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 34 | |
| 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 Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 46 | if(raw == ZERO_VAL) |
| 47 | return ZERO; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 48 | 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 Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public int compareTo(U64 o) { |
| 123 | return UnsignedLongs.compare(raw, o.raw); |
| 124 | } |
| 125 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public void putTo(PrimitiveSink sink) { |
| 128 | sink.putLong(raw); |
| 129 | } |
xinwu | f08ef68 | 2013-12-05 18:29:20 -0800 | [diff] [blame] | 130 | |
| 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 | } |
xinwu | 9b8989c | 2013-12-06 16:01:32 -0800 | [diff] [blame] | 139 | } |