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

package com.nimbusds.jose.jwk.source;

import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.util.cache.CachedObject;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;
import com.nimbusds.jose.proc.SecurityContext;

@ThreadSafe
abstract class AbstractCachingJWKSetSource<C extends SecurityContext> extends JWKSetSourceWrapper<C>
{
    private volatile CachedObject<JWKSet> cachedJWKSet;
    private final long timeToLive;
    
    AbstractCachingJWKSetSource(final JWKSetSource<C> source, final long timeToLive) {
        super(source);
        this.timeToLive = timeToLive;
    }
    
    CachedObject<JWKSet> getCachedJWKSet() {
        return this.cachedJWKSet;
    }
    
    void setCachedJWKSet(final CachedObject<JWKSet> cachedJWKSet) {
        this.cachedJWKSet = cachedJWKSet;
    }
    
    CachedObject<JWKSet> getCachedJWKSetIfValid(final long currentTime) {
        final CachedObject<JWKSet> threadSafeCache = this.getCachedJWKSet();
        if (threadSafeCache != null && threadSafeCache.isValid(currentTime)) {
            return threadSafeCache;
        }
        return null;
    }
    
    public long getTimeToLive() {
        return this.timeToLive;
    }
    
    CachedObject<JWKSet> cacheJWKSet(final JWKSet jwkSet, final long fetchTime) {
        final long currentTime = this.currentTimeMillis();
        final CachedObject<JWKSet> cachedJWKSet = new CachedObject<JWKSet>(jwkSet, currentTime, CachedObject.computeExpirationTime(fetchTime, this.getTimeToLive()));
        this.setCachedJWKSet(cachedJWKSet);
        return cachedJWKSet;
    }
    
    long currentTimeMillis() {
        return System.currentTimeMillis();
    }
}
