BAEL-5686: Implementing a Map with Multiple Keys (#12531)

This commit is contained in:
Graham Cox 2022-07-26 20:11:45 +01:00 committed by GitHub
parent c1ffc6c2c5
commit d083a20aa9
9 changed files with 297 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.map.multikey;
import java.util.HashMap;
import java.util.Map;
public class BaseClassUserCache {
private final Map<Object, User> cache = new HashMap<>();
public User getById(String id) {
return cache.get(id);
}
public User getById(Long id) {
return cache.get(id);
}
public void storeById(String id, User user) {
cache.put(id, user);
}
public void storeById(Long id, User user) {
cache.put(id, user);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.map.multikey;
import java.util.HashMap;
import java.util.Map;
public class MultipleMapsUserCache {
private final Map<String, User> stringCache = new HashMap<>();
private final Map<Long, User> longCache = new HashMap<>();
public User getById(String id) {
return stringCache.get(id);
}
public User getById(Long id) {
return longCache.get(id);
}
public void storeById(String id, User user) {
stringCache.put(id, user);
}
public void storeById(Long id, User user) {
longCache.put(id, user);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.map.multikey;
public class User {
private final String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.map.multikey;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class WrapperClassUserCache {
private Map<CacheKey, User> cache = new HashMap<>();
public User getById(CacheKey key) {
return cache.get(key);
}
public void storeById(CacheKey key, User user) {
cache.put(key, user);
}
public static class CacheKey {
private final Object value;
public CacheKey(String value) {
this.value = value;
}
public CacheKey(Long value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey = (CacheKey) o;
return value.equals(cacheKey.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.map.multikey;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class WrapperInterfaceUserCache {
private Map<CacheKey, User> cache = new HashMap<>();
public User getById(CacheKey key) {
return cache.get(key);
}
public void storeById(CacheKey key, User user) {
cache.put(key, user);
}
public interface CacheKey {
}
public static class StringCacheKey implements CacheKey{
private final String value;
public StringCacheKey(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StringCacheKey that = (StringCacheKey) o;
return value.equals(that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
public static class LongCacheKey implements CacheKey {
private final Long value;
public LongCacheKey(Long value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LongCacheKey that = (LongCacheKey) o;
return value.equals(that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.map.multikey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class BaseClassUserCacheUnitTest {
private BaseClassUserCache cache = new BaseClassUserCache();
@BeforeEach
public void setup() {
cache.storeById("a", new User("User A"));
cache.storeById("b", new User("User B"));
cache.storeById(3L, new User("User 3"));
cache.storeById(4L, new User("User 4"));
}
@Test
public void getByString() {
User user = cache.getById("b");
assertNotNull(user);
assertEquals("User B", user.getName());
}
@Test
public void getByLong() {
User user = cache.getById(4L);
assertNotNull(user);
assertEquals("User 4", user.getName());
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.map.multikey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class MultipleMapsUserCacheUnitTest {
private MultipleMapsUserCache cache = new MultipleMapsUserCache();
@BeforeEach
public void setup() {
cache.storeById("a", new User("User A"));
cache.storeById("b", new User("User B"));
cache.storeById(3L, new User("User 3"));
cache.storeById(4L, new User("User 4"));
}
@Test
public void getByString() {
User user = cache.getById("b");
assertNotNull(user);
assertEquals("User B", user.getName());
}
@Test
public void getByLong() {
User user = cache.getById(4L);
assertNotNull(user);
assertEquals("User 4", user.getName());
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.map.multikey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class WrapperClassUserCacheUnitTest {
private WrapperClassUserCache cache = new WrapperClassUserCache();
@BeforeEach
public void setup() {
cache.storeById(new WrapperClassUserCache.CacheKey("a"), new User("User A"));
cache.storeById(new WrapperClassUserCache.CacheKey("b"), new User("User B"));
cache.storeById(new WrapperClassUserCache.CacheKey(3L), new User("User 3"));
cache.storeById(new WrapperClassUserCache.CacheKey(4L), new User("User 4"));
}
@Test
public void getByString() {
User user = cache.getById(new WrapperClassUserCache.CacheKey("b"));
assertNotNull(user);
assertEquals("User B", user.getName());
}
@Test
public void getByLong() {
User user = cache.getById(new WrapperClassUserCache.CacheKey(4L));
assertNotNull(user);
assertEquals("User 4", user.getName());
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.map.multikey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class WrapperInterfaceUserCacheUnitTest {
private WrapperInterfaceUserCache cache = new WrapperInterfaceUserCache();
@BeforeEach
public void setup() {
cache.storeById(new WrapperInterfaceUserCache.StringCacheKey("a"), new User("User A"));
cache.storeById(new WrapperInterfaceUserCache.StringCacheKey("b"), new User("User B"));
cache.storeById(new WrapperInterfaceUserCache.LongCacheKey(3L), new User("User 3"));
cache.storeById(new WrapperInterfaceUserCache.LongCacheKey(4L), new User("User 4"));
}
@Test
public void getByString() {
User user = cache.getById(new WrapperInterfaceUserCache.StringCacheKey("b"));
assertNotNull(user);
assertEquals("User B", user.getName());
}
@Test
public void getByLong() {
User user = cache.getById(new WrapperInterfaceUserCache.LongCacheKey(4L));
assertNotNull(user);
assertEquals("User 4", user.getName());
}
}