Class DoubleCodec

  • Direct Known Subclasses:
    NoopDoubleCodec

    public abstract class DoubleCodec
    extends java.lang.Object
    A class for compressing and decompressing doubles. There are various methods for compressing either a single double, or multiple ones in one call. Similar methods exist for decompression. The compression itself is identical for all those methods. The difference they provide is in how much reuse of buffer data structures is allowed. Implementors need to implement only compressDouble(long,byte[],int) for compressing and decompressDouble(byte[],int,MutableDouble) for decompressing. The other methods for compression or decompression can be override if a better implementation can be provided. In addition, compressedSize(byte[], int) must provide the number of bytes for a certain compressed value. This is required as many decompressing methods to not offer feedback on how much data they've read. For testing and debug purposes, implementors need to provide describeCompressedValue(byte[], int, double). The value that is returned from DoubleCodec.CompressionInfo.compressedType() is used to call describeCompression(int) so that one should be implemented accordingly.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static int EXP_BIAS
      Bias used in representing a double exponent.
      protected static long EXP_BIT_MASK
      Bit mask to isolate the exponent field of a double.
      protected static int SIGNIFICAND_BITS
      The number of physical bits in the significand of a double number.
      protected static long SIGNIFICANT_BIT_MASK
      Bit mask to isolate the significand field of a double.
      protected static int SUPER_NORMAL_EXPONENT
      Exponent in representing a NaN or Infinity value.
    • Constructor Summary

      Constructors 
      Constructor Description
      DoubleCodec()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      int compressDouble​(double value, byte[] out)
      Compress a single double.
      abstract int compressDouble​(long doubleBits, byte[] out, int outPos)
      Compress the double from its bit representation and write result into out.
      int compressDoubles​(double[] data, int length, byte[] out)
      Compress many doubles in one call.
      abstract int compressedSize​(byte[] data, int pos)
      Return the number of bytes used to compress the current value.
      double decompressDouble​(byte[] data, int pos)
      Decompress a single double from the given byte array and return it.
      abstract int decompressDouble​(byte[] data, int pos, org.apache.commons.lang3.mutable.MutableDouble out)
      Decompress a single double from the given byte array and write the result into out.
      double[] decompressDoubles​(byte[] data, int length)
      Decompress may doubles in one call.
      abstract DoubleCodec.CompressionInfo describeCompressedValue​(byte[] data, int pos, double originalInput)
      Return debug info about how the current value is compressed.
      abstract java.lang.String describeCompression​(int type)
      Return some string description on how the data is compressed.
      protected static byte getSign​(long bits)
      Get the sign bit of a bit representation of a double.
      protected static long getSignificand​(long bits)
      Get the significand of a bit representation of a double.
      protected static int getUnbiasedExponent​(long bits)
      Get the unbiased exponent of a bit representation of a double.
      int supportedSignificandWith()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SIGNIFICAND_BITS

        protected static final int SIGNIFICAND_BITS
        The number of physical bits in the significand of a double number.
        See Also:
        Constant Field Values
      • EXP_BIAS

        protected static final int EXP_BIAS
        Bias used in representing a double exponent.
        See Also:
        Constant Field Values
      • SUPER_NORMAL_EXPONENT

        protected static final int SUPER_NORMAL_EXPONENT
        Exponent in representing a NaN or Infinity value.
        See Also:
        Constant Field Values
      • EXP_BIT_MASK

        protected static final long EXP_BIT_MASK
        Bit mask to isolate the exponent field of a double.
        See Also:
        Constant Field Values
      • SIGNIFICANT_BIT_MASK

        protected static final long SIGNIFICANT_BIT_MASK
        Bit mask to isolate the significand field of a double.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DoubleCodec

        public DoubleCodec()
    • Method Detail

      • getSign

        protected static byte getSign​(long bits)
        Get the sign bit of a bit representation of a double.
      • getUnbiasedExponent

        protected static int getUnbiasedExponent​(long bits)
        Get the unbiased exponent of a bit representation of a double.
      • getSignificand

        protected static long getSignificand​(long bits)
        Get the significand of a bit representation of a double.
      • compressDoubles

        public int compressDoubles​(double[] data,
                                   int length,
                                   byte[] out)
        Compress many doubles in one call.
        Parameters:
        data - the input data to compress.
        length - how many value to compress, reading from data starting at index 0 upto index length.
        out - the output buffer where the compressed values are written to.
        Returns:
        the number of bytes written into out.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if out is too small.
      • compressDouble

        public int compressDouble​(double value,
                                  byte[] out)
        Compress a single double.
        Parameters:
        value - the value to compress.
        out - the output buffer where the compressed value is written to.
        Returns:
        the number of bytes written into out. Technically, this is the next position to start writing to, but since we write to the beginning of the array, this is equal to the number of bytes written.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if out is too small.
      • decompressDoubles

        public double[] decompressDoubles​(byte[] data,
                                          int length)
        Decompress may doubles in one call. The data is read from index 0. There is no way to know how many bytes have been read.
        Parameters:
        data - the compressed data.
        length - how many values to decompress.
        Returns:
        the decompressed values.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if data did not contain enough data for all values.
      • compressDouble

        public abstract int compressDouble​(long doubleBits,
                                           byte[] out,
                                           int outPos)
        Compress the double from its bit representation and write result into out.
        Parameters:
        doubleBits - the double value as converted by Double.doubleToRawLongBits(double).
        out - the output buffer where the compressed value is written to.
        outPos - at which position to write the compressed value in out.
        Returns:
        the new value of outPos (NOT the number of bytes written).
      • decompressDouble

        public abstract int decompressDouble​(byte[] data,
                                             int pos,
                                             org.apache.commons.lang3.mutable.MutableDouble out)
        Decompress a single double from the given byte array and write the result into out.
        Parameters:
        data - the compressed data.
        pos - start reading from data at this position.
        out - output value, the result should be written using MutableDouble.doubleValue().
        Returns:
        the new value of pos after reading the compressed value.
      • decompressDouble

        public double decompressDouble​(byte[] data,
                                       int pos)
        Decompress a single double from the given byte array and return it. There is no way to know how many bytes have been read.
        Parameters:
        data - the compressed data.
        pos - start reading from data at this position.
        Returns:
        the decompressed value.
      • compressedSize

        public abstract int compressedSize​(byte[] data,
                                           int pos)
        Return the number of bytes used to compress the current value.
        Parameters:
        data - the compressed data.
        pos - start reading from data at this position.
        Returns:
        the number of bytes that the compressed value at pos is.
      • describeCompression

        @TestOnly
        public abstract java.lang.String describeCompression​(int type)
        Return some string description on how the data is compressed. For debugging or testing.
        Parameters:
        type - a type identifier.
        Returns:
        some string for describing how the data is compressed.
      • describeCompressedValue

        @TestOnly
        public abstract DoubleCodec.CompressionInfo describeCompressedValue​(byte[] data,
                                                                            int pos,
                                                                            double originalInput)
        Return debug info about how the current value is compressed. For debugging or testing.
        Parameters:
        data - the compressed data.
        pos - start reading from data at this position.
        Returns:
        info object describing the current compressed value.
      • supportedSignificandWith

        @TestOnly
        public int supportedSignificandWith()
        Returns:
        the guaranteed maximum significand width. If the compression is lossless, this value must equal SIGNIFICAND_WIDTH.