blob: 2f8bfee9186e8a2ca6b6414f814b9f2f1621596e [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 Wundsamb79ac382013-10-07 17:01:51 -070025import com.google.common.primitives.Ints;
26
Yotam Harcholf3f11152013-09-05 16:47:16 -070027public class U16 implements Writeable, OFValueType<U16> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070028 private final static short ZERO_VAL = 0;
29 public final static U16 ZERO = new U16(ZERO_VAL);
30
Yotam Harcholf3f11152013-09-05 16:47:16 -070031 public static int f(final short i) {
32 return i & 0xffff;
33 }
34
35 public static short t(final int l) {
36 return (short) l;
37 }
38
39 private final short raw;
40
41 private U16(short raw) {
42 this.raw = raw;
43 }
44
45 public static final U16 of(int value) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070046 return ofRaw(t(value));
Yotam Harcholf3f11152013-09-05 16:47:16 -070047 }
48
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070049 public static final U16 ofRaw(short raw) {
50 if(raw == ZERO_VAL)
51 return ZERO;
52 return new U16(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070053 }
54
55 public int getValue() {
56 return f(raw);
57 }
58
59 public short getRaw() {
60 return raw;
61 }
62
63 @Override
64 public String toString() {
Andreas Wundsam85c961f2013-09-29 21:22:12 -070065 return Integer.toString(f(raw));
Yotam Harcholf3f11152013-09-05 16:47:16 -070066 }
67
68 @Override
69 public int hashCode() {
70 final int prime = 31;
71 int result = 1;
72 result = prime * result + raw;
73 return result;
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj)
79 return true;
80 if (obj == null)
81 return false;
82 if (getClass() != obj.getClass())
83 return false;
84 U16 other = (U16) obj;
85 if (raw != other.raw)
86 return false;
87 return true;
88 }
89
90
91 @Override
92 public void writeTo(ChannelBuffer bb) {
93 bb.writeShort(raw);
94 }
95
96
97 public final static Reader READER = new Reader();
98
99 private static class Reader implements OFMessageReader<U16> {
100 @Override
101 public U16 readFrom(ChannelBuffer bb) throws OFParseError {
102 return ofRaw(bb.readShort());
103 }
104 }
105
106 @Override
107 public int getLength() {
108 return 2;
109 }
110
111 @Override
112 public U16 applyMask(U16 mask) {
113 return ofRaw( (short) (raw & mask.raw));
114 }
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700115
116 @Override
117 public int compareTo(U16 o) {
Andreas Wundsamb79ac382013-10-07 17:01:51 -0700118 return Ints.compare(f(raw), f(o.raw));
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700119 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700120}