Skip to content

Commit 995abd9

Browse files
committed
emulate AtomicReference
1 parent 8744ab4 commit 995abd9

File tree

4 files changed

+114
-28
lines changed

4 files changed

+114
-28
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* __ __ __ __ __ ___
2+
* \ \ / / \ \ / / __/
3+
* \ \/ / /\ \ \/ / /
4+
* \____/__/ \__\____/__/
5+
*
6+
* Copyright 2014-2017 Vavr, http://vavr.io
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package java.util.concurrent.atomic;
21+
22+
import java.util.function.UnaryOperator;
23+
import java.util.function.BinaryOperator;
24+
25+
/**
26+
* GWT emulated version of {@link AtomicReference} wrapping reference.
27+
*/
28+
public class AtomicReference<V> implements java.io.Serializable {
29+
private static final long serialVersionUID = -1848883965231344442L;
30+
31+
private V value;
32+
33+
public AtomicReference(V initialValue) {
34+
value = initialValue;
35+
}
36+
37+
public AtomicReference() {
38+
}
39+
40+
public final V get() {
41+
return value;
42+
}
43+
44+
public final void set(V newValue) {
45+
value = newValue;
46+
}
47+
48+
public final void lazySet(V newValue) {
49+
set(newValue);
50+
}
51+
52+
public final boolean compareAndSet(V expect, V update) {
53+
if (value == expect) {
54+
value = update;
55+
return true;
56+
}
57+
return false;
58+
}
59+
60+
public final boolean weakCompareAndSet(V expect, V update) {
61+
return compareAndSet(expect, update);
62+
}
63+
64+
public final V getAndSet(V newValue) {
65+
V prev = get();
66+
set(newValue);
67+
return prev;
68+
}
69+
70+
public final V getAndUpdate(UnaryOperator<V> updateFunction) {
71+
V prev = get();
72+
set(updateFunction.apply(prev));
73+
return prev;
74+
}
75+
76+
public final V updateAndGet(UnaryOperator<V> updateFunction) {
77+
V prev = get();
78+
V next = updateFunction.apply(prev);
79+
set(next);
80+
return next;
81+
}
82+
83+
public final V getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction) {
84+
V prev = get();
85+
set(accumulatorFunction.apply(prev, x));
86+
return prev;
87+
}
88+
89+
public final V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction) {
90+
V prev = get();
91+
V next = accumulatorFunction.apply(prev, x);
92+
set(next);
93+
return next;
94+
}
95+
96+
public String toString() {
97+
return String.valueOf(get());
98+
}
99+
100+
}

src/test/java/client/ConcurrentTest1.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.util.concurrent.Executors;
1414
import java.util.concurrent.atomic.AtomicBoolean;
15+
import java.util.concurrent.atomic.AtomicReference;
1516
import java.util.function.Predicate;
1617

1718
import static io.vavr.API.Future;
@@ -46,13 +47,13 @@ public void testCreateSuccessFuture() {
4647

4748
public void testFutureSuccess() {
4849
final AtomicBoolean ok = new AtomicBoolean(false);
49-
final Ref<Predicate<Try<? extends String>>> comp = new Ref<>();
50-
final Future<String> future = Future.join(comp::setV);
50+
final AtomicReference<Predicate<Try<? extends String>>> comp = new AtomicReference<>();
51+
final Future<String> future = Future.join(comp::set);
5152
future.onComplete(value -> {
5253
ok.set(true);
5354
assertEquals("value", value.get());
5455
});
55-
comp.getV().test(Try.success("value"));
56+
comp.get().test(Try.success("value"));
5657
assertTrue("onComplete handler should have been called", ok.get());
5758
}
5859
}

src/test/java/client/ConcurrentTest2.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.vavr.control.Try;
1313

1414
import java.util.concurrent.atomic.AtomicBoolean;
15+
import java.util.concurrent.atomic.AtomicReference;
1516
import java.util.function.Predicate;
1617

1718
/**
@@ -28,29 +29,29 @@ public class ConcurrentTest2 extends GWTTestCase {
2829

2930
public void testFutureFailure() {
3031
final AtomicBoolean onFailureCalled = new AtomicBoolean(false);
31-
final Ref<Predicate<Try<? extends String>>> comp = new Ref<>();
32-
final Future<String> future = Future.join(comp::setV);
32+
final AtomicReference<Predicate<Try<? extends String>>> comp = new AtomicReference<>();
33+
final Future<String> future = Future.join(comp::set);
3334
future.onFailure(e -> {
3435
onFailureCalled.set(true);
3536
assertEquals("message", e.getMessage());
3637
});
37-
comp.getV().test(Try.failure(new Exception("message")));
38+
comp.get().test(Try.failure(new Exception("message")));
3839
assertTrue("onFailure handler should have been called", onFailureCalled.get());
3940
}
4041

4142
public void testFutureSequence() {
4243
final AtomicBoolean onCompleteCalled = new AtomicBoolean(false);
43-
final Ref<Predicate<Try<? extends String>>> comp1 = new Ref<>();
44-
final Future<String> future1 = Future.join(comp1::setV);
45-
final Ref<Predicate<Try<? extends String>>> comp2 = new Ref<>();
46-
final Future<String> future2 = Future.join(comp2::setV);
44+
final AtomicReference<Predicate<Try<? extends String>>> comp1 = new AtomicReference<>();
45+
final Future<String> future1 = Future.join(comp1::set);
46+
final AtomicReference<Predicate<Try<? extends String>>> comp2 = new AtomicReference<>();
47+
final Future<String> future2 = Future.join(comp2::set);
4748
Future.sequence(List.of(future1, future2))
4849
.onComplete(results -> {
4950
onCompleteCalled.set(true);
5051
assertEquals(2, results.get().size());
5152
});
52-
comp1.getV().test(Try.success("success1"));
53-
comp2.getV().test(Try.success("success2"));
53+
comp1.get().test(Try.success("success1"));
54+
comp2.get().test(Try.success("success2"));
5455
assertTrue("onComplete handler should have been called", onCompleteCalled.get());
5556
}
5657
}

src/test/java/client/Ref.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)