blob: 2466ffc6b31e80d19968f17fdeaea73abdc87e65 [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
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070025import com.google.common.hash.PrimitiveSink;
Andreas Wundsamb79ac382013-10-07 17:01:51 -070026import com.google.common.primitives.Ints;
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070027
Yotam Harcholf3f11152013-09-05 16:47:16 -070028public class U16 implements Writeable, OFValueType<U16> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070029 private final static short ZERO_VAL = 0;
30 public final static U16 ZERO = new U16(ZERO_VAL);
31
Rich Lane9b178072014-05-19 15:31:55 -070032 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
Yotam Harcholf3f11152013-09-05 16:47:16 -070036 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) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070051 return ofRaw(t(value));
Yotam Harcholf3f11152013-09-05 16:47:16 -070052 }
53
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070054 public static final U16 ofRaw(short raw) {
55 if(raw == ZERO_VAL)
56 return ZERO;
57 return new U16(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070058 }
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() {
Andreas Wundsam8ad86992014-05-09 16:33:28 -070070 return String.format("0x%04x", raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070071 }
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 }
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700120
121 @Override
122 public int compareTo(U16 o) {
Andreas Wundsamb79ac382013-10-07 17:01:51 -0700123 return Ints.compare(f(raw), f(o.raw));
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700124 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700125
126 @Override
127 public void putTo(PrimitiveSink sink) {
128 sink.putShort(raw);
129 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700130}