Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/impl/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax.impl;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant empty line at the start of class declaration. The checklist requires removing all redundant empty lines - do not begin class implementation with an empty line.

public class Pair<K, V> {
private K key;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant empty line at the start of class declaration. The checklist requires removing all redundant empty lines - do not begin class implementation with an empty line.

private V value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant is missing the final keyword. According to checklist item #3, magic numbers should be constants with informative names, but a constant must be declared as final to actually be a constant. Change to: private static final int MAX_ELEMENTS_AMOUNT = 10;

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;
}
}
42 changes: 40 additions & 2 deletions src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,55 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {
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;
}
}
Loading