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