blob: e276092775eb96635cb269592ad238a3c53a1113 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -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;
26
27public class OFBooleanValue implements Writeable, OFValueType<OFBooleanValue> {
28 public final static OFBooleanValue TRUE = new OFBooleanValue(true);
29 public final static OFBooleanValue FALSE = new OFBooleanValue(false);
30
31 public final static OFBooleanValue NO_MASK = TRUE;
32 public final static OFBooleanValue FULL_MASK = FALSE;
33
34 private final boolean value;
35
36 private OFBooleanValue(boolean value) {
37 this.value = value;
38 }
39
40 public static OFBooleanValue of(boolean value) {
41 return value ? TRUE : FALSE;
42 }
43
44 public boolean getValue() {
45 return value;
46 }
47
48 public int getInt() {
49 return value ? 1 : 0;
50 }
51
52 @Override
53 public String toString() {
54 return "" + value;
55 }
56
57 @Override
58 public int hashCode() {
59 final int prime = 31;
60 int result = 1;
61 result = prime * result + getInt();
62 return result;
63 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (this == obj)
68 return true;
69 if (obj == null)
70 return false;
71 if (getClass() != obj.getClass())
72 return false;
73 OFBooleanValue other = (OFBooleanValue) obj;
74 if (value != other.value)
75 return false;
76 return true;
77 }
78
79 @Override
80 public void writeTo(ChannelBuffer bb) {
81 bb.writeByte(getInt());
82 }
83
84 private static class Reader implements OFMessageReader<OFBooleanValue> {
85 @Override
86 public OFBooleanValue readFrom(ChannelBuffer bb) throws OFParseError {
87 return of(bb.readByte() != 0);
88 }
89 }
90
91 @Override
92 public int getLength() {
93 return 1;
94 }
95
96 @Override
97 public OFBooleanValue applyMask(OFBooleanValue mask) {
98 return of(value && mask.value);
99 }
100
101 @Override
102 public int compareTo(OFBooleanValue o) {
103 return getInt() - o.getInt();
104 }
105
106 @Override
107 public void putTo(PrimitiveSink sink) {
108 sink.putByte((byte)getInt());
109 }
110}