<commit_before>package edu.brown.cs.systems.tracingplane.baggage_buffers;

import edu.brown.cs.systems.tracingplane.baggage_buffers.api.Bag;
import edu.brown.cs.systems.tracingplane.baggage_layer.BagKey;
import edu.brown.cs.systems.tracingplane.baggage_layer.BaggageLayer;
import edu.brown.cs.systems.tracingplane.baggage_layer.protocol.BaggageReader;
import edu.brown.cs.systems.tracingplane.baggage_layer.protocol.BaggageWriter;
import edu.brown.cs.systems.tracingplane.transit_layer.Baggage;

/**
 * <p>
 * The final layer in the baggage stack. BaggageBuffers is an implementation of the BaggageLayer. It enables you to
 * register baggage-buffers compiled classes then access them through the generated accessor methods.
 * </p>
 */
public class BaggageBuffers implements BaggageLayer<BaggageBuffersContents> {

    @Override
    public boolean isInstance(Baggage baggage) {
        return baggage == null || baggage instanceof BaggageBuffersContents;
    }

    @Override
    public BaggageBuffersContents newInstance() {
        return null;
    }

    @Override
    public void discard(BaggageBuffersContents baggage) {}

    @Override
    public BaggageBuffersContents branch(BaggageBuffersContents from) {
        return from == null ? null : from.branch();
    }

    @Override
    public BaggageBuffersContents join(BaggageBuffersContents left, BaggageBuffersContents right) {
        if (left == null) {
            return right;
        } else if (right == null) {
            return left;
        } else {
            return left.mergeWith(right);
        }
    }

    @Override
    public BaggageBuffersContents read(BaggageReader reader) {
        return BaggageBuffersContents.parseFrom(reader);
    }
<commit_msg>Add ability to register transit handlers<commit_after>package edu.brown.cs.systems.tracingplane.baggage_buffers;

import java.io.Closeable;
import edu.brown.cs.systems.tracingplane.baggage_buffers.api.Bag;
import edu.brown.cs.systems.tracingplane.baggage_layer.BagKey;
import edu.brown.cs.systems.tracingplane.baggage_layer.BaggageLayer;
import edu.brown.cs.systems.tracingplane.baggage_layer.protocol.BaggageReader;
import edu.brown.cs.systems.tracingplane.baggage_layer.protocol.BaggageWriter;
import edu.brown.cs.systems.tracingplane.transit_layer.Baggage;
import edu.brown.cs.systems.tracingplane.transit_layer.TransitLayerCallbacks.TransitHandler;
import edu.brown.cs.systems.tracingplane.transit_layer.TransitLayerCallbacks.TransitLayerCallbackRegistry;

/**
 * <p>
 * The final layer in the baggage stack. BaggageBuffers is an implementation of the BaggageLayer. It enables you to
 * register baggage-buffers compiled classes then access them through the generated accessor methods.
 * </p>
 */
public class BaggageBuffers implements BaggageLayer<BaggageBuffersContents> {

    private static final TransitLayerCallbackRegistry<BaggageBuffersContents> callbacks =
            new TransitLayerCallbackRegistry<>(BaggageBuffers::branchImpl, BaggageBuffers::joinImpl);

    /**
     * Register a callback handler that will be invoked when branching and joining
     */
    public Closeable registerCallbackHandler(TransitHandler transitHandler) {
        return callbacks.add(transitHandler);
    }

    @Override
    public boolean isInstance(Baggage baggage) {
        return baggage == null || baggage instanceof BaggageBuffersContents;
    }

    @Override
    public BaggageBuffersContents newInstance() {
        return null;
    }

    @Override
    public void discard(BaggageBuffersContents baggage) {}

    @Override
    public BaggageBuffersContents branch(BaggageBuffersContents from) {
        return callbacks.branch(from);
    }

    private static BaggageBuffersContents branchImpl(BaggageBuffersContents from) {
        return from == null ? null : from.branch();
    }

    @Override
    public BaggageBuffersContents join(BaggageBuffersContents left, BaggageBuffersContents right) {
        return callbacks.join(left, right);
    }

    private static BaggageBuffersContents joinImpl(BaggageBuffersContents left, BaggageBuffersContents right) {
        if (left == null) {
            return right;
        } else if (right == null) {
            return left;
        } else {
            return left.mergeWith(right);
        }
    }

    @Override
    public BaggageBuffersContents read(BaggageReader reader) {
        return BaggageBuffersContents.parseFrom(reader);
    }

{'fraction_non_alphanumeric': 0.07944030692845859, 'fraction_numerical': 0.0, 'mean_word_length': 3.806941431670282, 'pattern_counts': {'":': 0, '<': 11, '<?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': '7064833', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1223, 'n_words': 276}