Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution #1590

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
56 changes: 55 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,71 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the class definition.

private static final int MAX_ARRAY_SIZE = 10;
private final K[] keys;
private final V[] values;
private int currentSize;

Choose a reason for hiding this comment

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

Suggested change
private int currentSize;
private int size;


public StorageImpl() {

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the constructor.

keys = (K[]) new Object[MAX_ARRAY_SIZE];
values = (V[]) new Object[MAX_ARRAY_SIZE];
currentSize = 0;

Choose a reason for hiding this comment

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

Suggested change
currentSize = 0;

}

@Override

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the put method.

public void put(K key, V value) {
for (int i = 0; i < currentSize; i++) {

Choose a reason for hiding this comment

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

There is repeating code for checking if the key is already in the storage. Consider extracting this logic into a private method to adhere to the DRY principle.

if (key == null && keys[i] == null) {

Choose a reason for hiding this comment

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

The comparison of keys using '==' does not account for the potential logical equality of key objects. It is better to use 'equals' method with a null check.

values[i] = value;
return;
}
}

if (key == null) {
if (currentSize < MAX_ARRAY_SIZE) {
keys[currentSize] = null;
values[currentSize] = value;
currentSize++;
} else {
throw new IllegalStateException("Storage is full");
}
return;
}

for (int i = 0; i < currentSize; i++) {
if (keys[i] != null && keys[i].equals(key)) {

Choose a reason for hiding this comment

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

The null check for 'keys[i]' is not necessary here as it's already handled by the previous loop (lines 19-23).

values[i] = value;
return;
}
}

if (currentSize < MAX_ARRAY_SIZE) {
keys[currentSize] = key;
values[currentSize] = value;
currentSize++;
} else {
throw new IllegalStateException("Storage is full");
}
}

@Override

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the get method.

public V get(K key) {
for (int i = 0; i < currentSize; i++) {

Choose a reason for hiding this comment

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

The same repeating code for checking if the key is already in the storage is found here as well. It's a good practice to create a private method for such repeated logic.

if (key == null && keys[i] == null) {

Choose a reason for hiding this comment

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

As mentioned before, consider using 'equals' method with a null check for comparing keys instead of '==', to ensure logical equality is considered.

return values[i];
}
}

for (int i = 0; i < currentSize; i++) {
if (keys[i] != null && keys[i].equals(key)) {

Choose a reason for hiding this comment

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

Similar to the previous comment, the null check for 'keys[i]' is not necessary here as it's already handled by the previous loop (lines 54-58).

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

@Override

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the size method.

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