blob: 9fafc30f6fab58d48d097fbb487e461988592364 [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 org.jboss.netty.buffer.ChannelBuffer;
21import org.projectfloodlight.openflow.exceptions.OFParseError;
22import org.projectfloodlight.openflow.protocol.OFMessageReader;
23import org.projectfloodlight.openflow.protocol.Writeable;
24
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070025import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -070026import com.google.common.primitives.UnsignedInts;
27
Yotam Harcholf3f11152013-09-05 16:47:16 -070028public class U32 implements Writeable, OFValueType<U32> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070029 private final static int ZERO_VAL = 0;
30 public final static U32 ZERO = new U32(ZERO_VAL);
31
Yotam Harcholf3f11152013-09-05 16:47:16 -070032 private final int raw;
33
34 private U32(int raw) {
35 this.raw = raw;
36 }
37
38 public static U32 of(long value) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070039 return ofRaw(U32.t(value));
Yotam Harcholf3f11152013-09-05 16:47:16 -070040 }
41
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070042 public static U32 ofRaw(int raw) {
43 if(raw == ZERO_VAL)
44 return ZERO;
45 return new U32(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070046 }
47
48 public long getValue() {
49 return f(raw);
50 }
51
52 public int getRaw() {
53 return raw;
54 }
55
56 @Override
57 public String toString() {
58 return "" + f(raw);
59 }
60
61 @Override
62 public int hashCode() {
63 final int prime = 31;
64 int result = 1;
65 result = prime * result + raw;
66 return result;
67 }
68
69 @Override
70 public boolean equals(Object obj) {
71 if (this == obj)
72 return true;
73 if (obj == null)
74 return false;
75 if (getClass() != obj.getClass())
76 return false;
77 U32 other = (U32) obj;
78 if (raw != other.raw)
79 return false;
80
81 return true;
82 }
83
84 public static long f(final int i) {
85 return i & 0xffffffffL;
86 }
87
88 public static int t(final long l) {
89 return (int) l;
90 }
91
92 @Override
93 public void writeTo(ChannelBuffer bb) {
94 bb.writeInt(raw);
95 }
96
97 public final static Reader READER = new Reader();
98
99 private static class Reader implements OFMessageReader<U32> {
100 @Override
101 public U32 readFrom(ChannelBuffer bb) throws OFParseError {
102 return new U32(bb.readInt());
103 }
104 }
105
106 @Override
107 public int getLength() {
108 return 4;
109 }
110
111 @Override
112 public U32 applyMask(U32 mask) {
113 return ofRaw(raw & mask.raw);
114 }
115
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700116 @Override
117 public int compareTo(U32 o) {
118 return UnsignedInts.compare(raw, o.raw);
119 }
120
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700121 @Override
122 public void putTo(PrimitiveSink sink) {
123 sink.putInt(raw);
124 }}