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

pl.allegro.tech.hermes.consumers.consumer.rate.maxrate.ConsumerMaxRatesEncoder Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
package pl.allegro.tech.hermes.consumers.consumer.rate.maxrate;

import org.agrona.ExpandableDirectByteBuffer;
import org.agrona.MutableDirectBuffer;
import pl.allegro.tech.hermes.consumers.consumer.rate.sbe.stubs.MaxRateEncoder;
import pl.allegro.tech.hermes.consumers.consumer.rate.sbe.stubs.MessageHeaderEncoder;
import pl.allegro.tech.hermes.consumers.subscription.id.SubscriptionId;
import pl.allegro.tech.hermes.consumers.subscription.id.SubscriptionIds;

import java.util.Map;

class ConsumerMaxRatesEncoder {

    private final SubscriptionIds subscriptionIds;
    private final MutableDirectBuffer buffer;

    ConsumerMaxRatesEncoder(SubscriptionIds subscriptionIds, int bufferSize) {
        this.subscriptionIds = subscriptionIds;
        this.buffer = new ExpandableDirectByteBuffer(bufferSize);
    }

    byte[] encode(ConsumerMaxRates consumerMaxRates) {
        MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder();
        MaxRateEncoder body = new MaxRateEncoder();

        Map filteredRates = consumerMaxRates.toSubscriptionsIdsMap(subscriptionIds::getSubscriptionId);

        MaxRateEncoder.SubscriptionsEncoder subscriptionsEncoder = body.wrapAndApplyHeader(buffer, 0, headerEncoder)
                .subscriptionsCount(filteredRates.size());

        filteredRates.forEach((id, maxRate) -> {
            subscriptionsEncoder.next()
                    .id(id.getValue())
                    .maxRate(maxRate.getMaxRate());
        });

        int len = headerEncoder.encodedLength() + body.encodedLength();

        byte[] dst = new byte[len];
        buffer.getBytes(0, dst);
        return dst;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy