Skip to content
Open
Changes from 1 commit
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> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Violates checklist item #1: Remove all redundant empty lines. Do not begin class or method implementation with empty line.

public static final int MAX_SIZE = 10;
private Object[] keys = 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.

Violates checklist item #2: All non-constant fields should be initialized in the constructor, not inline.

private Object[] values = 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.

Violates checklist item #2: All non-constant fields should be initialized in the constructor, not inline.


@Override
public void put(K key, V value) {
for (int i = 0; i < keys.length; i++) {
if (keys[i] != null && keys[i].equals(key)) {
values[i] = value;
return;
}
if (keys[i] == 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.

Violates checklist item #5 (DRY): This condition duplicates the logic from lines 14 and 18. The reference check keys[i] == key is redundant when keys[i].equals(key) already handles all cases including null.

values[i] = value;
return;
}
if (keys[i] == null && values[i] == null) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CRITICAL BUG: This condition is wrong. It checks keys[i] == null && values[i] == null, but after inserting a key-value pair, values[i] is set to a non-null value. On subsequent insertions, this condition will never be true, preventing new entries from being stored. Should only check if the key slot is empty: keys[i] == null.

values[i] = 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.

CRITICAL BUG: After fixing the empty slot condition, this line should set keys[i] = key first, then values[i] = value. Currently if the fix isn't applied, this assigns value before the key.

keys[i] = key;
return;
}

}
}

@Override
public V get(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.

Violates checklist item #1: Remove empty line before method implementation.

for (int i = 0; i < keys.length; i++) {
if (keys[i] != null && keys[i].equals(key)) {
return (V) values[i];
}
if (keys[i] == null && key == null) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Violates checklist item #5 (DRY): Duplicate logic from line 14. The condition keys[i] != null && keys[i].equals(key) already handles the case where both are non-null.

return (V) values[i];
Comment on lines +39 to +41
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 keys[i] == key check is redundant - the first condition on line 34 already handles null keys and equal keys via keys[i].equals(key). This duplicate check should be removed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@mateacademy-ai-mentor ця стрічка потрібна, вона перевіряє ключ null, в рядку 34 значення порівнюються лише якщо keys[i] != null, щоб уникнути помилки NullPointerExceptions

Comment on lines +36 to +41
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

DRY violation: The return (V) values[i] logic is duplicated in get() (lines 37 and 40). Consider extracting the index-finding logic to a private method findKeyIndex(K key) that returns the index or -1, then use it in both get() and put().

}
}
return null;
}

@Override
public int size() {
return -1;
int size = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Violates checklist item #1: Remove empty line before method implementation.

for (int i = 0; i < values.length; i++) {
if (values[i] != null) {
size++;
}
}
return size;
}

}
Loading