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      public String toString() {
039        return "(null)";
040      }
041    
042      @Override
043      public int hashCode() { return 0; }
044      
045      @Override
046      public int compareTo(NullWritable other) {
047        return 0;
048      }
049      public boolean equals(Object other) { return other instanceof NullWritable; }
050      public void readFields(DataInput in) throws IOException {}
051      public void write(DataOutput out) throws IOException {}
052    
053      /** A Comparator &quot;optimized&quot; for NullWritable. */
054      public static class Comparator extends WritableComparator {
055        public Comparator() {
056          super(NullWritable.class);
057        }
058    
059        /**
060         * Compare the buffers in serialized form.
061         */
062        @Override
063        public int compare(byte[] b1, int s1, int l1,
064                           byte[] b2, int s2, int l2) {
065          assert 0 == l1;
066          assert 0 == l2;
067          return 0;
068        }
069      }
070    
071      static {                                        // register this comparator
072        WritableComparator.define(NullWritable.class, new Comparator());
073      }
074    }
075