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