blob: c69786c8c05e31411bfff56606bfb361733fa80f [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -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
25import com.google.common.hash.PrimitiveSink;
26import com.google.common.primitives.UnsignedInts;
27
28public class U32 implements Writeable, OFValueType<U32> {
29 private final static int ZERO_VAL = 0;
30 public final static U32 ZERO = new U32(ZERO_VAL);
31
32 private static final int NO_MASK_VAL = 0xFFffFFff;
33 public final static U32 NO_MASK = new U32(NO_MASK_VAL);
34 public static final U32 FULL_MASK = ZERO;
35
36 private final int raw;
37
38 private U32(int raw) {
39 this.raw = raw;
40 }
41
42 public static U32 of(long value) {
43 return ofRaw(U32.t(value));
44 }
45
46 public static U32 ofRaw(int raw) {
47 if(raw == ZERO_VAL)
48 return ZERO;
49 if(raw == NO_MASK_VAL)
50 return NO_MASK;
51 return new U32(raw);
52 }
53
54 public long getValue() {
55 return f(raw);
56 }
57
58 public int getRaw() {
59 return raw;
60 }
61
62 @Override
63 public String toString() {
64 return String.format("0x%08x", raw);
65 }
66
67 @Override
68 public int hashCode() {
69 final int prime = 31;
70 int result = 1;
71 result = prime * result + raw;
72 return result;
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (this == obj)
78 return true;
79 if (obj == null)
80 return false;
81 if (getClass() != obj.getClass())
82 return false;
83 U32 other = (U32) obj;
84 if (raw != other.raw)
85 return false;
86
87 return true;
88 }
89
90 public static long f(final int i) {
91 return i & 0xffffffffL;
92 }
93
94 public static int t(final long l) {
95 return (int) l;
96 }
97
98 @Override
99 public void writeTo(ChannelBuffer bb) {
100 bb.writeInt(raw);
101 }
102
103 public final static Reader READER = new Reader();
104
105 private static class Reader implements OFMessageReader<U32> {
106 @Override
107 public U32 readFrom(ChannelBuffer bb) throws OFParseError {
108 return new U32(bb.readInt());
109 }
110 }
111
112 @Override
113 public int getLength() {
114 return 4;
115 }
116
117 @Override
118 public U32 applyMask(U32 mask) {
119 return ofRaw(raw & mask.raw);
120 }
121
122 @Override
123 public int compareTo(U32 o) {
124 return UnsignedInts.compare(raw, o.raw);
125 }
126
127 @Override
128 public void putTo(PrimitiveSink sink) {
129 sink.putInt(raw);
130 }}