All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.permazen.encoding.DoubleEncoding Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen.encoding;

import com.google.common.base.Preconditions;

import io.permazen.util.ByteReader;
import io.permazen.util.ByteUtil;
import io.permazen.util.ByteWriter;

import java.util.OptionalInt;

import org.dellroad.stuff.java.Primitive;

/**
 * {@link Double} type.
 */
public class DoubleEncoding extends NumberEncoding {

    private static final long serialVersionUID = 7124114664265270273L;

    private static final long POS_XOR = 0x8000000000000000L;
    private static final long NEG_XOR = 0xffffffffffffffffL;
    private static final long SIGN_BIT = 0x8000000000000000L;

    public DoubleEncoding(EncodingId encodingId) {
       super(encodingId, Primitive.DOUBLE);
    }

    @Override
    public Double read(ByteReader reader) {
        Preconditions.checkArgument(reader != null);
        long bits = ByteUtil.readLong(reader);
        bits ^= (bits & SIGN_BIT) == 0 ? NEG_XOR : POS_XOR;
        return Double.longBitsToDouble(bits);
    }

    @Override
    public void write(ByteWriter writer, Double value) {
        Preconditions.checkArgument(writer != null);
        long bits = Double.doubleToLongBits(value);
        bits ^= (bits & SIGN_BIT) != 0 ? NEG_XOR : POS_XOR;
        ByteUtil.writeLong(writer, bits);
    }

    @Override
    public void skip(ByteReader reader) {
        Preconditions.checkArgument(reader != null);
        reader.skip(8);
    }

    @Override
    public Double validate(Object obj) {
        if (obj instanceof Character)
            return (double)(Character)obj;
        if (obj instanceof Byte || obj instanceof Short
          || obj instanceof Integer || obj instanceof Float || obj instanceof Long)
            return ((Number)obj).doubleValue();
        return super.validate(obj);
    }

    @Override
    public boolean hasPrefix0x00() {
        return true;
    }

    @Override
    public boolean hasPrefix0xff() {
        return true;
    }

    @Override
    public OptionalInt getFixedWidth() {
        return OptionalInt.of(8);
    }

// Conversion

    @Override
    protected Double convertNumber(Number value) {
        return value.doubleValue();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy