/*
 * Copyright (c) 2019 The Broad Institute
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package htsjdk.samtools.cram.compression;

import htsjdk.samtools.cram.compression.rans.RANS4x8Decode;
import htsjdk.samtools.cram.compression.rans.RANS4x8Encode;
import htsjdk.samtools.cram.compression.rans.RANS4x8Params;
import htsjdk.samtools.cram.compression.rans.RANSParams;
import htsjdk.samtools.cram.structure.CRAMCodecModelContext;
import htsjdk.samtools.cram.structure.block.BlockCompressionMethod;
import java.util.Objects;

/**
 * CRAM external compressor that uses the rANS 4x8 entropy coder (CRAM 3.0).
 * Wraps shared {@link RANS4x8Encode} and {@link RANS4x8Decode} instances to
 * avoid repeated allocation of large internal tables.
 */
public final class RANS4x8ExternalCompressor extends ExternalCompressor {
    private final RANSParams.ORDER order;
    private final RANS4x8Encode ransEncode;
    private final RANS4x8Decode ransDecode;

    /**
     * We use a shared RANS instance for all compressors.
     * @param ransEncode ransEncoder to use
     * @param ransDecode ransDecoder to use
     */
    public RANS4x8ExternalCompressor(final RANS4x8Encode ransEncode, final RANS4x8Decode ransDecode) {
        this(RANSParams.ORDER.ZERO, ransEncode, ransDecode);
    }

    /**
     * Create a rANS 4x8 compressor with the specified order (as an integer).
     *
     * @param order the rANS order (0 or 1)
     * @param ransEncode shared encoder instance
     * @param ransDecode shared decoder instance
     */
    public RANS4x8ExternalCompressor(final int order, final RANS4x8Encode ransEncode, final RANS4x8Decode ransDecode) {
        this(RANSParams.ORDER.fromInt(order), ransEncode, ransDecode);
    }

    /**
     * Create a rANS 4x8 compressor with the specified order.
     *
     * @param order the rANS order (ZERO or ONE)
     * @param ransEncode shared encoder instance
     * @param ransDecode shared decoder instance
     */
    public RANS4x8ExternalCompressor(
            final RANSParams.ORDER order, final RANS4x8Encode ransEncode, final RANS4x8Decode ransDecode) {
        super(BlockCompressionMethod.RANS);
        this.ransEncode = ransEncode;
        this.ransDecode = ransDecode;
        this.order = order;
    }

    @Override
    public byte[] compress(final byte[] data, final CRAMCodecModelContext unused_contextModel) {
        return ransEncode.compress(data, new RANS4x8Params(order));
    }

    @Override
    public byte[] uncompress(byte[] data) {
        return ransDecode.uncompress(data);
    }

    @Override
    public String toString() {
        return String.format("%s(%s)", this.getMethod(), order);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RANS4x8ExternalCompressor that = (RANS4x8ExternalCompressor) o;

        return this.order == that.order;
    }

    @Override
    public int hashCode() {
        return Objects.hash(getMethod(), order);
    }
}
