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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove this empty line. Per checklist item #1, do not begin class implementation with empty line.

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 violates checklist item #1: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)" — remove the empty line immediately after the package declaration so the import/class declaration starts right after the package line.

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.

Why there is 2 StorageImpl classes?

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

CHECKLIST ITEM #2 VIOLATION: The size field is declared on line 7 but not explicitly initialized in the constructor. Add this.size = 0; in the constructor.


@SuppressWarnings("unchecked")
public StorageImpl() {
keys = (K[]) new Object[MAX_SIZE];
values = (V[]) new Object[MAX_SIZE];
Comment on lines +11 to +12
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 violates checklist item #6: "Don't use class Objects". The constructor creates generic arrays via (K[]) new Object[MAX_SIZE] and (V[]) new Object[MAX_SIZE]. Avoid direct use of Object here per the checklist — consider using java.lang.reflect.Array.newInstance(...) or another permitted pattern to create generic arrays without relying on raw Object array creation.

Comment on lines +11 to +12
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 #6 VIOLATION: Generic arrays should not be created via direct (K[]) new Object[] cast. Use Array.newInstance approach instead for both keys (line 11) and values (line 12) arrays.

}
Comment on lines +7 to +13
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 violates checklist item #2: "All non-constant fields should be initialized in the constructor". The field size (declared here) is not explicitly initialized in the constructor. Initialize size in the constructor (for example this.size = 0;) to satisfy the requirement.


@Override
public void put(K key, V value) {
for (int i = 0; i < size; i++) {
boolean keysAreEqual = keys[i] == null ? key == null : keys[i].equals(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.

This violates checklist item #5 (DRY principle). The key comparison logic keys[i] == null ? key == null : keys[i].equals(key) is repeated identically in get() method at line 32. Extract this to a private helper method to avoid code duplication.

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

@Override
public V get(K key) {
for (int i = 0; i < size; i++) {
boolean keysAreEqual = keys[i] == null ? key == null : keys[i].equals(key);
Comment on lines +18 to +32
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 #5 VIOLATION: The key comparison logic keys[i] == null ? key == null : keys[i].equals(key) is duplicated in both put() (line 18) and get() (line 32). Extract this into a private helper method like private boolean areKeysEqual(K a, K b) to avoid repetition.

if (keysAreEqual) {
Comment on lines +18 to +33
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 violates checklist item #5: "Don't create repeating code If the logic of your code repeats - move it to the separate private method." The key-comparison expression keys[i] == null ? key == null : keys[i].equals(key) is duplicated in put() and get(). Extract it to a private helper, e.g. private boolean areKeysEqual(K a, K b) { return a == null ? b == null : a.equals(b); }, and call it from both places to remove duplication.

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

@Override
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;
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 violates checklist item #1: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)" Remove the empty line after the package/import block so the class declaration and imports start without leading blank lines.

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

@SuppressWarnings("unchecked")
public StorageImpl() {
Comment on lines +8 to +21
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 violates checklist item #2: "All non-constant fields should be initialized in the constructor". The field size is declared but not explicitly initialized in the constructor. Initialize it in the constructor (for example this.size = 0;) so all non-constant fields are set there.

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 violation: The 'size' field should be explicitly initialized in the constructor. Add 'this.size = 0;' inside the constructor.

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.

This violates checklist item #6: "Don't use class Objects". The constructor creates arrays via (K[]) new Object[MAX_SIZE] and (V[]) new Object[MAX_SIZE] (lines shown). The checklist forbids using Object class directly here; consider using java.lang.reflect.Array.newInstance or another approach accepted by the task to create generic arrays, or clarify the permitted pattern.

}

@Override
public void put(K key, V value) {
for (int i = 0; i < size; 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.

Checklist item #5 violation: Key comparison logic duplicated in put() method (lines 27-30). Extract to a private helper method like 'private boolean areKeysEqual(K a, K b)' to follow DRY principle.

boolean keysAreEqual = keys[i] == null ? key == null : keys[i].equals(key);
if (keysAreEqual) {
values[i] = value;
return;
}
}
keys[size] = key;
values[size] = value;
size++;
}

@Override
public V get(K key) {
for (int i = 0; i < size; 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.

Checklist item #5 violation: Same key comparison logic duplicated in get() method. Extract to the same private helper method.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

create

private int indexOf(K key) {
        for (int i = 0; i < size; i++) {
            if (key == keys[i] || key != null && key.equals(keys[i])) {
                return i;
            }
        }
        return NO_INDEX;
    }
Suggested change
for (int i = 0; i < size; i++) {
int index = indexOf(key);
return index == NO_INDEX ? null : values[index];

boolean keysAreEqual = keys[i] == null ? key == null : keys[i].equals(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.

This violates checklist item #5: "Don't create repeating code If the logic of your code repeats - move it to the separate private method." The key-comparison expression keys[i] == null ? key == null : keys[i].equals(key) is duplicated in put() and get(). Extract it into a private helper, e.g. private boolean areKeysEqual(K a, K b) { return a == null ? b == null : a.equals(b); }, and call that from both places to remove repetition.

if (keysAreEqual) {
return values[i];
}
}
return null;
}

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