001/* 002 * Copyright 2018 Paul Schaub. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.pgpainless.key.selection.keyring; 017 018import javax.annotation.Nonnull; 019import java.util.HashSet; 020import java.util.Iterator; 021import java.util.Set; 022 023import org.bouncycastle.openpgp.PGPPublicKeyRing; 024import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; 025import org.pgpainless.util.MultiMap; 026 027public abstract class PublicKeyRingSelectionStrategy<O> implements KeyRingSelectionStrategy<PGPPublicKeyRing, PGPPublicKeyRingCollection, O> { 028 029 @Override 030 public Set<PGPPublicKeyRing> selectKeyRingsFromCollection(@Nonnull O identifier, @Nonnull PGPPublicKeyRingCollection keyRingCollection) { 031 Set<PGPPublicKeyRing> accepted = new HashSet<>(); 032 for (Iterator<PGPPublicKeyRing> i = keyRingCollection.getKeyRings(); i.hasNext(); ) { 033 PGPPublicKeyRing ring = i.next(); 034 if (accept(identifier, ring)) accepted.add(ring); 035 } 036 return accepted; 037 } 038 039 @Override 040 public MultiMap<O, PGPPublicKeyRing> selectKeyRingsFromCollections(@Nonnull MultiMap<O, PGPPublicKeyRingCollection> keyRingCollections) { 041 MultiMap<O, PGPPublicKeyRing> keyRings = new MultiMap<>(); 042 for (O identifier : keyRingCollections.keySet()) { 043 for (PGPPublicKeyRingCollection collection : keyRingCollections.get(identifier)) { 044 keyRings.put(identifier, selectKeyRingsFromCollection(identifier, collection)); 045 } 046 } 047 return keyRings; 048 } 049}