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    /** Singleton Writable with no data. */
027    @InterfaceAudience.Public
028    @InterfaceStability.Stable
029    public class NullWritable implements WritableComparable<NullWritable> {
030    
031      private static final NullWritable THIS = new NullWritable();
032    
033      private NullWritable() {}                       // no public ctor
034    
035      /** Returns the single instance of this class. */
036      public static NullWritable get() { return THIS; }
037      
038      @Override
039      public String toString() {
040        return "(null)";
041      }
042    
043      @Override
044      public int hashCode() { return 0; }
045      
046      @Override
047      public int compareTo(NullWritable other) {
048        return 0;
049      }
050      @Override
051      public boolean equals(Object other) { return other instanceof NullWritable; }
052      @Override
053      public void readFields(DataInput in) throws IOException {}
054      @Override
055      public void write(DataOutput out) throws IOException {}
056    
057      /** A Comparator &quot;optimized&quot; for NullWritable. */
058      public static class Comparator extends WritableComparator {
059        public Comparator() {
060          super(NullWritable.class);
061        }
062    
063        /**
064         * Compare the buffers in serialized form.
065         */
066        @Override
067        public int compare(byte[] b1, int s1, int l1,
068                           byte[] b2, int s2, int l2) {
069          assert 0 == l1;
070          assert 0 == l2;
071          return 0;
072        }
073      }
074    
075      static {                                        // register this comparator
076        WritableComparator.define(NullWritable.class, new Comparator());
077      }
078    }
079