
package com.gyee.edge.protocol.iec60870.infoelement;

import java.io.DataInputStream;
import java.io.IOException;
import java.text.MessageFormat;

abstract class IeAbstractQuality extends InformationElement {

    protected int value;

    public IeAbstractQuality(boolean blocked, boolean substituted, boolean notTopical, boolean invalid) {

        value = 0;

        if (blocked) {
            value |= 0x10;
        }
        if (substituted) {
            value |= 0x20;
        }
        if (notTopical) {
            value |= 0x40;
        }
        if (invalid) {
            value |= 0x80;
        }

    }

    IeAbstractQuality(DataInputStream is) throws IOException {
        value = is.readUnsignedByte();
    }

    @Override
    int encode(byte[] buffer, int i) {
        buffer[i] = (byte) value;
        return 1;
    }

    public boolean isBlocked() {
        return hasBitSet(0x10);
    }

    public boolean isSubstituted() {
        return hasBitSet(0x20);
    }

    public boolean isNotTopical() {
        return hasBitSet(0x40);
    }

    public boolean isInvalid() {
        return hasBitSet(0x80);
    }

    private boolean hasBitSet(int mask) {
        return (value & mask) == mask;
    }

    @Override
    public String toString() {
        return MessageFormat.format("blocked: {0}, substituted: {1}, not topical: {2}, invalid: {3}", isBlocked(),
                isSubstituted(), isNotTopical(), isInvalid());
    }
}
