diff --git a/.idea/misc.xml b/.idea/misc.xml index 8dc554d..7956a8e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + diff --git a/pom.xml b/pom.xml index b4c0aac..9a3d9c4 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,49 @@ 5.8.1 test - + + org.slf4j + slf4j-api + 2.0.17 + + + + org.slf4j + slf4j-simple + 2.0.17 + runtime + + + org.junit.jupiter + junit-jupiter-engine + 5.11.4 + test + + + + org.junit.jupiter + junit-jupiter-api + 5.11.4 + test + + + + org.junit.platform + junit-platform-commons + 1.12.2 + + + org.junit.platform + junit-platform-engine + 1.12.2 + test + + + org.junit.platform + junit-platform-launcher + 1.12.2 + test + + - \ No newline at end of file + \ No newline at end of file diff --git a/src/main/java/org/redis/Main.java b/src/main/java/org/redis/Main.java index 68b3db6..13c0472 100644 --- a/src/main/java/org/redis/Main.java +++ b/src/main/java/org/redis/Main.java @@ -1,7 +1,7 @@ package org.redis; public class Main { + public static void main(String[] args) { - System.out.println("Hello world!"); } } \ No newline at end of file diff --git a/src/main/java/org/redis/RedisMap.java b/src/main/java/org/redis/RedisMap.java new file mode 100644 index 0000000..85bf3a5 --- /dev/null +++ b/src/main/java/org/redis/RedisMap.java @@ -0,0 +1,107 @@ +package org.redis; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +public class RedisMap implements Map { + private final JedisPool jedisPool; + private final String field; + + public RedisMap(String filed) { + this.field = filed; + this.jedisPool = new JedisPool(); + } + + + @Override + public int size() { + try (Jedis jedis = jedisPool.getResource()) { + return Math.toIntExact(jedis.hlen(field)); + } + } + + @Override + public boolean isEmpty() { + try (Jedis jedis = jedisPool.getResource()) { + return jedis.hlen(field) == 0; + } + + } + + @Override + public boolean containsKey(Object key) { + try (Jedis jedis = jedisPool.getResource()) { + return jedis.hexists(field, key.toString()); + } + } + + @Override + public boolean containsValue(Object value) { + try (Jedis jedis = jedisPool.getResource()) { + Collection values = jedis.hvals("field"); + return values.contains(value.toString()); + } + } + + @Override + public String get(Object key) { + try (Jedis jedis = jedisPool.getResource()) { + return jedis.hget(field, key.toString()); + } + } + + @Override + public String put(String key, String value) { + try (Jedis jedis = jedisPool.getResource()) { + return String.valueOf(jedis.hset(field, key, value)); + } + } + + @Override + public String remove(Object key) { + try (Jedis jedis = jedisPool.getResource()) { + jedis.hdel(field, key.toString()); + return ""; + } + } + + @Override + public void putAll(Map m) { + try (Jedis jedis = jedisPool.getResource()) { + m.keySet().forEach(key -> jedis.hset(field, key, m.get(key))); + } + + } + + @Override + public void clear() { + try (Jedis jedis = jedisPool.getResource()) { + jedis.flushAll(); + } + } + + @Override + public Set keySet() { + try (Jedis jedis = jedisPool.getResource()) { + return jedis.hkeys(field); + } + } + + @Override + public Collection values() { + try (Jedis jedis = jedisPool.getResource()) { + return jedis.hvals(field); + } + } + + @Override + public Set> entrySet() { + try (Jedis jedis = jedisPool.getResource()) { + return jedis.hgetAll(field).entrySet(); + } + } +} diff --git a/src/test/java/org/redis/RedisMapTest.java b/src/test/java/org/redis/RedisMapTest.java new file mode 100644 index 0000000..7749d31 --- /dev/null +++ b/src/test/java/org/redis/RedisMapTest.java @@ -0,0 +1,103 @@ +package org.redis; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; + +class RedisMapTest { + + private RedisMap redisMap; + + @BeforeEach + public void setUp() { + this.redisMap = new RedisMap("field"); + this.redisMap.clear(); + } + + @Test + void sizeTest() { + redisMap.put("Key", "Value"); + assertEquals(1, redisMap.size()); + } + + + @Test + void EmptyTest() { + redisMap.put("Key", "Value"); + redisMap.remove("Key"); + assertTrue(redisMap.isEmpty()); + + } + + @Test + void containsKeyTest() { + redisMap.put("Key", "Value"); + assertTrue(redisMap.containsKey("Key")); + } + + @Test + void containsValueTest() { + redisMap.put("Key", "Value"); + assertTrue(redisMap.containsValue("Value")); + } + + + @Test + void getTest() { + redisMap.put("Key", "Value"); + assertEquals("Value", redisMap.get("Key")); + } + + + @Test + void putTest() { + assertEquals("1", redisMap.put("Key", "Value")); + } + + @Test + void removeTest() { + redisMap.put("Key", "Value"); + assertEquals("", redisMap.remove("Key")); + } + + @Test + void putAll() { + Map animals = new HashMap<>(); + animals.put("1", "cat"); + animals.put("2", "dog"); + animals.put("3", "cow"); + redisMap.putAll(animals); + assertEquals(3, redisMap.size()); + } + + + @Test + void keySet() { + redisMap.put("Key", "Value"); + assertIterableEquals(Set.of("Key"), redisMap.keySet()); + } + + @Test + void values() { + Map animals = new HashMap<>(); + animals.put("1", "cat"); + animals.put("2", "dog"); + animals.put("3", "cow"); + redisMap.putAll(animals); + assertIterableEquals(animals.values(), redisMap.values()); + } + + @Test + void entrySet() { + Map animals = new HashMap<>(); + animals.put("1", "cat"); + animals.put("2", "dog"); + animals.put("3", "cow"); + redisMap.putAll(animals); + assertIterableEquals(animals.entrySet(), redisMap.entrySet()); + } + +} \ No newline at end of file