@Override public void putAll(final Map<? extends K, ? extends V> mapToCopy) { for (final Map.Entry<? extends K, ? extends V> entry : mapToCopy.entrySet()) { put(entry.getKey(), entry.getValue()); } }
/** * Removes the entry with the given key if the entry's expiration time is * less than <code>now</code>. If the entry has a negative expiration time, * the entry is never removed. */ private void removeIfExpired(final Object key, final long now) { final Long expirationTimeObject = expirationMap.get(key); if (isExpired(now, expirationTimeObject)) { remove(key); } }
/** * All expired entries are removed from the map prior to determining the * contains result. * {@inheritDoc} */ @Override public boolean containsValue(final Object value) { removeAllExpired(now()); return super.containsValue(value); }
/** * All expired entries are removed from the map prior to returning the entry value. * {@inheritDoc} */ @Override public V get(final Object key) { removeIfExpired(key, now()); return super.get(key); }
public PacketDefragmentator(int order, int total, int ttl) throws Exception { super(order, total); this.ttl = ttl; this.accumulator = new PassiveExpiringMap<>(ttl); }
@Override public CompletableFuture<Void> removeToken(AccessToken accessToken) { Preconditions.checkNotNull(accessToken); synchronized (tokensExpirationDates) { tokensExpirationDates.remove(accessToken); } return CompletableFuture.completedFuture(null); }
/** * Construct a map decorator using the given time-to-live value measured in * the given time units of measure to create and use a * {@link ConstantTimeToLiveExpirationPolicy} expiration policy. * * @param timeToLive the constant amount of time an entry is available * before it expires. A negative value results in entries that NEVER * expire. A zero value results in entries that ALWAYS expire. * @param timeUnit the unit of time for the <code>timeToLive</code> * parameter, must not be null. * @throws NullPointerException if the time unit is null. */ public PassiveExpiringMap(final long timeToLive, final TimeUnit timeUnit) { this(validateAndConvertToMillis(timeToLive, timeUnit)); }
/** * Removes all entries in the map whose expiration time is less than * <code>now</code>. The exceptions are entries with negative expiration * times; those entries are never removed. * * @see #isExpired(long, Long) */ private void removeAllExpired(final long now) { final Iterator<Map.Entry<Object, Long>> iter = expirationMap.entrySet().iterator(); while (iter.hasNext()) { final Map.Entry<Object, Long> expirationEntry = iter.next(); if (isExpired(now, expirationEntry.getValue())) { // remove entry from collection super.remove(expirationEntry.getKey()); // remove entry from expiration map iter.remove(); } } }
@Override public CompletableFuture<String> getUsernameFromToken(AccessToken accessToken) throws InvalidAccessToken { Preconditions.checkNotNull(accessToken); synchronized (tokensExpirationDates) { return CompletableFuture.completedFuture( Optional.ofNullable(tokensExpirationDates.get(accessToken)) .<CompletionException>orElseThrow(() -> new CompletionException(new InvalidAccessToken(accessToken)))); } }
@Inject public MemoryAccessTokenRepository(@Named(TOKEN_EXPIRATION_IN_MS) long durationInMilliseconds) { tokensExpirationDates = new PassiveExpiringMap<>(durationInMilliseconds); }
/** * All expired entries are removed from the map prior to determining the * contains result. * {@inheritDoc} */ @Override public boolean containsKey(final Object key) { removeIfExpired(key, now()); return super.containsKey(key); }
/** * Construct a map decorator that decorates the given map using the given * time-to-live value measured in the given time units of measure to create * {@link ConstantTimeToLiveExpirationPolicy} expiration policy. This policy * is used to determine expiration times. If there are any elements already * in the map being decorated, they will NEVER expire unless they are * replaced. * * @param timeToLive the constant amount of time an entry is available * before it expires. A negative value results in entries that NEVER * expire. A zero value results in entries that ALWAYS expire. * @param timeUnit the unit of time for the <code>timeToLive</code> * parameter, must not be null. * @param map the map to decorate, must not be null. * @throws NullPointerException if the map or time unit is null. */ public PassiveExpiringMap(final long timeToLive, final TimeUnit timeUnit, final Map<K, V> map) { this(validateAndConvertToMillis(timeToLive, timeUnit), map); }
public MQTTSession(Vertx vertx, ConfigParser config) { this.vertx = vertx; this.decoder = new MQTTDecoder(); this.encoder = new MQTTEncoder(); this.securityEnabled = config.isSecurityEnabled(); this.retainSupport = config.isRetainSupport(); this.subscriptions = new LinkedHashMap<>(); this.qosUtils = new QOSUtils(); PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<String, List<Subscription>> expirePeriod = new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<>( 30, TimeUnit.MINUTES); this.matchingSubscriptionsCache = new PassiveExpiringMap<>( expirePeriod, new HashMap<>() ); this.topicsManager = new MQTTTopicsManagerOptimized(); this.storeManager = new StoreManager(this.vertx); this.authenticatorAddress = config.getAuthenticatorAddress(); this.queue = new LinkedList<>(); }
/** * All expired entries are removed from the map prior to returning the key set. * {@inheritDoc} */ @Override public Set<K> keySet() { removeAllExpired(now()); return super.keySet(); }
@Override public CompletableFuture<Void> addToken(String username, AccessToken accessToken) { Preconditions.checkNotNull(username); Preconditions.checkArgument(! username.isEmpty(), "Username should not be empty"); Preconditions.checkNotNull(accessToken); synchronized (tokensExpirationDates) { tokensExpirationDates.put(accessToken, username); } return CompletableFuture.completedFuture(null); }
expirePeriod = new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<>( 1, TimeUnit.DAYS); this.db = new PassiveExpiringMap<>( expirePeriod, new LinkedHashMap<>() ); this.topicsManager = new MQTTTopicsManagerOptimized();
/** * All expired entries are removed from the map prior to returning the size. * {@inheritDoc} */ @Override public int size() { removeAllExpired(now()); return super.size(); }
/** * All expired entries are removed from the map prior to returning the entry set. * {@inheritDoc} */ @Override public Set<Entry<K, V>> entrySet() { removeAllExpired(now()); return super.entrySet(); }
/** * All expired entries are removed from the map prior to determining if it is empty. * {@inheritDoc} */ @Override public boolean isEmpty() { removeAllExpired(now()); return super.isEmpty(); }
/** * All expired entries are removed from the map prior to returning the value collection. * {@inheritDoc} */ @Override public Collection<V> values() { removeAllExpired(now()); return super.values(); } }