Merge pull request #8846 from ramkumarvenkat/BAEL-3886

Guava MapMaker
This commit is contained in:
Jonathan Cook 2020-04-08 17:12:34 +02:00 committed by GitHub
commit 5b92f11303
4 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.baeldung.guava.mapmaker;
public class Profile {
private long id;
private String type;
public Profile(long id, String type) {
this.id = id;
this.type = type;
}
public long getId() {
return id;
}
public String getName() {
return type;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.guava.mapmaker;
public class Session {
private long id;
public Session(long id) {
this.id = id;
}
public long getId() {
return id;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.guava.mapmaker;
public class User {
private long id;
private String name;
public User(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.guava.mapmaker;
import com.google.common.collect.MapMaker;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.ConcurrentMap;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
public class GuavaMapMakerUnitTest {
@Test
public void whenCreateCaches_thenCreated() {
ConcurrentMap<User, Session> sessionCache = new MapMaker().makeMap();
assertNotNull(sessionCache);
ConcurrentMap<User, Profile> profileCache = new MapMaker().makeMap();
assertNotNull(profileCache);
User userA = new User(1, "UserA");
sessionCache.put(userA, new Session(100));
Assert.assertThat(sessionCache.size(), equalTo(1));
profileCache.put(userA, new Profile(1000, "Personal"));
Assert.assertThat(profileCache.size(), equalTo(1));
}
@Test
public void whenCreateCacheWithInitialCapacity_thenCreated() {
ConcurrentMap<User, Profile> profileCache = new MapMaker().initialCapacity(100).makeMap();
assertNotNull(profileCache);
}
@Test
public void whenCreateCacheWithConcurrencyLevel_thenCreated() {
ConcurrentMap<User, Session> sessionCache = new MapMaker().concurrencyLevel(10).makeMap();
assertNotNull(sessionCache);
}
@Test
public void whenCreateCacheWithWeakKeys_thenCreated() {
ConcurrentMap<User, Session> sessionCache = new MapMaker().weakKeys().makeMap();
assertNotNull(sessionCache);
}
@Test
public void whenCreateCacheWithWeakValues_thenCreated() {
ConcurrentMap<User, Profile> profileCache = new MapMaker().weakValues().makeMap();
assertNotNull(profileCache);
}
}