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

package org.bouncycastle.cms;

import java.math.BigInteger;
import org.bouncycastle.asn1.x500.X500Name;
import java.util.Iterator;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import org.bouncycastle.util.Iterable;

public class RecipientInformationStore implements Iterable<RecipientInformation>
{
    private final List all;
    private final Map table;
    
    public RecipientInformationStore(final RecipientInformation recipientInformation) {
        this.table = new HashMap();
        (this.all = new ArrayList(1)).add(recipientInformation);
        this.table.put(recipientInformation.getRID(), this.all);
    }
    
    public RecipientInformationStore(final Collection<RecipientInformation> c) {
        this.table = new HashMap();
        for (final RecipientInformation recipientInformation : c) {
            final RecipientId rid = recipientInformation.getRID();
            ArrayList list = this.table.get(rid);
            if (list == null) {
                list = new ArrayList(1);
                this.table.put(rid, list);
            }
            list.add(recipientInformation);
        }
        this.all = new ArrayList(c);
    }
    
    public RecipientInformation get(final RecipientId recipientId) {
        final Collection<RecipientInformation> recipients = this.getRecipients(recipientId);
        return (recipients.size() == 0) ? null : recipients.iterator().next();
    }
    
    public int size() {
        return this.all.size();
    }
    
    public Collection<RecipientInformation> getRecipients() {
        return new ArrayList<RecipientInformation>(this.all);
    }
    
    public Collection<RecipientInformation> getRecipients(final RecipientId recipientId) {
        if (recipientId instanceof PKIXRecipientId) {
            final PKIXRecipientId pkixRecipientId = (PKIXRecipientId)recipientId;
            final X500Name issuer = pkixRecipientId.getIssuer();
            final byte[] subjectKeyIdentifier = pkixRecipientId.getSubjectKeyIdentifier();
            if (issuer != null && subjectKeyIdentifier != null) {
                final ArrayList list = new ArrayList();
                final ArrayList list2 = this.table.get(new PKIXRecipientId(pkixRecipientId.getType(), issuer, pkixRecipientId.getSerialNumber(), null));
                if (list2 != null) {
                    list.addAll(list2);
                }
                final ArrayList list3 = this.table.get(new PKIXRecipientId(pkixRecipientId.getType(), null, null, subjectKeyIdentifier));
                if (list3 != null) {
                    list.addAll(list3);
                }
                return list;
            }
        }
        final ArrayList c = this.table.get(recipientId);
        return (c == null) ? new ArrayList<RecipientInformation>() : new ArrayList<RecipientInformation>(c);
    }
    
    @Override
    public Iterator<RecipientInformation> iterator() {
        return this.getRecipients().iterator();
    }
}
