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 | b79ac38 | 2013-10-07 17:01:51 -0700 | [diff] [blame] | 26 | import com.google.common.primitives.Ints; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 27 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | public class U16 implements Writeable, OFValueType<U16> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 29 | private final static short ZERO_VAL = 0; |
| 30 | public final static U16 ZERO = new U16(ZERO_VAL); |
| 31 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 32 | public static int f(final short i) { |
| 33 | return i & 0xffff; |
| 34 | } |
| 35 | |
| 36 | public static short t(final int l) { |
| 37 | return (short) l; |
| 38 | } |
| 39 | |
| 40 | private final short raw; |
| 41 | |
| 42 | private U16(short raw) { |
| 43 | this.raw = raw; |
| 44 | } |
| 45 | |
| 46 | public static final U16 of(int value) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 47 | return ofRaw(t(value)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 50 | public static final U16 ofRaw(short raw) { |
| 51 | if(raw == ZERO_VAL) |
| 52 | return ZERO; |
| 53 | return new U16(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public int getValue() { |
| 57 | return f(raw); |
| 58 | } |
| 59 | |
| 60 | public short getRaw() { |
| 61 | return raw; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String toString() { |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 66 | return Integer.toString(f(raw)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int hashCode() { |
| 71 | final int prime = 31; |
| 72 | int result = 1; |
| 73 | result = prime * result + raw; |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object obj) { |
| 79 | if (this == obj) |
| 80 | return true; |
| 81 | if (obj == null) |
| 82 | return false; |
| 83 | if (getClass() != obj.getClass()) |
| 84 | return false; |
| 85 | U16 other = (U16) obj; |
| 86 | if (raw != other.raw) |
| 87 | return false; |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | @Override |
| 93 | public void writeTo(ChannelBuffer bb) { |
| 94 | bb.writeShort(raw); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public final static Reader READER = new Reader(); |
| 99 | |
| 100 | private static class Reader implements OFMessageReader<U16> { |
| 101 | @Override |
| 102 | public U16 readFrom(ChannelBuffer bb) throws OFParseError { |
| 103 | return ofRaw(bb.readShort()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int getLength() { |
| 109 | return 2; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public U16 applyMask(U16 mask) { |
| 114 | return ofRaw( (short) (raw & mask.raw)); |
| 115 | } |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 116 | |
| 117 | @Override |
| 118 | public int compareTo(U16 o) { |
Andreas Wundsam | b79ac38 | 2013-10-07 17:01:51 -0700 | [diff] [blame] | 119 | return Ints.compare(f(raw), f(o.raw)); |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 120 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 121 | |
| 122 | @Override |
| 123 | public void putTo(PrimitiveSink sink) { |
| 124 | sink.putShort(raw); |
| 125 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 126 | } |