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