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