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