Rich Lane | 9c27b5d | 2013-10-30 17:14:32 -0700 | [diff] [blame] | 1 | /** |
| 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 | |
| 18 | package org.projectfloodlight.openflow.types; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 22 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 23 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 24 | |
| 25 | import com.google.common.hash.PrimitiveSink; |
| 26 | |
| 27 | public 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 Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame] | 31 | public final static OFBooleanValue NO_MASK = TRUE; |
| 32 | public final static OFBooleanValue FULL_MASK = FALSE; |
| 33 | |
Rich Lane | 9c27b5d | 2013-10-30 17:14:32 -0700 | [diff] [blame] | 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 | } |