Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | /** |
| 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 | |
| 18 | package org.projectfloodlight.openflow.types; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 22 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 23 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 24 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 25 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 26 | import com.google.common.primitives.UnsignedBytes; |
| 27 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | public class U8 implements Writeable, OFValueType<U8> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 29 | private final static byte ZERO_VAL = 0; |
| 30 | public final static U8 ZERO = new U8(ZERO_VAL); |
| 31 | |
Andreas Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame] | 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 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 43 | if(value == ZERO_VAL) |
| 44 | return ZERO; |
Andreas Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame] | 45 | if(value == NO_MASK_VAL) |
| 46 | return NO_MASK; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 47 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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() { |
Andreas Wundsam | 8ad8699 | 2014-05-09 16:33:28 -0700 | [diff] [blame] | 65 | return String.format("0x%02x", raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public int compareTo(U8 o) { |
| 126 | return UnsignedBytes.compare(raw, o.raw); |
| 127 | } |
| 128 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public void putTo(PrimitiveSink sink) { |
| 131 | sink.putByte(raw); |
| 132 | } |
| 133 | } |