43 |
|
volatile int anIntField; |
44 |
|
volatile Integer anIntegerField; |
45 |
|
|
46 |
< |
AtomicLongFieldUpdater aLongFieldUpdater() { |
46 |
> |
AtomicLongFieldUpdater<Atomic8Test> aLongFieldUpdater() { |
47 |
|
return AtomicLongFieldUpdater.newUpdater |
48 |
|
(Atomic8Test.class, "aLongField"); |
49 |
|
} |
50 |
|
|
51 |
< |
AtomicIntegerFieldUpdater anIntFieldUpdater() { |
51 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> anIntFieldUpdater() { |
52 |
|
return AtomicIntegerFieldUpdater.newUpdater |
53 |
|
(Atomic8Test.class, "anIntField"); |
54 |
|
} |
289 |
|
* result of supplied function |
290 |
|
*/ |
291 |
|
public void testReferenceArrayGetAndUpdate() { |
292 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
292 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
293 |
|
a.set(0, one); |
294 |
|
assertEquals((Integer) 1, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
295 |
|
assertEquals((Integer) 18, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
301 |
|
* returns result. |
302 |
|
*/ |
303 |
|
public void testReferenceArrayUpdateAndGet() { |
304 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
304 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
305 |
|
a.set(0, one); |
306 |
|
assertEquals((Integer) 18, a.updateAndGet(0, Atomic8Test::addInteger17)); |
307 |
|
assertEquals((Integer) 35, a.updateAndGet(0, Atomic8Test::addInteger17)); |
312 |
|
* with supplied function. |
313 |
|
*/ |
314 |
|
public void testReferenceArrayGetAndAccumulate() { |
315 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
315 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
316 |
|
a.set(0, one); |
317 |
|
assertEquals((Integer) 1, a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); |
318 |
|
assertEquals((Integer) 3, a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); |
324 |
|
* returns result. |
325 |
|
*/ |
326 |
|
public void testReferenceArrayAccumulateAndGet() { |
327 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
327 |
> |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
328 |
|
a.set(0, one); |
329 |
|
assertEquals((Integer) 7, a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); |
330 |
|
assertEquals((Integer) 10, a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); |
501 |
|
() -> aLongFieldUpdater().getAndUpdate(this, null), |
502 |
|
() -> anIntFieldUpdater().getAndUpdate(this, null), |
503 |
|
() -> anIntegerFieldUpdater().getAndUpdate(this, null), |
504 |
– |
////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17), |
505 |
– |
////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17), |
506 |
– |
////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17), |
504 |
|
}; |
505 |
|
assertThrows(NullPointerException.class, throwingActions); |
506 |
|
} |
561 |
|
assertThrows(NullPointerException.class, throwingActions); |
562 |
|
} |
563 |
|
|
564 |
+ |
/** |
565 |
+ |
* Object arguments for parameters of type T that are not |
566 |
+ |
* instances of the class passed to the newUpdater call will |
567 |
+ |
* result in a ClassCastException being thrown. |
568 |
+ |
*/ |
569 |
+ |
public void testFieldUpdaters_ClassCastException() { |
570 |
+ |
// Use raw types to allow passing wrong object type, provoking CCE |
571 |
+ |
final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); |
572 |
+ |
final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); |
573 |
+ |
final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater(); |
574 |
+ |
final Object obj = new Object(); |
575 |
+ |
for (Object x : new Object[]{ new Object(), null }) { |
576 |
+ |
Runnable[] throwingActions = { |
577 |
+ |
() -> longUpdater.get(x), |
578 |
+ |
() -> intUpdater.get(x), |
579 |
+ |
() -> refUpdater.get(x), |
580 |
+ |
|
581 |
+ |
() -> longUpdater.set(x, 17L), |
582 |
+ |
() -> intUpdater.set(x, 17), |
583 |
+ |
() -> refUpdater.set(x, (Integer) 17), |
584 |
+ |
|
585 |
+ |
() -> longUpdater.addAndGet(x, 17L), |
586 |
+ |
() -> intUpdater.addAndGet(x, 17), |
587 |
+ |
|
588 |
+ |
() -> longUpdater.getAndUpdate(x, y -> y), |
589 |
+ |
() -> intUpdater.getAndUpdate(x, y -> y), |
590 |
+ |
() -> refUpdater.getAndUpdate(x, y -> y), |
591 |
+ |
|
592 |
+ |
() -> longUpdater.compareAndSet(x, 17L, 42L), |
593 |
+ |
() -> intUpdater.compareAndSet(x, 17, 42), |
594 |
+ |
() -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42), |
595 |
+ |
}; |
596 |
+ |
assertThrows(ClassCastException.class, throwingActions); |
597 |
+ |
} |
598 |
+ |
} |
599 |
+ |
|
600 |
|
} |