// 
// Decompiled by Procyon v0.6.0
// 

package io.netty.handler.codec.quic;

import io.netty.util.internal.EmptyArrays;
import java.util.Objects;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import java.nio.ByteBuffer;
import java.net.SocketAddress;

public final class QuicConnectionAddress extends SocketAddress
{
    static final QuicConnectionAddress NULL_LEN;
    public static final QuicConnectionAddress EPHEMERAL;
    private final String toStr;
    private final ByteBuffer connId;
    
    public QuicConnectionAddress(final byte[] connId) {
        this(ByteBuffer.wrap(connId.clone()), true);
    }
    
    public QuicConnectionAddress(final ByteBuffer connId) {
        this(connId.duplicate(), true);
    }
    
    private QuicConnectionAddress(final ByteBuffer connId, final boolean validate) {
        Quic.ensureAvailability();
        if (validate && connId.remaining() > Quiche.QUICHE_MAX_CONN_ID_LEN) {
            throw new IllegalArgumentException("Connection ID can only be of max length " + Quiche.QUICHE_MAX_CONN_ID_LEN);
        }
        if (connId == null) {
            this.connId = null;
            this.toStr = "QuicConnectionAddress{EPHEMERAL}";
        }
        else {
            this.connId = connId.asReadOnlyBuffer().duplicate();
            final ByteBuf buffer = Unpooled.wrappedBuffer(connId);
            try {
                this.toStr = "QuicConnectionAddress{connId=" + ByteBufUtil.hexDump(buffer) + '}';
            }
            finally {
                buffer.release();
            }
        }
    }
    
    @Override
    public String toString() {
        return this.toStr;
    }
    
    @Override
    public int hashCode() {
        if (this == QuicConnectionAddress.EPHEMERAL) {
            return System.identityHashCode(QuicConnectionAddress.EPHEMERAL);
        }
        return Objects.hash(this.connId);
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (!(obj instanceof QuicConnectionAddress)) {
            return false;
        }
        final QuicConnectionAddress address = (QuicConnectionAddress)obj;
        return obj == this || this.connId.equals(address.connId);
    }
    
    ByteBuffer id() {
        if (this.connId == null) {
            return ByteBuffer.allocate(0);
        }
        return this.connId.duplicate();
    }
    
    public static QuicConnectionAddress random(final int length) {
        return new QuicConnectionAddress(QuicConnectionIdGenerator.randomGenerator().newId(length));
    }
    
    public static QuicConnectionAddress random() {
        return random(Quiche.QUICHE_MAX_CONN_ID_LEN);
    }
    
    static {
        NULL_LEN = new QuicConnectionAddress(EmptyArrays.EMPTY_BYTES);
        EPHEMERAL = new QuicConnectionAddress(null, false);
    }
}
