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    
019    package org.apache.hadoop.io;
020    
021    import java.io.*;
022    
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    
026    /** 
027     * A WritableComparable for booleans. 
028     */
029    @InterfaceAudience.Public
030    @InterfaceStability.Stable
031    public 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      @Override
061      public void readFields(DataInput in) throws IOException {
062        value = in.readBoolean();
063      }
064    
065      /**
066       */
067      @Override
068      public void write(DataOutput out) throws IOException {
069        out.writeBoolean(value);
070      }
071    
072      /**
073       */
074      @Override
075      public boolean equals(Object o) {
076        if (!(o instanceof BooleanWritable)) {
077          return false;
078        }
079        BooleanWritable other = (BooleanWritable) o;
080        return this.value == other.value;
081      }
082    
083      @Override
084      public int hashCode() {
085        return value ? 0 : 1;
086      }
087    
088    
089    
090      /**
091       */
092      @Override
093      public int compareTo(BooleanWritable o) {
094        boolean a = this.value;
095        boolean b = o.value;
096        return ((a == b) ? 0 : (a == false) ? -1 : 1);
097      }
098      
099      @Override
100      public String toString() {
101        return Boolean.toString(get());
102      }
103    
104      /** 
105       * A Comparator optimized for BooleanWritable. 
106       */ 
107      public static class Comparator extends WritableComparator {
108        public Comparator() {
109          super(BooleanWritable.class);
110        }
111    
112        @Override
113        public int compare(byte[] b1, int s1, int l1,
114                           byte[] b2, int s2, int l2) {
115          return compareBytes(b1, s1, l1, b2, s2, l2);
116        }
117      }
118    
119    
120      static {
121        WritableComparator.define(BooleanWritable.class, new Comparator());
122      }
123    }