<commit_before>package de.newsarea.homecockpit.connector.fsuipc.facade.eventhandler;

import de.newsarea.homecockpit.connector.facade.eventhandler.InboundEventHandler;
import de.newsarea.homecockpit.connector.fsuipc.FSUIPCConnector;
import de.newsarea.homecockpit.connector.fsuipc.facade.eventhandler.domain.FSUIPCOffset;
import de.newsarea.homecockpit.fsuipc.domain.OffsetIdent;
import de.newsarea.homecockpit.fsuipc.domain.OffsetItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class InboundToggleBitEventHandler extends AbstractFSUIPCEventHandler implements InboundEventHandler {
	
	private static final Logger log = LoggerFactory.getLogger(InboundToggleBitEventHandler.class);

    private Byte bitIdx;

    public Byte getBitIdx() {
        if(bitIdx == null) {
            bitIdx = Byte.parseByte(getParameterValue("bitIdx"));
        }
        return bitIdx;
    }

    public InboundToggleBitEventHandler(FSUIPCConnector connector, Map<String, String> parameters) {
        super(connector, parameters);
    }

    public InboundToggleBitEventHandler(FSUIPCConnector connector, FSUIPCOffset offset, int size, Byte bitIdx) {
        this(connector,
                toParameters(
                        new AbstractMap.SimpleEntry<>("offset", offset.toHexString()),
                        new AbstractMap.SimpleEntry<>("size", String.valueOf(size)),
                        new AbstractMap.SimpleEntry<>("bitIdx", String.valueOf(bitIdx))
                )
        );
    }

    @Override
    public void handleInboundEvent(Object value) throws IOException {
		log.debug("handleOutput - {}", value);
        try {
            toggleBit(getOffset().getValue(), getSize(), getBitIdx());
        } catch (TimeoutException e) {
            log.error(e.getMessage(), e);
        }
    }
	
	/* HELPER */

    private void toggleBit(int offset, int size, byte bitIdx) throws IOException, TimeoutException {
    	OffsetItem offsetItem = getConnector().read(new OffsetIdent(offset, size));
        byte[] value = offsetItem.getValue().toByteArray();
    	int bidx = (bitIdx / 8);
		byte nidx = (byte)(bitIdx % 8);
		byte newByteValue = value[value.length - 1 - bidx];
		newByteValue ^= (1 << nidx);
		value[value.length - 1 - bidx] = newByteValue;
        getConnector().write(new OffsetItem(offset, size, value));
    }

}<commit_msg>add direct toggleBit function to InboundToggleBitEventHandler<commit_after>package de.newsarea.homecockpit.connector.fsuipc.facade.eventhandler;

import de.newsarea.homecockpit.connector.facade.eventhandler.InboundEventHandler;
import de.newsarea.homecockpit.connector.fsuipc.FSUIPCConnector;
import de.newsarea.homecockpit.connector.fsuipc.facade.eventhandler.domain.FSUIPCOffset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.Map;

public class InboundToggleBitEventHandler extends AbstractFSUIPCEventHandler implements InboundEventHandler {
	
	private static final Logger log = LoggerFactory.getLogger(InboundToggleBitEventHandler.class);

    private Byte bitIdx;

    public Byte getBitIdx() {
        if(bitIdx == null) {
            bitIdx = Byte.parseByte(getParameterValue("bitIdx"));
        }
        return bitIdx;
    }

    public InboundToggleBitEventHandler(FSUIPCConnector connector, Map<String, String> parameters) {
        super(connector, parameters);
    }

    public InboundToggleBitEventHandler(FSUIPCConnector connector, FSUIPCOffset offset, int size, Byte bitIdx) {
        this(connector,
                toParameters(
                        new AbstractMap.SimpleEntry<>("offset", offset.toHexString()),
                        new AbstractMap.SimpleEntry<>("size", String.valueOf(size)),
                        new AbstractMap.SimpleEntry<>("bitIdx", String.valueOf(bitIdx))
                )
        );
    }

    @Override
    public void handleInboundEvent(Object value) throws IOException {
		log.debug("handleOutput - {}", value);
        getConnector().toggleBit(getOffset().getValue(), getSize(), getBitIdx());
    }

}
{'fraction_non_alphanumeric': 0.10338301395788975, 'fraction_numerical': 0.0021291696238466998, 'mean_word_length': 3.464625131995776, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2660293', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1197, 'n_words': 227}