blob: 41e740e3558bad25ab735fdf85b974b286e10300 [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 U8 implements Writeable, OFValueType<U8> {
26 private final byte raw;
27
28 private U8(byte raw) {
29 this.raw = raw;
30 }
31
32 public static final U8 of(short value) {
33 return new U8(t(value));
34 }
35
36 public static final U8 ofRaw(byte value) {
37 return new U8(value);
38 }
39
40 public short getValue() {
41 return f(raw);
42 }
43
44 public byte 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 U8 other = (U8) obj;
70 if (raw != other.raw)
71 return false;
72 return true;
73 }
74
75
76 @Override
77 public void writeTo(ChannelBuffer bb) {
78 bb.writeByte(raw);
79 }
80
81 public static short f(final byte i) {
82 return (short) (i & 0xff);
83 }
84
85 public static byte t(final short l) {
86 return (byte) l;
87 }
88
89
90 public final static Reader READER = new Reader();
91
92 private static class Reader implements OFMessageReader<U8> {
93 @Override
94 public U8 readFrom(ChannelBuffer bb) throws OFParseError {
95 return new U8(bb.readByte());
96 }
97 }
98
99 @Override
100 public int getLength() {
101 return 1;
102 }
103
104 @Override
105 public U8 applyMask(U8 mask) {
106 return ofRaw( (byte) (raw & mask.raw));
107 }
108
109}