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 org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 22 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 23 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 24 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 25 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 26 | import com.google.common.primitives.UnsignedInts; |
| 27 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | public class U32 implements Writeable, OFValueType<U32> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 29 | private final static int ZERO_VAL = 0; |
| 30 | public final static U32 ZERO = new U32(ZERO_VAL); |
| 31 | |
Andreas Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame^] | 32 | private static final int NO_MASK_VAL = 0xFFffFFff; |
| 33 | public final static U32 NO_MASK = new U32(NO_MASK_VAL); |
| 34 | public static final U32 FULL_MASK = ZERO; |
| 35 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 36 | private final int raw; |
| 37 | |
| 38 | private U32(int raw) { |
| 39 | this.raw = raw; |
| 40 | } |
| 41 | |
| 42 | public static U32 of(long value) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 43 | return ofRaw(U32.t(value)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 46 | public static U32 ofRaw(int raw) { |
| 47 | if(raw == ZERO_VAL) |
| 48 | return ZERO; |
Andreas Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame^] | 49 | if(raw == NO_MASK_VAL) |
| 50 | return NO_MASK; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 51 | return new U32(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public long getValue() { |
| 55 | return f(raw); |
| 56 | } |
| 57 | |
| 58 | public int getRaw() { |
| 59 | return raw; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String toString() { |
| 64 | return "" + f(raw); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | final int prime = 31; |
| 70 | int result = 1; |
| 71 | result = prime * result + raw; |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean equals(Object obj) { |
| 77 | if (this == obj) |
| 78 | return true; |
| 79 | if (obj == null) |
| 80 | return false; |
| 81 | if (getClass() != obj.getClass()) |
| 82 | return false; |
| 83 | U32 other = (U32) obj; |
| 84 | if (raw != other.raw) |
| 85 | return false; |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | public static long f(final int i) { |
| 91 | return i & 0xffffffffL; |
| 92 | } |
| 93 | |
| 94 | public static int t(final long l) { |
| 95 | return (int) l; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void writeTo(ChannelBuffer bb) { |
| 100 | bb.writeInt(raw); |
| 101 | } |
| 102 | |
| 103 | public final static Reader READER = new Reader(); |
| 104 | |
| 105 | private static class Reader implements OFMessageReader<U32> { |
| 106 | @Override |
| 107 | public U32 readFrom(ChannelBuffer bb) throws OFParseError { |
| 108 | return new U32(bb.readInt()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int getLength() { |
| 114 | return 4; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public U32 applyMask(U32 mask) { |
| 119 | return ofRaw(raw & mask.raw); |
| 120 | } |
| 121 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 122 | @Override |
| 123 | public int compareTo(U32 o) { |
| 124 | return UnsignedInts.compare(raw, o.raw); |
| 125 | } |
| 126 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void putTo(PrimitiveSink sink) { |
| 129 | sink.putInt(raw); |
| 130 | }} |