package org.bouncycastle.openpgp.api;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;

/* JADX INFO: loaded from: classes2.dex */
public abstract class OpenPGPKeyMaterialPool<M extends OpenPGPCertificate> implements OpenPGPKeyMaterialProvider<M> {
    private final Map<KeyIdentifier, M> pool = new HashMap();
    private OpenPGPKeyMaterialProvider<M> callback = null;
    private boolean cacheResultsFromCallback = true;

    /* JADX INFO: loaded from: classes.dex */
    public static class OpenPGPCertificatePool extends OpenPGPKeyMaterialPool<OpenPGPCertificate> implements OpenPGPKeyMaterialProvider.OpenPGPCertificateProvider {
        public OpenPGPCertificatePool() {
        }

        public OpenPGPCertificatePool(Collection<OpenPGPCertificate> collection) {
            super(collection);
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public OpenPGPCertificatePool addItem(OpenPGPCertificate openPGPCertificate) {
            super.addItem(openPGPCertificate);
            return this;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public OpenPGPCertificatePool setCacheResultsFromCallback(boolean z) {
            super.setCacheResultsFromCallback(z);
            return this;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public OpenPGPCertificatePool setMissingItemCallback(OpenPGPKeyMaterialProvider<OpenPGPCertificate> openPGPKeyMaterialProvider) {
            super.setMissingItemCallback((OpenPGPKeyMaterialProvider) openPGPKeyMaterialProvider);
            return this;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public /* bridge */ /* synthetic */ OpenPGPKeyMaterialPool setMissingItemCallback(OpenPGPKeyMaterialProvider openPGPKeyMaterialProvider) {
            return setMissingItemCallback((OpenPGPKeyMaterialProvider<OpenPGPCertificate>) openPGPKeyMaterialProvider);
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static class OpenPGPKeyPool extends OpenPGPKeyMaterialPool<OpenPGPKey> implements OpenPGPKeyMaterialProvider.OpenPGPKeyProvider {
        public OpenPGPKeyPool() {
        }

        public OpenPGPKeyPool(Collection<OpenPGPKey> collection) {
            super(collection);
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public OpenPGPKeyPool addItem(OpenPGPKey openPGPKey) {
            super.addItem(openPGPKey);
            return this;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public OpenPGPKeyPool setCacheResultsFromCallback(boolean z) {
            super.setCacheResultsFromCallback(z);
            return this;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public OpenPGPKeyPool setMissingItemCallback(OpenPGPKeyMaterialProvider<OpenPGPKey> openPGPKeyMaterialProvider) {
            super.setMissingItemCallback((OpenPGPKeyMaterialProvider) openPGPKeyMaterialProvider);
            return this;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialPool
        public /* bridge */ /* synthetic */ OpenPGPKeyMaterialPool setMissingItemCallback(OpenPGPKeyMaterialProvider openPGPKeyMaterialProvider) {
            return setMissingItemCallback((OpenPGPKeyMaterialProvider<OpenPGPKey>) openPGPKeyMaterialProvider);
        }
    }

    public OpenPGPKeyMaterialPool() {
    }

    public OpenPGPKeyMaterialPool(Collection<M> collection) {
        Iterator<M> it = collection.iterator();
        while (it.hasNext()) {
            addItem(it.next());
        }
    }

    public OpenPGPKeyMaterialPool(M m) {
        addItem(m);
    }

    public OpenPGPKeyMaterialPool<M> addItem(M m) {
        if (m != null) {
            Iterator<KeyIdentifier> it = m.getAllKeyIdentifiers().iterator();
            while (it.hasNext()) {
                this.pool.put(it.next(), m);
            }
        }
        return this;
    }

    public Collection<M> getAllItems() {
        return (Collection) this.pool.values().stream().distinct().collect(Collectors.toList());
    }

    @Override // org.bouncycastle.openpgp.api.OpenPGPKeyMaterialProvider
    public M provide(KeyIdentifier keyIdentifier) {
        M m = this.pool.get(keyIdentifier);
        if (m == null && this.callback != null) {
            m = (M) this.callback.provide(keyIdentifier);
            if (this.cacheResultsFromCallback) {
                addItem(m);
            }
        }
        return m;
    }

    public OpenPGPKeyMaterialPool<M> setCacheResultsFromCallback(boolean z) {
        this.cacheResultsFromCallback = z;
        return this;
    }

    public OpenPGPKeyMaterialPool<M> setMissingItemCallback(OpenPGPKeyMaterialProvider<M> openPGPKeyMaterialProvider) {
        if (openPGPKeyMaterialProvider == null) {
            throw new NullPointerException();
        }
        this.callback = openPGPKeyMaterialProvider;
        return this;
    }
}
