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

package io.netty.handler.codec.quic;

import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import org.jetbrains.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.security.SecureRandom;

final class ConnectionIdChannelMap
{
    private static final SecureRandom random;
    private final Map<ConnectionIdKey, QuicheQuicChannel> channelMap;
    private final SipHash sipHash;
    
    ConnectionIdChannelMap() {
        this.channelMap = new HashMap<ConnectionIdKey, QuicheQuicChannel>();
        final byte[] seed = new byte[16];
        ConnectionIdChannelMap.random.nextBytes(seed);
        this.sipHash = new SipHash(1, 3, seed);
    }
    
    private ConnectionIdKey key(final ByteBuffer cid) {
        final long hash = this.sipHash.macHash(cid);
        return new ConnectionIdKey(hash, cid);
    }
    
    @Nullable
    QuicheQuicChannel put(final ByteBuffer cid, final QuicheQuicChannel channel) {
        return this.channelMap.put(this.key(cid), channel);
    }
    
    @Nullable
    QuicheQuicChannel remove(final ByteBuffer cid) {
        return this.channelMap.remove(this.key(cid));
    }
    
    @Nullable
    QuicheQuicChannel get(final ByteBuffer cid) {
        return this.channelMap.get(this.key(cid));
    }
    
    void clear() {
        this.channelMap.clear();
    }
    
    static {
        random = new SecureRandom();
    }
    
    private static final class ConnectionIdKey implements Comparable<ConnectionIdKey>
    {
        private final long hash;
        private final ByteBuffer key;
        
        ConnectionIdKey(final long hash, final ByteBuffer key) {
            this.hash = hash;
            this.key = key;
        }
        
        @Override
        public boolean equals(final Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || this.getClass() != o.getClass()) {
                return false;
            }
            final ConnectionIdKey that = (ConnectionIdKey)o;
            return this.hash == that.hash && Objects.equals(this.key, that.key);
        }
        
        @Override
        public int hashCode() {
            return (int)this.hash;
        }
        
        @Override
        public int compareTo(@NotNull final ConnectionIdKey o) {
            final int result = Long.compare(this.hash, o.hash);
            return (result != 0) ? result : this.key.compareTo(o.key);
        }
    }
}
