From 9860318a485ac6705f4457f2d49d4460df331975 Mon Sep 17 00:00:00 2001 From: ramkumarvenkat Date: Tue, 10 Mar 2020 16:35:00 +0530 Subject: [PATCH 1/5] Guava MapMaker --- .../guava/mapmaker/GuavaMapMakerUnitTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java diff --git a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java new file mode 100644 index 0000000000..36c0cd8493 --- /dev/null +++ b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.guava.mapmaker; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.MapMaker; +import org.junit.Test; + +import java.util.concurrent.ConcurrentMap; + +public class GuavaMapMakerUnitTest { + @Test + public void whenMakeMap_thenCreated() { + ConcurrentMap m = new MapMaker() + .makeMap(); + assertNotNull(m); + } + + @Test + public void whenMakeMapWithWeakKeys_thenCreated() { + ConcurrentMap m = new MapMaker() + .weakKeys() + .makeMap(); + assertNotNull(m); + } + + @Test + public void whenMakeMapWithWeakValues_thenCreated() { + ConcurrentMap m = new MapMaker() + .weakValues() + .makeMap(); + assertNotNull(m); + } + + @Test + public void whenMakeMapWithInitialCapacity_thenCreated() { + ConcurrentMap m = new MapMaker() + .initialCapacity(10) + .makeMap(); + assertNotNull(m); + } + + @Test + public void whenMakeMapWithConcurrencyLevel_thenCreated() { + ConcurrentMap m = new MapMaker() + .concurrencyLevel(10) + .makeMap(); + assertNotNull(m); + } +} From 79433c0396ed3a147fecf0aa3d531486ba5b00c8 Mon Sep 17 00:00:00 2001 From: ramkumarvenkat Date: Thu, 12 Mar 2020 07:35:47 +0530 Subject: [PATCH 2/5] Fixed comments --- .../guava/mapmaker/GuavaMapMakerUnitTest.java | 49 +++++++------------ 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java index 36c0cd8493..d9a5fc6cd8 100644 --- a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java +++ b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java @@ -1,50 +1,35 @@ package com.baeldung.guava.mapmaker; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertEquals; - import com.google.common.collect.MapMaker; import org.junit.Test; import java.util.concurrent.ConcurrentMap; +import static org.junit.Assert.assertNotNull; + public class GuavaMapMakerUnitTest { - @Test - public void whenMakeMap_thenCreated() { - ConcurrentMap m = new MapMaker() - .makeMap(); - assertNotNull(m); + @Test public void whenMakeMap_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().makeMap(); + assertNotNull(concurrentMap); } - @Test - public void whenMakeMapWithWeakKeys_thenCreated() { - ConcurrentMap m = new MapMaker() - .weakKeys() - .makeMap(); - assertNotNull(m); + @Test public void whenMakeMapWithWeakKeys_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().weakKeys().makeMap(); + assertNotNull(concurrentMap); } - @Test - public void whenMakeMapWithWeakValues_thenCreated() { - ConcurrentMap m = new MapMaker() - .weakValues() - .makeMap(); - assertNotNull(m); + @Test public void whenMakeMapWithWeakValues_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().weakValues().makeMap(); + assertNotNull(concurrentMap); } - @Test - public void whenMakeMapWithInitialCapacity_thenCreated() { - ConcurrentMap m = new MapMaker() - .initialCapacity(10) - .makeMap(); - assertNotNull(m); + @Test public void whenMakeMapWithInitialCapacity_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().initialCapacity(10).makeMap(); + assertNotNull(concurrentMap); } - @Test - public void whenMakeMapWithConcurrencyLevel_thenCreated() { - ConcurrentMap m = new MapMaker() - .concurrencyLevel(10) - .makeMap(); - assertNotNull(m); + @Test public void whenMakeMapWithConcurrencyLevel_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().concurrencyLevel(10).makeMap(); + assertNotNull(concurrentMap); } } From d3b351e27f4e21857ffa92d8acb5f5a5efba86eb Mon Sep 17 00:00:00 2001 From: ramkumarvenkat Date: Mon, 16 Mar 2020 08:30:23 +0530 Subject: [PATCH 3/5] fix formatting --- .../guava/mapmaker/GuavaMapMakerUnitTest.java | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java index d9a5fc6cd8..8da459f22e 100644 --- a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java +++ b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java @@ -8,28 +8,33 @@ import java.util.concurrent.ConcurrentMap; import static org.junit.Assert.assertNotNull; public class GuavaMapMakerUnitTest { - @Test public void whenMakeMap_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().makeMap(); - assertNotNull(concurrentMap); - } + @Test + public void whenMakeMap_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().makeMap(); + assertNotNull(concurrentMap); + } - @Test public void whenMakeMapWithWeakKeys_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().weakKeys().makeMap(); - assertNotNull(concurrentMap); - } + @Test + public void whenMakeMapWithWeakKeys_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().weakKeys().makeMap(); + assertNotNull(concurrentMap); + } - @Test public void whenMakeMapWithWeakValues_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().weakValues().makeMap(); - assertNotNull(concurrentMap); - } + @Test + public void whenMakeMapWithWeakValues_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().weakValues().makeMap(); + assertNotNull(concurrentMap); + } - @Test public void whenMakeMapWithInitialCapacity_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().initialCapacity(10).makeMap(); - assertNotNull(concurrentMap); - } + @Test + public void whenMakeMapWithInitialCapacity_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().initialCapacity(10).makeMap(); + assertNotNull(concurrentMap); + } - @Test public void whenMakeMapWithConcurrencyLevel_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().concurrencyLevel(10).makeMap(); - assertNotNull(concurrentMap); - } + @Test + public void whenMakeMapWithConcurrencyLevel_thenCreated() { + ConcurrentMap concurrentMap = new MapMaker().concurrencyLevel(10).makeMap(); + assertNotNull(concurrentMap); + } } From a0478a7832e003ac3748ae5b574504b12e64dc1a Mon Sep 17 00:00:00 2001 From: ramkumarvenkat Date: Tue, 31 Mar 2020 12:09:13 +0530 Subject: [PATCH 4/5] Fix code after article changes --- .../com/baeldung/guava/entity/Profile.java | 20 ++++++++ .../com/baeldung/guava/entity/Session.java | 13 ++++++ .../java/com/baeldung/guava/entity/User.java | 20 ++++++++ .../guava/mapmaker/GuavaMapMakerUnitTest.java | 46 +++++++++++++------ 4 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 guava-collections-map/src/main/java/com/baeldung/guava/entity/Profile.java create mode 100644 guava-collections-map/src/main/java/com/baeldung/guava/entity/Session.java create mode 100644 guava-collections-map/src/main/java/com/baeldung/guava/entity/User.java diff --git a/guava-collections-map/src/main/java/com/baeldung/guava/entity/Profile.java b/guava-collections-map/src/main/java/com/baeldung/guava/entity/Profile.java new file mode 100644 index 0000000000..17a6502f39 --- /dev/null +++ b/guava-collections-map/src/main/java/com/baeldung/guava/entity/Profile.java @@ -0,0 +1,20 @@ +package com.baeldung.guava.entity; + +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; + } + +} diff --git a/guava-collections-map/src/main/java/com/baeldung/guava/entity/Session.java b/guava-collections-map/src/main/java/com/baeldung/guava/entity/Session.java new file mode 100644 index 0000000000..b834c23df1 --- /dev/null +++ b/guava-collections-map/src/main/java/com/baeldung/guava/entity/Session.java @@ -0,0 +1,13 @@ +package com.baeldung.guava.entity; + +public class Session { + private long id; + + public Session(long id) { + this.id = id; + } + + public long getId() { + return id; + } +} diff --git a/guava-collections-map/src/main/java/com/baeldung/guava/entity/User.java b/guava-collections-map/src/main/java/com/baeldung/guava/entity/User.java new file mode 100644 index 0000000000..613045ec23 --- /dev/null +++ b/guava-collections-map/src/main/java/com/baeldung/guava/entity/User.java @@ -0,0 +1,20 @@ +package com.baeldung.guava.entity; + +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; + } + +} diff --git a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java index 8da459f22e..e2bc1349c6 100644 --- a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java +++ b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java @@ -1,40 +1,56 @@ package com.baeldung.guava.mapmaker; +import com.baeldung.guava.entity.Profile; +import com.baeldung.guava.entity.Session; +import com.baeldung.guava.entity.User; 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 whenMakeMap_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().makeMap(); - assertNotNull(concurrentMap); + public void whenCreateCaches_thenCreated() { + ConcurrentMap sessionCache = new MapMaker().makeMap(); + assertNotNull(sessionCache); + + ConcurrentMap 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 whenMakeMapWithWeakKeys_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().weakKeys().makeMap(); - assertNotNull(concurrentMap); + public void whenCreateCacheWithInitialCapacity_thenCreated() { + ConcurrentMap profileCache = new MapMaker().initialCapacity(100).makeMap(); + assertNotNull(profileCache); } @Test - public void whenMakeMapWithWeakValues_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().weakValues().makeMap(); - assertNotNull(concurrentMap); + public void whenCreateCacheWithConcurrencyLevel_thenCreated() { + ConcurrentMap sessionCache = new MapMaker().concurrencyLevel(10).makeMap(); + assertNotNull(sessionCache); } @Test - public void whenMakeMapWithInitialCapacity_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().initialCapacity(10).makeMap(); - assertNotNull(concurrentMap); + public void whenCreateCacheWithWeakKeys_thenCreated() { + ConcurrentMap sessionCache = new MapMaker().weakKeys().makeMap(); + assertNotNull(sessionCache); } @Test - public void whenMakeMapWithConcurrencyLevel_thenCreated() { - ConcurrentMap concurrentMap = new MapMaker().concurrencyLevel(10).makeMap(); - assertNotNull(concurrentMap); + public void whenCreateCacheWithWeakValues_thenCreated() { + ConcurrentMap profileCache = new MapMaker().weakValues().makeMap(); + assertNotNull(profileCache); } } From 7187cb0dfbd3487877248742309519bed0f55d49 Mon Sep 17 00:00:00 2001 From: ramkumarvenkat Date: Thu, 2 Apr 2020 07:13:19 +0530 Subject: [PATCH 5/5] Change package name --- .../java/com/baeldung/guava/{entity => mapmaker}/Profile.java | 2 +- .../java/com/baeldung/guava/{entity => mapmaker}/Session.java | 2 +- .../java/com/baeldung/guava/{entity => mapmaker}/User.java | 2 +- .../com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java | 3 --- 4 files changed, 3 insertions(+), 6 deletions(-) rename guava-collections-map/src/main/java/com/baeldung/guava/{entity => mapmaker}/Profile.java (88%) rename guava-collections-map/src/main/java/com/baeldung/guava/{entity => mapmaker}/Session.java (81%) rename guava-collections-map/src/main/java/com/baeldung/guava/{entity => mapmaker}/User.java (88%) diff --git a/guava-collections-map/src/main/java/com/baeldung/guava/entity/Profile.java b/guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/Profile.java similarity index 88% rename from guava-collections-map/src/main/java/com/baeldung/guava/entity/Profile.java rename to guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/Profile.java index 17a6502f39..165c5a9f8f 100644 --- a/guava-collections-map/src/main/java/com/baeldung/guava/entity/Profile.java +++ b/guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/Profile.java @@ -1,4 +1,4 @@ -package com.baeldung.guava.entity; +package com.baeldung.guava.mapmaker; public class Profile { private long id; diff --git a/guava-collections-map/src/main/java/com/baeldung/guava/entity/Session.java b/guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/Session.java similarity index 81% rename from guava-collections-map/src/main/java/com/baeldung/guava/entity/Session.java rename to guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/Session.java index b834c23df1..a614f431f8 100644 --- a/guava-collections-map/src/main/java/com/baeldung/guava/entity/Session.java +++ b/guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/Session.java @@ -1,4 +1,4 @@ -package com.baeldung.guava.entity; +package com.baeldung.guava.mapmaker; public class Session { private long id; diff --git a/guava-collections-map/src/main/java/com/baeldung/guava/entity/User.java b/guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/User.java similarity index 88% rename from guava-collections-map/src/main/java/com/baeldung/guava/entity/User.java rename to guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/User.java index 613045ec23..a7f0435049 100644 --- a/guava-collections-map/src/main/java/com/baeldung/guava/entity/User.java +++ b/guava-collections-map/src/main/java/com/baeldung/guava/mapmaker/User.java @@ -1,4 +1,4 @@ -package com.baeldung.guava.entity; +package com.baeldung.guava.mapmaker; public class User { private long id; diff --git a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java index e2bc1349c6..754e3ac099 100644 --- a/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java +++ b/guava-collections-map/src/test/java/com/baeldung/guava/mapmaker/GuavaMapMakerUnitTest.java @@ -1,8 +1,5 @@ package com.baeldung.guava.mapmaker; -import com.baeldung.guava.entity.Profile; -import com.baeldung.guava.entity.Session; -import com.baeldung.guava.entity.User; import com.google.common.collect.MapMaker; import org.junit.Assert; import org.junit.Test;