package io.grpc.netty;

import io.grpc.internal.ReadableBuffer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocatorMetric;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * Helper class used to expose package-private classes for metrics and testing
 */
public class GrpcNettyBackdoor {

    private GrpcNettyBackdoor() {
        throw new UnsupportedOperationException("Utility classes should not be instantiated");
    }

    /**
     * @return metrics about the {@link PooledByteBufAllocator} used by {@code grpc-netty}
     */
    @Nonnull
    public static PooledByteBufAllocatorMetric getAllocatorMetric() {
        return ((PooledByteBufAllocator) Utils.getByteBufAllocator(false)).metric();
    }

    @Nonnull
    public static ReadableBuffer newReadableBuffer(@Nonnull ByteBuf buf) {
        return new NettyReadableBuffer(requireNonNull(buf, "buf"));
    }
}
