001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.io;
020
021import java.io.*;
022
023import org.apache.hadoop.classification.InterfaceAudience;
024import org.apache.hadoop.classification.InterfaceStability;
025
026/** 
027 * A WritableComparable for booleans. 
028 */
029@InterfaceAudience.Public
030@InterfaceStability.Stable
031public class BooleanWritable implements WritableComparable<BooleanWritable> {
032  private boolean value;
033
034  /** 
035   */
036  public BooleanWritable() {};
037
038  /** 
039   */
040  public BooleanWritable(boolean value) {
041    set(value);
042  }
043
044  /** 
045   * Set the value of the BooleanWritable
046   */    
047  public void set(boolean value) {
048    this.value = value;
049  }
050
051  /**
052   * Returns the value of the BooleanWritable
053   */
054  public boolean get() {
055    return value;
056  }
057
058  /**
059   */
060  public void readFields(DataInput in) throws IOException {
061    value = in.readBoolean();
062  }
063
064  /**
065   */
066  public void write(DataOutput out) throws IOException {
067    out.writeBoolean(value);
068  }
069
070  /**
071   */
072  @Override
073  public boolean equals(Object o) {
074    if (!(o instanceof BooleanWritable)) {
075      return false;
076    }
077    BooleanWritable other = (BooleanWritable) o;
078    return this.value == other.value;
079  }
080
081  @Override
082  public int hashCode() {
083    return value ? 0 : 1;
084  }
085
086
087
088  /**
089   */
090  @Override
091  public int compareTo(BooleanWritable o) {
092    boolean a = this.value;
093    boolean b = o.value;
094    return ((a == b) ? 0 : (a == false) ? -1 : 1);
095  }
096  
097  @Override
098  public String toString() {
099    return Boolean.toString(get());
100  }
101
102  /** 
103   * A Comparator optimized for BooleanWritable. 
104   */ 
105  public static class Comparator extends WritableComparator {
106    public Comparator() {
107      super(BooleanWritable.class);
108    }
109
110    @Override
111    public int compare(byte[] b1, int s1, int l1,
112                       byte[] b2, int s2, int l2) {
113      return compareBytes(b1, s1, l1, b2, s2, l2);
114    }
115  }
116
117
118  static {
119    WritableComparator.define(BooleanWritable.class, new Comparator());
120  }
121}