Iterator<Cache.Entry<Number, CharSequence>> iterator = testCache.iterator(); assertThat(iterator.hasNext(), is(true));
@Test public void iterator() { cache.put(1, "a"); cache.put(2, "b"); changesOf(0, 0, 2, 0); Iterator<Cache.Entry<Integer, String>> iterator = cache.iterator(); changesOf(0, 0, 0, 0); iterator.next().getKey(); changesOf(1, 0, 0, 0); expect(iterator.hasNext()).isTrue(); changesOf(0, 0, 0, 0); iterator.next().getKey(); changesOf(1, 0, 0, 0); expect(iterator.hasNext()).isFalse(); changesOf(0, 0, 0, 0); iterator.remove(); changesOf(0, 0, 0, 1); }
@Override public Set<String> getKeys() { Set<String> keys = new HashSet<>(); Iterator<Cache.Entry<String, DefaultExchangeHolder>> entries = cache.iterator(); while (entries.hasNext()) { keys.add(entries.next().getKey()); } return Collections.unmodifiableSet(keys); }
/** * {@inheritDoc} */ @Override public Iterator enumerateKeys() { return getCache().iterator(); }
@Override public List<Client> getClients(UserSubject resourceOwner) { List<Client> clients = new ArrayList<>(); for (Iterator<Cache.Entry<String, Client>> it = clientCache.iterator(); it.hasNext();) { Cache.Entry<String, Client> entry = it.next(); Client client = entry.getValue(); if (isClientMatched(client, resourceOwner)) { clients.add(client); } } return clients; }
@Override public List<Client> getClients(UserSubject resourceOwner) { List<Client> clients = new ArrayList<>(); for (Iterator<Cache.Entry<String, Client>> it = clientCache.iterator(); it.hasNext();) { Cache.Entry<String, Client> entry = it.next(); Client client = entry.getValue(); if (isClientMatched(client, resourceOwner)) { clients.add(client); } } return clients; }
/** * print all of the content of the cache using Iterator, if expires or not exist you will see no value */ void printContentWithIterator(Cache<String, Integer> cache) { System.out.println("==============> " + cache.getName() + " <====================="); Iterator<Cache.Entry<String, Integer>> iterator = cache.iterator(); while (iterator.hasNext()) { Cache.Entry<String, Integer> next = iterator.next(); System.out.println("Key: " + next.getKey() + ", Value: " + next.getValue()); } System.out.println("============================================================"); }
@Override public void run() { while (!Thread.interrupted()) { Iterator<Cache.Entry<String, Integer>> iterator = cacheAtCluster.iterator(); while (iterator.hasNext()) { Cache.Entry<String, Integer> entry = iterator.next(); // value maybe null which means it is expired if (entry.getValue() != null) { // poor entry just remove iterator.remove(); // do something with entry, save to db etc. } } if (!sleepMillis(1000)) { return; } } } });
protected static <K, V extends ServerAccessToken> List<V> getTokens(Cache<K, V> cache, Client client, UserSubject sub) { final Set<K> toRemove = new HashSet<>(); final List<V> tokens = new ArrayList<>(); for (Iterator<Cache.Entry<K, V>> it = cache.iterator(); it.hasNext();) { Cache.Entry<K, V> entry = it.next(); V token = entry.getValue(); if (isExpired(token)) { toRemove.add(entry.getKey()); } else if (isTokenMatched(token, client, sub)) { tokens.add(token); } } cache.removeAll(toRemove); return tokens; }
protected static <K, V extends ServerAccessToken> List<V> getTokens(Cache<K, V> cache, Client client, UserSubject sub) { final Set<K> toRemove = new HashSet<>(); final List<V> tokens = new ArrayList<>(); for (Iterator<Cache.Entry<K, V>> it = cache.iterator(); it.hasNext();) { Cache.Entry<K, V> entry = it.next(); V token = entry.getValue(); if (isExpired(token)) { toRemove.add(entry.getKey()); } else if (isTokenMatched(token, client, sub)) { tokens.add(token); } } cache.removeAll(toRemove); return tokens; }
@Override public List<ServerAuthorizationCodeGrant> getCodeGrants(Client c, UserSubject sub) { final Set<String> toRemove = new HashSet<>(); final List<ServerAuthorizationCodeGrant> grants = new ArrayList<>(); for (Iterator<Cache.Entry<String, ServerAuthorizationCodeGrant>> it = grantCache.iterator(); it.hasNext();) { Cache.Entry<String, ServerAuthorizationCodeGrant> entry = it.next(); ServerAuthorizationCodeGrant grant = entry.getValue(); if (isExpired(grant)) { toRemove.add(entry.getKey()); } else if (AbstractCodeDataProvider.isCodeMatched(grant, c, sub)) { grants.add(grant); } } grantCache.removeAll(toRemove); return grants; }
@Override public List<ServerAuthorizationCodeGrant> getCodeGrants(Client c, UserSubject sub) { final Set<String> toRemove = new HashSet<>(); final List<ServerAuthorizationCodeGrant> grants = new ArrayList<>(); for (Iterator<Cache.Entry<String, ServerAuthorizationCodeGrant>> it = grantCache.iterator(); it.hasNext();) { Cache.Entry<String, ServerAuthorizationCodeGrant> entry = it.next(); ServerAuthorizationCodeGrant grant = entry.getValue(); if (isExpired(grant)) { toRemove.add(entry.getKey()); } else if (AbstractCodeDataProvider.isCodeMatched(grant, c, sub)) { grants.add(grant); } } grantCache.removeAll(toRemove); return grants; }
protected List<ServerAccessToken> getJwtAccessTokens(Client client, UserSubject sub) { final Set<String> toRemove = new HashSet<>(); final List<ServerAccessToken> tokens = new ArrayList<>(); for (Iterator<Cache.Entry<String, String>> it = jwtAccessTokenCache.iterator(); it.hasNext();) { Cache.Entry<String, String> entry = it.next(); String jose = entry.getValue(); JoseJwtConsumer theConsumer = jwtTokenConsumer == null ? new JoseJwtConsumer() : jwtTokenConsumer; ServerAccessToken token = JwtTokenUtils.createAccessTokenFromJwt(theConsumer, jose, this, super.getJwtAccessTokenClaimMap()); if (isExpired(token)) { toRemove.add(entry.getKey()); } else if (isTokenMatched(token, client, sub)) { tokens.add(token); } } jwtAccessTokenCache.removeAll(toRemove); return tokens; }
protected List<ServerAccessToken> getJwtAccessTokens(Client client, UserSubject sub) { final Set<String> toRemove = new HashSet<>(); final List<ServerAccessToken> tokens = new ArrayList<>(); for (Iterator<Cache.Entry<String, String>> it = jwtAccessTokenCache.iterator(); it.hasNext();) { Cache.Entry<String, String> entry = it.next(); String jose = entry.getValue(); JoseJwtConsumer theConsumer = jwtTokenConsumer == null ? new JoseJwtConsumer() : jwtTokenConsumer; ServerAccessToken token = JwtTokenUtils.createAccessTokenFromJwt(theConsumer, jose, this, super.getJwtAccessTokenClaimMap()); if (isExpired(token)) { toRemove.add(entry.getKey()); } else if (isTokenMatched(token, client, sub)) { tokens.add(token); } } jwtAccessTokenCache.removeAll(toRemove); return tokens; }
private <K> void doIterator(Supplier<K> keySupplier, Cache<K, String> map1, Cache<K, String> map2) { K key1 = keySupplier.get(), key2 = keySupplier.get(), key3 = keySupplier.get(), key4 = keySupplier.get(), key5 = keySupplier.get(), key6 = keySupplier.get(); assertFalse(map1.iterator().hasNext()); Map<K, String> data = new HashMap<>(); data.put(key1, "one"); data.put(key2, "two"); data.put(key3, "three"); data.put(key4, "four"); data.put(key5, "five"); data.put(key6, "five"); map2.putAll(data); Map<K, String> res0 = new HashMap<>(); for (Cache.Entry<K, String> e : map1) res0.put(e.getKey(), e.getValue()); assertEquals(data, res0); map2.clear(); }
/** * Will fail unless CacheEntryImpl specified in pom.xml */ @Test public void testCacheEntryUnwrap() { final Class<?> unwrapClass = getUnwrapClass(Cache.Entry.class); cache.put(1l, "Tonto"); Cache.Entry entry = cache.iterator().next(); final Object unwrappedCacheEntry = entry.unwrap(unwrapClass); assertTrue(unwrapClass.isAssignableFrom(unwrappedCacheEntry.getClass())); }
@Test public void iterator() { LinkedHashMap<Long, String> data = createLSData(3); cache.putAll(data); Iterator<Cache.Entry<Long, String>> iterator = cache.iterator(); while (iterator.hasNext()) { Cache.Entry<Long, String> next = iterator.next(); assertEquals(next.getValue(), data.get(next.getKey())); iterator.remove(); data.remove(next.getKey()); } assertTrue(data.isEmpty()); }
@Test public void iterator_Empty() { Iterator<Cache.Entry<Long, String>> iterator = cache.iterator(); assertFalse(iterator.hasNext()); try { iterator.remove(); fail(); } catch (IllegalStateException e) { //good } try { iterator.next(); fail(); } catch (NoSuchElementException e) { //good } }
@Test public void iterator_Closed() { cache.close(); try { cache.iterator(); fail("should have thrown an exception - cache closed"); } catch (IllegalStateException e) { //good } }
@Test public void checkWriteAllAndDeleteAll() { assertTrue(copy.isEmpty()); assertFalse(cache.iterator().hasNext()); cache.put("foo", "bar"); assertEquals(1, copy.size()); cache.remove("foo"); assertTrue(copy.isEmpty()); cache.putAll(new HashMap<String, String>() {{ put("a", "b"); put("b", "c"); }}); assertEquals(2, copy.size()); cache.removeAll(new HashSet<String>(asList("a", "b"))); assertTrue(copy.isEmpty()); } }