blob: 6c4ac11873c3b7da3a29a9f0811825ec0f44f8a7 [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
Yotam Harcholf3f11152013-09-05 16:47:16 -070032 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 Wundsamb75c4ad2013-09-23 14:45:35 -070047 return ofRaw(t(value));
Yotam Harcholf3f11152013-09-05 16:47:16 -070048 }
49
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070050 public static final U16 ofRaw(short raw) {
51 if(raw == ZERO_VAL)
52 return ZERO;
53 return new U16(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070054 }
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 Wundsam8ad86992014-05-09 16:33:28 -070066 return String.format("0x%04x", raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070067 }
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 Wundsam85c961f2013-09-29 21:22:12 -0700116
117 @Override
118 public int compareTo(U16 o) {
Andreas Wundsamb79ac382013-10-07 17:01:51 -0700119 return Ints.compare(f(raw), f(o.raw));
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700120 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700121
122 @Override
123 public void putTo(PrimitiveSink sink) {
124 sink.putShort(raw);
125 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700126}