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