blob: 99416a3f2cea70b3f14689ee93087ddcd9d71642 [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;
Sovietaced8fe84f32015-07-22 22:17:31 -040021import org.projectfloodlight.openflow.exceptions.OFParseError;
22import org.projectfloodlight.openflow.protocol.OFMessageReader;
Rich Lane9c27b5d2013-10-30 17:14:32 -070023import 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
Andreas Wundsam14ae8c62013-11-05 17:15:35 -080031 public final static OFBooleanValue NO_MASK = TRUE;
32 public final static OFBooleanValue FULL_MASK = FALSE;
33
Sovietaced8fe84f32015-07-22 22:17:31 -040034 public final static Reader READER_INSTANCE = new Reader();
35
Rich Lane9c27b5d2013-10-30 17:14:32 -070036 private final boolean value;
37
38 private OFBooleanValue(boolean value) {
39 this.value = value;
40 }
41
42 public static OFBooleanValue of(boolean value) {
43 return value ? TRUE : FALSE;
44 }
45
46 public boolean getValue() {
47 return value;
48 }
49
50 public int getInt() {
51 return value ? 1 : 0;
52 }
53
54 @Override
55 public String toString() {
56 return "" + value;
57 }
58
59 @Override
60 public int hashCode() {
61 final int prime = 31;
62 int result = 1;
63 result = prime * result + getInt();
64 return result;
65 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (this == obj)
70 return true;
71 if (obj == null)
72 return false;
73 if (getClass() != obj.getClass())
74 return false;
75 OFBooleanValue other = (OFBooleanValue) obj;
76 if (value != other.value)
77 return false;
78 return true;
79 }
80
81 @Override
82 public void writeTo(ChannelBuffer bb) {
83 bb.writeByte(getInt());
84 }
85
Sovietaced8fe84f32015-07-22 22:17:31 -040086 private static class Reader implements OFMessageReader<OFBooleanValue> {
87 @Override
88 public OFBooleanValue readFrom(ChannelBuffer bb) throws OFParseError {
89 return of(bb.readByte() != 0);
90 }
91 }
92
Rich Lane9c27b5d2013-10-30 17:14:32 -070093 @Override
94 public int getLength() {
95 return 1;
96 }
97
98 @Override
99 public OFBooleanValue applyMask(OFBooleanValue mask) {
100 return of(value && mask.value);
101 }
102
103 @Override
104 public int compareTo(OFBooleanValue o) {
105 return getInt() - o.getInt();
106 }
107
108 @Override
109 public void putTo(PrimitiveSink sink) {
110 sink.putByte((byte)getInt());
111 }
112}