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.impl;
017
018import java.util.Iterator;
019
020import org.bouncycastle.openpgp.PGPPublicKeyRing;
021import org.bouncycastle.openpgp.PGPSecretKeyRing;
022import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy;
023import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy;
024
025public class ExactUserId {
026
027    public static class PubRingSelectionStrategy extends PublicKeyRingSelectionStrategy<String> {
028
029        @Override
030        public boolean accept(String identifier, PGPPublicKeyRing keyRing) {
031            Iterator<String> userIds = keyRing.getPublicKey().getUserIDs();
032            while (userIds.hasNext()) {
033                if (userIds.next().equals(identifier)) return true;
034            }
035            return false;
036        }
037    }
038
039    public static class SecRingSelectionStrategy extends SecretKeyRingSelectionStrategy<String> {
040
041        @Override
042        public boolean accept(String identifier, PGPSecretKeyRing keyRing) {
043            Iterator<String> userIds = keyRing.getPublicKey().getUserIDs();
044            while (userIds.hasNext()) {
045                if (userIds.next().equals(identifier)) return true;
046            }
047            return false;
048        }
049    }
050}