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