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

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import edu.brown.cs.systems.tracingplane.atom_layer.types.TypeUtils;
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.protocol.BaggageWriter;
import edu.brown.cs.systems.tracingplane.transit_layer.Baggage;

public class BaggageBuffersUtils {
    
    private BaggageBuffersUtils(){}

    /**
     * Ideally, this is an instance method, but for now we're doing it here
     */
    public static int serializedSize(Baggage instance) {
        if (instance == null) {
            return 0;
        }
        byte[] serialized = Baggage.serialize(instance);
        return serialized == null ? 0 : serialized.length;
    }

    /**
     * Ideally, this is an instance method, but for now we're doing it here<commit_msg>Make sure calculating serialized size doesn't invoke transit layer listeners<commit_after>package edu.brown.cs.systems.tracingplane.baggage_buffers;

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import edu.brown.cs.systems.tracingplane.atom_layer.types.TypeUtils;
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.protocol.BaggageWriter;
import edu.brown.cs.systems.tracingplane.transit_layer.Baggage;
import edu.brown.cs.systems.tracingplane.transit_layer.TransitLayer;

public class BaggageBuffersUtils {
    
    private BaggageBuffersUtils(){}

    /**
     * Ideally, this is an instance method, but for now we're doing it here
     */
    public static int serializedSize(Baggage instance) {
        if (instance == null) {
            return 0;
        }
        return calculateSerializedSize(Baggage.transit, instance);
    }
    
    private static <B extends Baggage> int calculateSerializedSize(TransitLayer<B> transit, Baggage baggage) {
        if (transit.isInstance(baggage)) {
            byte[] serialized = transit.serialize((B) baggage);
            return serialized == null ? 0 : serialized.length;
        } else {
            return 0;
        }
    }

    /**
     * Ideally, this is an instance method, but for now we're doing it here
{'fraction_non_alphanumeric': 0.09734513274336283, 'fraction_numerical': 0.002011263073209976, 'mean_word_length': 3.4891696750902526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6966731', 'n_tokens_mistral': 805, 'n_tokens_neox': 736, 'n_words': 167}