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