-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add storage impl #1917
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
base: master
Are you sure you want to change the base?
Add storage impl #1917
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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]; | ||
| private V[] values = (V[]) new Object[10]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| private int size = 0; | ||
|
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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]withnew Object[MAX_SIZE].