Skip to content
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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.17</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.12.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.12.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
</project>
2 changes: 1 addition & 1 deletion src/main/java/org/redis/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.redis;

public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
}
}
107 changes: 107 additions & 0 deletions src/main/java/org/redis/RedisMap.java
Original file line number Diff line number Diff line change
@@ -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<String, String> {
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<String> 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<? extends String, ? extends String> 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<String> keySet() {
try (Jedis jedis = jedisPool.getResource()) {
return jedis.hkeys(field);
}
}

@Override
public Collection<String> values() {
try (Jedis jedis = jedisPool.getResource()) {
return jedis.hvals(field);
}
}

@Override
public Set<Entry<String, String>> entrySet() {
try (Jedis jedis = jedisPool.getResource()) {
return jedis.hgetAll(field).entrySet();
}
}
}
103 changes: 103 additions & 0 deletions src/test/java/org/redis/RedisMapTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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<String, String> animals = new HashMap<>();
animals.put("1", "cat");
animals.put("2", "dog");
animals.put("3", "cow");
redisMap.putAll(animals);
assertIterableEquals(animals.entrySet(), redisMap.entrySet());
}

}