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