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    /** A WritableComparable for floats. */
027    @InterfaceAudience.Public
028    @InterfaceStability.Stable
029    public class FloatWritable implements WritableComparable<FloatWritable> {
030      private float value;
031    
032      public FloatWritable() {}
033    
034      public FloatWritable(float value) { set(value); }
035    
036      /** Set the value of this FloatWritable. */
037      public void set(float value) { this.value = value; }
038    
039      /** Return the value of this FloatWritable. */
040      public float get() { return value; }
041    
042      public void readFields(DataInput in) throws IOException {
043        value = in.readFloat();
044      }
045    
046      public void write(DataOutput out) throws IOException {
047        out.writeFloat(value);
048      }
049    
050      /** Returns true iff <code>o</code> is a FloatWritable with the same value. */
051      @Override
052      public boolean equals(Object o) {
053        if (!(o instanceof FloatWritable))
054          return false;
055        FloatWritable other = (FloatWritable)o;
056        return this.value == other.value;
057      }
058    
059      @Override
060      public int hashCode() {
061        return Float.floatToIntBits(value);
062      }
063    
064      /** Compares two FloatWritables. */
065      @Override
066      public int compareTo(FloatWritable o) {
067        float thisValue = this.value;
068        float thatValue = o.value;
069        return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
070      }
071    
072      @Override
073      public String toString() {
074        return Float.toString(value);
075      }
076    
077      /** A Comparator optimized for FloatWritable. */ 
078      public static class Comparator extends WritableComparator {
079        public Comparator() {
080          super(FloatWritable.class);
081        }
082        @Override
083        public int compare(byte[] b1, int s1, int l1,
084                           byte[] b2, int s2, int l2) {
085          float thisValue = readFloat(b1, s1);
086          float thatValue = readFloat(b2, s2);
087          return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
088        }
089      }
090    
091      static {                                        // register this comparator
092        WritableComparator.define(FloatWritable.class, new Comparator());
093      }
094    
095    }
096