blob: daf5fa685cc6beb3445b7e738dfd754117fa74be [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 Wundsam85c961f2013-09-29 21:22:12 -070025import com.google.common.primitives.UnsignedInts;
26
Yotam Harcholf3f11152013-09-05 16:47:16 -070027public class U32 implements Writeable, OFValueType<U32> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070028 private final static int ZERO_VAL = 0;
29 public final static U32 ZERO = new U32(ZERO_VAL);
30
Yotam Harcholf3f11152013-09-05 16:47:16 -070031 private final int raw;
32
33 private U32(int raw) {
34 this.raw = raw;
35 }
36
37 public static U32 of(long value) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070038 return ofRaw(U32.t(value));
Yotam Harcholf3f11152013-09-05 16:47:16 -070039 }
40
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070041 public static U32 ofRaw(int raw) {
42 if(raw == ZERO_VAL)
43 return ZERO;
44 return new U32(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070045 }
46
47 public long getValue() {
48 return f(raw);
49 }
50
51 public int getRaw() {
52 return raw;
53 }
54
55 @Override
56 public String toString() {
57 return "" + f(raw);
58 }
59
60 @Override
61 public int hashCode() {
62 final int prime = 31;
63 int result = 1;
64 result = prime * result + raw;
65 return result;
66 }
67
68 @Override
69 public boolean equals(Object obj) {
70 if (this == obj)
71 return true;
72 if (obj == null)
73 return false;
74 if (getClass() != obj.getClass())
75 return false;
76 U32 other = (U32) obj;
77 if (raw != other.raw)
78 return false;
79
80 return true;
81 }
82
83 public static long f(final int i) {
84 return i & 0xffffffffL;
85 }
86
87 public static int t(final long l) {
88 return (int) l;
89 }
90
91 @Override
92 public void writeTo(ChannelBuffer bb) {
93 bb.writeInt(raw);
94 }
95
96 public final static Reader READER = new Reader();
97
98 private static class Reader implements OFMessageReader<U32> {
99 @Override
100 public U32 readFrom(ChannelBuffer bb) throws OFParseError {
101 return new U32(bb.readInt());
102 }
103 }
104
105 @Override
106 public int getLength() {
107 return 4;
108 }
109
110 @Override
111 public U32 applyMask(U32 mask) {
112 return ofRaw(raw & mask.raw);
113 }
114
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700115 @Override
116 public int compareTo(U32 o) {
117 return UnsignedInts.compare(raw, o.raw);
118 }
119
Yotam Harcholf3f11152013-09-05 16:47:16 -0700120}