diff --git a/src/main/java/core/basesyntax/impl/Pair.java b/src/main/java/core/basesyntax/impl/Pair.java new file mode 100644 index 000000000..6ccee79ed --- /dev/null +++ b/src/main/java/core/basesyntax/impl/Pair.java @@ -0,0 +1,23 @@ +package core.basesyntax.impl; + +public class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + public void setValue(V value) { + this.value = value; + } +} diff --git a/src/main/java/core/basesyntax/impl/StorageImpl.java b/src/main/java/core/basesyntax/impl/StorageImpl.java index 455a7b080..57af03d9b 100644 --- a/src/main/java/core/basesyntax/impl/StorageImpl.java +++ b/src/main/java/core/basesyntax/impl/StorageImpl.java @@ -3,17 +3,59 @@ import core.basesyntax.Storage; public class StorageImpl implements Storage { + private static final int MAX_SIZE = 10; + private Pair[] storage; + private int size; + + public StorageImpl() { + this.size = 0; + this.storage = new Pair[MAX_SIZE]; + } + + private Pair findPair(K key) { + for (Pair item : storage) { + if (item == null) { + continue; + } + + if ((key != null + && item != null + && key.equals(item.getKey()) + ) + || item.getKey() == key) { + return item; + } + } + + return null; + } + @Override public void put(K key, V value) { + Pair pair = this.findPair(key); + + if (pair != null) { + pair.setValue(value); + return; + } + + this.storage[size] = new Pair(key, value); + this.size++; } @Override public V get(K key) { - return null; + Pair pair = this.findPair(key); + + if (pair == null) { + return null; + } + + return (V) pair.getValue(); } @Override public int size() { - return -1; + return this.size; } }