Skip to content

Commit 79e322b

Browse files
authored
Extend API v10 (#57)
1 parent 9ea5173 commit 79e322b

File tree

51 files changed

+1544
-1006
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1544
-1006
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ repositories {
4545
}
4646
4747
ext {
48-
rlibVersion = "10.0.alpha10"
48+
rlibVersion = "10.0.alpha11"
4949
}
5050
5151
dependencies {

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
rootProject.version = "10.0.alpha10"
1+
rootProject.version = "10.0.alpha11"
22
group = 'javasabr.rlib'
33

44
allprojects {

rlib-classpath/src/test/java/javasabr/rlib/classpath/ClasspathScannerTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package javasabr.rlib.classpath;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import java.util.Collection;
46
import javasabr.rlib.collections.array.Array;
57
import javasabr.rlib.collections.array.impl.AbstractArray;
68
import javasabr.rlib.logger.api.LoggerLevel;
79
import javasabr.rlib.logger.api.LoggerManager;
8-
import org.junit.jupiter.api.Assertions;
910
import org.junit.jupiter.api.Test;
1011

1112
/**
@@ -26,10 +27,10 @@ void testSystemClasspathScanner() {
2627

2728
Array<Class<Collection>> implementations = scanner.findImplementations(Collection.class);
2829

29-
Assertions.assertFalse(implementations.isEmpty());
30+
assertThat(implementations.isEmpty()).isFalse();
3031

3132
Array<Class<AbstractArray>> inherited = scanner.findInherited(AbstractArray.class);
3233

33-
Assertions.assertFalse(inherited.isEmpty());
34+
assertThat(inherited.isEmpty()).isFalse();
3435
}
3536
}

rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableArray.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.stream.StreamSupport;
1212
import javasabr.rlib.collections.array.Array;
1313
import javasabr.rlib.collections.array.UnsafeMutableArray;
14-
import javasabr.rlib.common.util.ObjectUtils;
1514
import lombok.AccessLevel;
1615
import lombok.experimental.FieldDefaults;
1716
import org.jspecify.annotations.Nullable;

rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableIntToRefDictionary.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ static <V> MutableIntToRefDictionary<V> ofTypes(Class<V> valueType) {
2525
@Nullable
2626
V put(int key, V value);
2727

28+
/**
29+
* @return the existing value if the key is already present, or null if the key was absent and the new mapping was added.
30+
*/
31+
@Nullable
32+
V putIfAbsent(int key, V value);
33+
2834
void putAll(IntToRefDictionary<? extends V> dictionary);
2935

3036
MutableIntToRefDictionary<V> append(IntToRefDictionary<? extends V> dictionary);
@@ -40,6 +46,11 @@ static <V> MutableIntToRefDictionary<V> ofTypes(Class<V> valueType) {
4046
@Nullable
4147
V remove(int key);
4248

49+
/**
50+
* @return true if the expectedValue was removed
51+
*/
52+
boolean remove(int key, V expectedValue);
53+
4354
/**
4455
* @return the optional value of the previous value for the key.
4556
*/

rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableLongToRefDictionary.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ static <V> MutableLongToRefDictionary<V> ofTypes(Class<V> valueType) {
2525
@Nullable
2626
V put(long key, V value);
2727

28+
/**
29+
* @return the existing value if the key is already present, or null if the key was absent and the new mapping was added.
30+
*/
31+
@Nullable
32+
V putIfAbsent(long key, V value);
33+
2834
void putAll(LongToRefDictionary<? extends V> dictionary);
2935

3036
MutableLongToRefDictionary<V> append(LongToRefDictionary<? extends V> dictionary);
@@ -40,6 +46,11 @@ static <V> MutableLongToRefDictionary<V> ofTypes(Class<V> valueType) {
4046
@Nullable
4147
V remove(long key);
4248

49+
/**
50+
* @return true if the expectedValue was removed
51+
*/
52+
boolean remove(long key, V expectedValue);
53+
4354
/**
4455
* @return the optional value of the previous value for the key.
4556
*/

rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableRefToRefDictionary.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ static <K, V> MutableRefToRefDictionary<K, V> ofTypes(
2626
@Nullable
2727
V put(K key, V value);
2828

29+
/**
30+
* @return the existing value if the key is already present, or null if the key was absent and the new mapping was added.
31+
*/
32+
@Nullable
33+
V putIfAbsent(K key, V value);
34+
2935
void putAll(RefToRefDictionary<? extends K, ? extends V> dictionary);
3036

3137
MutableRefToRefDictionary<K, V> append(RefToRefDictionary<? extends K, ? extends V> dictionary);
@@ -41,6 +47,11 @@ static <K, V> MutableRefToRefDictionary<K, V> ofTypes(
4147
@Nullable
4248
V remove(K key);
4349

50+
/**
51+
* @return true if the expectedValue was removed
52+
*/
53+
boolean remove(K key, V expectedValue);
54+
4455
/**
4556
* @return the optional value of the previous value for the key.
4657
*/

rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/impl/AbstractMutableHashBasedIntToRefDictionary.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javasabr.rlib.collections.dictionary.impl;
22

3+
import java.util.Objects;
34
import java.util.Optional;
45
import java.util.function.Function;
56
import java.util.function.IntFunction;
@@ -84,6 +85,23 @@ public V put(int key, V value) {
8485
return null;
8586
}
8687

88+
@Nullable
89+
@Override
90+
public V putIfAbsent(int key, V value) {
91+
@Nullable E[] entries = entries();
92+
int hash = hash(key);
93+
int entryIndex = indexFor(hash, entries.length);
94+
95+
for (E entry = entries[entryIndex]; entry != null; entry = entry.next()) {
96+
if (entry.hash() == hash && key == entry.key()) {
97+
return entry.value();
98+
}
99+
}
100+
101+
addEntry(hash, key, value, entryIndex);
102+
return null;
103+
}
104+
87105
@Override
88106
public Optional<V> putOptional(int key, V value) {
89107
return Optional.ofNullable(put(key, value));
@@ -120,6 +138,37 @@ public V remove(int key) {
120138
return removed.value();
121139
}
122140

141+
@Override
142+
public boolean remove(int key, V expectedValue) {
143+
@Nullable E[] entries = entries();
144+
int hash = hash(key);
145+
int entryIndex = indexFor(hash, entries.length);
146+
147+
E previousEntry = entries[entryIndex];
148+
E entry = previousEntry;
149+
150+
while (entry != null) {
151+
E nextEntry = entry.next();
152+
if (entry.hash() == hash && key == entry.key()) {
153+
if (Objects.equals(entry.value(), expectedValue)) {
154+
decrementSize();
155+
if (previousEntry == entry) {
156+
entries[entryIndex] = nextEntry;
157+
} else {
158+
previousEntry.next(nextEntry);
159+
}
160+
return true;
161+
} else {
162+
return false;
163+
}
164+
}
165+
previousEntry = entry;
166+
entry = nextEntry;
167+
}
168+
169+
return false;
170+
}
171+
123172
@Override
124173
public Optional<V> removeOptional(int key) {
125174
return Optional.ofNullable(remove(key));

rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/impl/AbstractMutableHashBasedLongToRefDictionary.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javasabr.rlib.collections.dictionary.impl;
22

3+
import java.util.Objects;
34
import java.util.Optional;
45
import java.util.function.Function;
56
import java.util.function.LongFunction;
@@ -83,7 +84,24 @@ public V put(long key, V value) {
8384
addEntry(hash, key, value, entryIndex);
8485
return null;
8586
}
87+
88+
@Nullable
89+
@Override
90+
public V putIfAbsent(long key, V value) {
91+
@Nullable E[] entries = entries();
92+
int hash = hash(Long.hashCode(key));
93+
int entryIndex = indexFor(hash, entries.length);
8694

95+
for (E entry = entries[entryIndex]; entry != null; entry = entry.next()) {
96+
if (entry.hash() == hash && key == entry.key()) {
97+
return entry.value();
98+
}
99+
}
100+
101+
addEntry(hash, key, value, entryIndex);
102+
return null;
103+
}
104+
87105
@Override
88106
public Optional<V> putOptional(long key, V value) {
89107
return Optional.ofNullable(put(key, value));
@@ -120,6 +138,37 @@ public V remove(long key) {
120138
return removed.value();
121139
}
122140

141+
@Override
142+
public boolean remove(long key, V expectedValue) {
143+
@Nullable E[] entries = entries();
144+
int hash = hash(Long.hashCode(key));
145+
int entryIndex = indexFor(hash, entries.length);
146+
147+
E previousEntry = entries[entryIndex];
148+
E entry = previousEntry;
149+
150+
while (entry != null) {
151+
E nextEntry = entry.next();
152+
if (entry.hash() == hash && key == entry.key()) {
153+
if (Objects.equals(entry.value(), expectedValue)) {
154+
decrementSize();
155+
if (previousEntry == entry) {
156+
entries[entryIndex] = nextEntry;
157+
} else {
158+
previousEntry.next(nextEntry);
159+
}
160+
return true;
161+
} else {
162+
return false;
163+
}
164+
}
165+
previousEntry = entry;
166+
entry = nextEntry;
167+
}
168+
169+
return false;
170+
}
171+
123172
@Override
124173
public Optional<V> removeOptional(long key) {
125174
return Optional.ofNullable(remove(key));

rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/impl/AbstractMutableHashBasedRefToRefDictionary.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javasabr.rlib.collections.dictionary.impl;
22

3+
import java.util.Objects;
34
import java.util.Optional;
45
import java.util.function.Function;
56
import java.util.function.Supplier;
@@ -83,6 +84,23 @@ public V put(K key, V value) {
8384
return null;
8485
}
8586

87+
@Nullable
88+
@Override
89+
public V putIfAbsent(K key, V value) {
90+
@Nullable E[] entries = entries();
91+
int hash = hash(key.hashCode());
92+
int entryIndex = indexFor(hash, entries.length);
93+
94+
for (E entry = entries[entryIndex]; entry != null; entry = entry.next()) {
95+
if (entry.hash() == hash && key.equals(entry.key())) {
96+
return entry.value();
97+
}
98+
}
99+
100+
addEntry(hash, key, value, entryIndex);
101+
return null;
102+
}
103+
86104
@Override
87105
public Optional<V> putOptional(K key, V value) {
88106
return Optional.ofNullable(put(key, value));
@@ -119,6 +137,37 @@ public V remove(K key) {
119137
return removed.value();
120138
}
121139

140+
@Override
141+
public boolean remove(K key, V expectedValue) {
142+
@Nullable E[] entries = entries();
143+
int hash = hash(key.hashCode());
144+
int entryIndex = indexFor(hash, entries.length);
145+
146+
E previousEntry = entries[entryIndex];
147+
E entry = previousEntry;
148+
149+
while (entry != null) {
150+
E nextEntry = entry.next();
151+
if (entry.hash() == hash && key.equals(entry.key())) {
152+
if (Objects.equals(entry.value(), expectedValue)) {
153+
decrementSize();
154+
if (previousEntry == entry) {
155+
entries[entryIndex] = nextEntry;
156+
} else {
157+
previousEntry.next(nextEntry);
158+
}
159+
return true;
160+
} else {
161+
return false;
162+
}
163+
}
164+
previousEntry = entry;
165+
entry = nextEntry;
166+
}
167+
168+
return false;
169+
}
170+
122171
@Override
123172
public Optional<V> removeOptional(K key) {
124173
return Optional.ofNullable(remove(key));

0 commit comments

Comments
 (0)