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

package com.nimbusds.jose.jwk.source;

import java.io.IOException;
import com.nimbusds.jose.KeySourceException;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.JWK;
import java.util.List;
import com.nimbusds.jose.jwk.JWKSelector;
import java.util.Objects;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;
import java.io.Closeable;
import com.nimbusds.jose.proc.SecurityContext;

@ThreadSafe
public class JWKSetBasedJWKSource<C extends SecurityContext> implements JWKSource<C>, Closeable
{
    private final JWKSetSource<C> source;
    
    public JWKSetBasedJWKSource(final JWKSetSource<C> source) {
        Objects.requireNonNull(source);
        this.source = source;
    }
    
    @Override
    public List<JWK> get(final JWKSelector jwkSelector, final C context) throws KeySourceException {
        final long currentTime = System.currentTimeMillis();
        final JWKSet jwkSet = this.source.getJWKSet(JWKSetCacheRefreshEvaluator.noRefresh(), currentTime, context);
        List<JWK> select = jwkSelector.select(jwkSet);
        if (select.isEmpty()) {
            final JWKSet recentJwkSet = this.source.getJWKSet(JWKSetCacheRefreshEvaluator.referenceComparison(jwkSet), currentTime, context);
            select = jwkSelector.select(recentJwkSet);
        }
        return select;
    }
    
    public JWKSetSource<C> getJWKSetSource() {
        return this.source;
    }
    
    @Override
    public void close() throws IOException {
        this.source.close();
    }
}
