Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating containsKey method in PassiveExpiringMap #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void clear() {
*/
@Override
public boolean containsKey(final Object key) {
removeIfExpired(key, now());
removeAllExpired(now());
return super.containsKey(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ public long expirationTime(final Integer key, final String value) {
}
}

/**
* alternate policy that will cause multiples of 3 to expire after 250 milliseconds, all others never expire.
*/
private static class AlternateExpirationPolicy implements ExpirationPolicy<Integer, String> {
private static final long serialVersionUID = 1L;

@Override
public long expirationTime(Integer key, String value) {
if (key.intValue() % 3 != 0) {
return -1;
}

return 250;
}
}

public static Test suite() {
return BulkTest.makeSuite(PassiveExpiringMapTest.class);
}
Expand Down Expand Up @@ -101,6 +117,18 @@ private Map<Integer, String> makeTestMap() {
return m;
}

private PassiveExpiringMap<Integer, String> makePassiveExpiringMap() {
final PassiveExpiringMap<Integer, String> m =
new PassiveExpiringMap<>(new AlternateExpirationPolicy());
m.put(Integer.valueOf(1), "one");
m.put(Integer.valueOf(2), "two");
m.put(Integer.valueOf(3), "three");
m.put(Integer.valueOf(4), "four");
m.put(Integer.valueOf(5), "five");
m.put(Integer.valueOf(6), "six");
return m;
}

public void testConstructors() {
try {
final Map<String, String> map = null;
Expand Down Expand Up @@ -128,13 +156,20 @@ public void testConstructors() {
}

public void testContainsKey() {
final Map<Integer, String> m = makeTestMap();
assertFalse(m.containsKey(Integer.valueOf(1)));
assertFalse(m.containsKey(Integer.valueOf(3)));
assertFalse(m.containsKey(Integer.valueOf(5)));
assertTrue(m.containsKey(Integer.valueOf(2)));
assertTrue(m.containsKey(Integer.valueOf(4)));
assertTrue(m.containsKey(Integer.valueOf(6)));
final PassiveExpiringMap<Integer, String> m = makePassiveExpiringMap();
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
assertEquals(6, m.decorated().size());
assertTrue(m.containsKey(1));
assertEquals(4, m.decorated().size());
assertTrue(m.containsKey(2));
assertTrue(m.containsKey(4));
assertTrue(m.containsKey(5));
assertFalse(m.containsKey(3));
assertFalse(m.containsKey(6));
assertEquals(4, m.decorated().size());
}

public void testContainsValue() {
Expand Down