Skip to content
Open
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
40 changes: 39 additions & 1 deletion 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 int size;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The 'size' field should be explicitly initialized in the constructor. According to the checklist, all non-constant fields must be initialized in the constructor.

private K[] keys;
private V[] values;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line at the beginning of the constructor implementation. This is before the constructor body starts.

@SuppressWarnings("unchecked")
public StorageImpl() {
this.size = 0;
keys = (K[]) new Object[MAX_SIZE];
values = (V[]) new Object[MAX_SIZE];
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line at the beginning of the 'put' method implementation. The @OverRide annotation on line 18 marks the start of the method, and there should be no empty line before it.

@Override
public void put(K key, V value) {
int index = findKeyIndex(key);

if (index != -1) {
values[index] = value;
return;
}
if (size == MAX_SIZE) {
throw new IllegalStateException("Storage is full");
}
keys[size] = key;
values[size] = value;
size++;
}

@Override
public V get(K key) {
return null;
int index = findKeyIndex(key);
if (index == -1) {
return null;
}
return values[index];
}

@Override
public int size() {
return size;
}

private int findKeyIndex(K key) {
for (int i = 0; i < size; i++) {
if ((keys[i] == null && key == null)
|| (keys[i] != null && keys[i].equals(key))) {
return i;
}
}
return -1;
}
}
Loading