001package com.nimbusds.jose.jwk; 002 003 004import java.util.*; 005 006import net.jcip.annotations.Immutable; 007 008 009/** 010 * Selects (filters) one or more JSON Web Keys (JWKs) from a JWK set. 011 * 012 * @author Vladimir Dzhuvinov 013 * @version 2015-04-15 014 */ 015@Immutable 016public final class JWKSelector { 017 018 019 /** 020 * The JWK matcher. 021 */ 022 private final JWKMatcher matcher; 023 024 025 026 public JWKSelector(final JWKMatcher matcher) { 027 028 if (matcher == null) { 029 throw new IllegalArgumentException("The JWK matcher must not be null"); 030 } 031 032 this.matcher = matcher; 033 } 034 035 036 /** 037 * Returns the JWK matcher. 038 * 039 * @return The JWK matcher. 040 */ 041 public JWKMatcher getMatcher() { 042 043 return matcher; 044 } 045 046 047 /** 048 * Selects the keys from the specified JWK set according to the 049 * matcher's criteria. 050 * 051 * @param jwkSet The JWK set. May be {@code null}. 052 * 053 * @return The selected keys, ordered by their position in the JWK set, 054 * empty list if none were matched or the JWK is {@code null}. 055 */ 056 public List<JWK> select(final JWKSet jwkSet) { 057 058 List<JWK> selectedKeys = new LinkedList<>(); 059 060 if (jwkSet == null) 061 return selectedKeys; 062 063 for (JWK key: jwkSet.getKeys()) { 064 065 if (matcher.matches(key)) { 066 selectedKeys.add(key); 067 } 068 } 069 070 return selectedKeys; 071 } 072}