blob: f56a528ffb7b1119936e44b7b091d25048af76d4 [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
25public class U32 implements Writeable, OFValueType<U32> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070026 private final static int ZERO_VAL = 0;
27 public final static U32 ZERO = new U32(ZERO_VAL);
28
Yotam Harcholf3f11152013-09-05 16:47:16 -070029 private final int raw;
30
31 private U32(int raw) {
32 this.raw = raw;
33 }
34
35 public static U32 of(long value) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070036 return ofRaw(U32.t(value));
Yotam Harcholf3f11152013-09-05 16:47:16 -070037 }
38
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070039 public static U32 ofRaw(int raw) {
40 if(raw == ZERO_VAL)
41 return ZERO;
42 return new U32(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070043 }
44
45 public long getValue() {
46 return f(raw);
47 }
48
49 public int getRaw() {
50 return raw;
51 }
52
53 @Override
54 public String toString() {
55 return "" + f(raw);
56 }
57
58 @Override
59 public int hashCode() {
60 final int prime = 31;
61 int result = 1;
62 result = prime * result + raw;
63 return result;
64 }
65
66 @Override
67 public boolean equals(Object obj) {
68 if (this == obj)
69 return true;
70 if (obj == null)
71 return false;
72 if (getClass() != obj.getClass())
73 return false;
74 U32 other = (U32) obj;
75 if (raw != other.raw)
76 return false;
77
78 return true;
79 }
80
81 public static long f(final int i) {
82 return i & 0xffffffffL;
83 }
84
85 public static int t(final long l) {
86 return (int) l;
87 }
88
89 @Override
90 public void writeTo(ChannelBuffer bb) {
91 bb.writeInt(raw);
92 }
93
94 public final static Reader READER = new Reader();
95
96 private static class Reader implements OFMessageReader<U32> {
97 @Override
98 public U32 readFrom(ChannelBuffer bb) throws OFParseError {
99 return new U32(bb.readInt());
100 }
101 }
102
103 @Override
104 public int getLength() {
105 return 4;
106 }
107
108 @Override
109 public U32 applyMask(U32 mask) {
110 return ofRaw(raw & mask.raw);
111 }
112
113}