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

import core.basesyntax.impl.StorageImpl;

public class Application {
public static void main(String[] args) {
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 constant name MAX_SIZE is not fully informative. Consider renaming to MAX_STORAGE_CAPACITY to clearly indicate its purpose.

Storage<Integer, String> storage = new StorageImpl<>();
storage.put(1, "Value1");
storage.put(2, "Value2");
storage.put(3, "Value3");

System.out.println("Get value by key 2: " + storage.get(2));
System.out.println("Storage size: " + storage.size());

storage.put(1, "NewValue1");
System.out.println("Get updated value by key 1: " + storage.get(1));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Per checklist requirements, remove empty lines between method implementations. There should be no blank line after line 15 before the next method.

}
}
43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/StorageImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax;

public class StorageImpl<K, V> implements Storage<K, V> {
Comment on lines +1 to +3
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing import statement. This file cannot implement Storage interface without importing it. Add: import core.basesyntax.Storage;

private static final int MAX_SIZE = 10;
private final K[] keys;
private final V[] values;
private int size;

public StorageImpl() {
keys = (K[]) new Object[MAX_SIZE];
values = (V[]) new Object[MAX_SIZE];
}

@Override
public void put(K key, V value) {
for (int i = 0; i < size; i++) {
if (key == null && keys[i] == null || key != null && key.equals(keys[i])) {
values[i] = value;
return;
}
}
if (size < MAX_SIZE) {
keys[size] = key;
values[size] = value;
size++;
}
}

@Override
Comment on lines +18 to +29
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 key-searching loop logic is duplicated in the get() method. Consider extracting this into a private helper method like findKeyIndex(K key) to follow DRY principle (checklist item #5).

public V get(K key) {
for (int i = 0; i < size; i++) {
if (key == null && keys[i] == null || key != null && key.equals(keys[i])) {
return values[i];
}
}
return null;
}

@Override
Comment on lines +32 to +39
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 key-searching loop logic duplicates code from put() method. Extract to a private helper method to follow DRY principle (checklist item #5).

public int size() {
return size;
}
}
29 changes: 28 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,44 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {
private static final int MAX_SIZE = 10;
private final K[] keys;
private final V[] values;
private int size;

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

@Override
public void put(K key, V value) {
for (int i = 0; i < size; i++) {
if (key == keys[i] || (key != null && key.equals(keys[i]))) {
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 will throw NullPointerException if key is null. The expression key == keys[i] is evaluated first, and if key is null, it becomes null == keys[i], but if keys[i] is also null, Java still evaluates the left side as false. However, if keys[i] is not null, calling .equals() on null throws NPE. Use null-safe comparison: key == null && keys[i] == null || key != null && key.equals(keys[i])

values[i] = value;
return;
}
}
if (size < MAX_SIZE) {
keys[size] = key;
values[size] = value;
size++;
}
}

@Override
public V get(K key) {
for (int i = 0; i < size; i++) {
if (key == keys[i] || (key != null && key.equals(keys[i]))) {
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 has the same null-handling issue as line 20. When key is null, key.equals(keys[i]) will throw NPE.

Comment on lines +19 to +35
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 null-checking logic in put() and get() methods is identical. Per DRY principle, extract this into a private helper method like findKeyIndex(K key) to avoid code duplication.

return values[i];
}
}
return null;
}

@Override
public int size() {
return -1;
return size;
}
}
Loading