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
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
package core.basesyntax.impl;

import core.basesyntax.Storage;
import java.util.Objects;

public class StorageImpl<K, V> implements Storage<K, V> {
private static final int MAX_SIZE = 10;
private K[] keys = (K[]) new Object[10];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #3: Magic number 10 should use the MAX_SIZE constant. Replace new Object[10] with new Object[MAX_SIZE].

private V[] values = (V[]) new Object[10];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #3: Magic number 10 should use the MAX_SIZE constant. Replace new Object[10] with new Object[MAX_SIZE].

private int size = 0;
Comment on lines +8 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #2: All non-constant fields should be initialized in the constructor, not at declaration. Move keys, values, and size initialization to a constructor.


@Override
public void put(K key, V value) {
if (getIndex(key) >= 0) {
values[getIndex(key)] = value;
Comment on lines +14 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #4: getIndex(key) is called twice - once on line 14 for the condition and again on line 15 for assignment. Store the result in a variable to avoid repeating the method call.

} else {
size++;
if (size > MAX_SIZE) {
throw new RuntimeException("Can't add new element. Maximum size reached.");
}
keys[size - 1] = key;
values[size - 1] = value;
}
}

@Override
public V get(K key) {
int index = getIndex(key);
if (index >= 0) {
return values[index];
}
return null;
}

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

private int getIndex(K key) {
for (int i = 0; i < size; i++) {
if (Objects.equals(key, keys[i])) {
return i;
}
}
return -1;
}
}
Loading