blob: 55a467e97444b7a732084c8f64d462854c4a7666 [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 U16 implements Writeable, OFValueType<U16> {
26 public static int f(final short i) {
27 return i & 0xffff;
28 }
29
30 public static short t(final int l) {
31 return (short) l;
32 }
33
34 private final short raw;
35
36 private U16(short raw) {
37 this.raw = raw;
38 }
39
40 public static final U16 of(int value) {
41 return new U16(t(value));
42 }
43
44 public static final U16 ofRaw(short value) {
45 return new U16(value);
46 }
47
48 public int getValue() {
49 return f(raw);
50 }
51
52 public short getRaw() {
53 return raw;
54 }
55
56 @Override
57 public String toString() {
58 return "" + f(raw);
59 }
60
61 @Override
62 public int hashCode() {
63 final int prime = 31;
64 int result = 1;
65 result = prime * result + raw;
66 return result;
67 }
68
69 @Override
70 public boolean equals(Object obj) {
71 if (this == obj)
72 return true;
73 if (obj == null)
74 return false;
75 if (getClass() != obj.getClass())
76 return false;
77 U16 other = (U16) obj;
78 if (raw != other.raw)
79 return false;
80 return true;
81 }
82
83
84 @Override
85 public void writeTo(ChannelBuffer bb) {
86 bb.writeShort(raw);
87 }
88
89
90 public final static Reader READER = new Reader();
91
92 private static class Reader implements OFMessageReader<U16> {
93 @Override
94 public U16 readFrom(ChannelBuffer bb) throws OFParseError {
95 return ofRaw(bb.readShort());
96 }
97 }
98
99 @Override
100 public int getLength() {
101 return 2;
102 }
103
104 @Override
105 public U16 applyMask(U16 mask) {
106 return ofRaw( (short) (raw & mask.raw));
107 }
108}