blob: 88d6b1576fc3af0f29f36d0ef767b6f940d0388d [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -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.openflow.types;
19
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070020import org.jboss.netty.buffer.ChannelBuffer;
21import org.openflow.exceptions.OFParseError;
22import org.openflow.protocol.OFMessageReader;
23import org.openflow.protocol.Writeable;
24
25public class U32 implements Writeable {
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 long getValue() {
41 return f(raw);
42 }
43
44 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 return true;
73 }
74
Andreas Wundsam40e14f72013-05-06 14:49:08 -070075 public static long f(final int i) {
76 return i & 0xffffffffL;
77 }
78
79 public static int t(final long l) {
80 return (int) l;
81 }
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070082
83 @Override
84 public void writeTo(ChannelBuffer bb) {
85 bb.writeInt(raw);
86 }
87
88 public final static Reader READER = new Reader();
89
90 private static class Reader implements OFMessageReader<U32> {
91 @Override
92 public U32 readFrom(ChannelBuffer bb) throws OFParseError {
93 return new U32(bb.readInt());
94 }
95 }
96
Andreas Wundsam40e14f72013-05-06 14:49:08 -070097}