-
Notifications
You must be signed in to change notification settings - Fork 6
Extend API v10 #57
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
Extend API v10 #57
Changes from 5 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 |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ repositories { | |
| } | ||
|
|
||
| ext { | ||
| rlibVersion = "10.0.alpha10" | ||
| rlibVersion = "10.0.alpha11" | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| rootProject.version = "10.0.alpha10" | ||
| rootProject.version = "10.0.alpha11" | ||
| group = 'javasabr.rlib' | ||
|
|
||
| allprojects { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,12 @@ static <V> MutableLongToRefDictionary<V> ofTypes(Class<V> valueType) { | |||||||
| @Nullable | ||||||||
| V put(long key, V value); | ||||||||
|
|
||||||||
| /** | ||||||||
| * @return the previous value associated with the specified key, or null if there was no mapping for the key. | ||||||||
|
||||||||
| * @return the previous value associated with the specified key, or null if there was no mapping for the key. | |
| * @return the existing value associated with the specified key if the key is already present, | |
| * or null if the key was absent and the new mapping was added. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| package javasabr.rlib.collections.dictionary.impl; | ||||||
|
|
||||||
| import java.util.Objects; | ||||||
| import java.util.Optional; | ||||||
| import java.util.function.Function; | ||||||
| import java.util.function.IntFunction; | ||||||
|
|
@@ -84,6 +85,23 @@ public V put(int key, V value) { | |||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| @Nullable | ||||||
| @Override | ||||||
| public V putIfAbsent(int key, V value) { | ||||||
| @Nullable E[] entries = entries(); | ||||||
| int hash = hash(key); | ||||||
| int entryIndex = indexFor(hash, entries.length); | ||||||
|
|
||||||
| for (E entry = entries[entryIndex]; entry != null; entry = entry.next()) { | ||||||
| if (entry.hash() == hash && key == entry.key()) { | ||||||
| return null; | ||||||
|
||||||
| return null; | |
| return entry.value(); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package javasabr.rlib.collections.dictionary.impl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Optional; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.function.Function; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.function.LongFunction; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -83,7 +84,24 @@ public V put(long key, V value) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addEntry(hash, key, value, entryIndex); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Nullable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public V putIfAbsent(long key, V value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Nullable E[] entries = entries(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int hash = hash(Long.hashCode(key)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int entryIndex = indexFor(hash, entries.length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (E entry = entries[entryIndex]; entry != null; entry = entry.next()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (entry.hash() == hash && key == entry.key()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
JavaSaBr marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addEntry(hash, key, value, entryIndex); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public Optional<V> putOptional(long key, V value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Optional.ofNullable(put(key, value)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -120,6 +138,37 @@ public V remove(long key) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return removed.value(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public boolean remove(long key, V expectedValue) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Nullable E[] entries = entries(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int hash = hash(Long.hashCode(key)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int entryIndex = indexFor(hash, entries.length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| E previosEntry = entries[entryIndex]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| E entry = previosEntry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (entry != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| E nextEntry = entry.next(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (entry.hash() == hash && key == entry.key()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Objects.equals(entry.value(), expectedValue)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| decrementSize(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (previosEntry == entry) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| entries[entryIndex] = nextEntry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| previosEntry.next(nextEntry); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| previosEntry = entry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| E previosEntry = entries[entryIndex]; | |
| E entry = previosEntry; | |
| while (entry != null) { | |
| E nextEntry = entry.next(); | |
| if (entry.hash() == hash && key == entry.key()) { | |
| if (Objects.equals(entry.value(), expectedValue)) { | |
| decrementSize(); | |
| if (previosEntry == entry) { | |
| entries[entryIndex] = nextEntry; | |
| } else { | |
| previosEntry.next(nextEntry); | |
| } | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| previosEntry = entry; | |
| E previousEntry = entries[entryIndex]; | |
| E entry = previousEntry; | |
| while (entry != null) { | |
| E nextEntry = entry.next(); | |
| if (entry.hash() == hash && key == entry.key()) { | |
| if (Objects.equals(entry.value(), expectedValue)) { | |
| decrementSize(); | |
| if (previousEntry == entry) { | |
| entries[entryIndex] = nextEntry; | |
| } else { | |
| previousEntry.next(nextEntry); | |
| } | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| previousEntry = entry; |
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.
The javadoc comment is incorrect. The method should return "the existing value if the key is already present, or null if the key was absent and the new mapping was added" - not "the previous value associated with the specified key". The current wording is confusing and doesn't match standard putIfAbsent semantics.