forked from google/thread-weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueList.java
More file actions
42 lines (36 loc) · 1.12 KB
/
UniqueList.java
File metadata and controls
42 lines (36 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Copyright 2009 Weaver authors
*
* This code is part of the Weaver tutorial and may be freely used.
*/
import java.util.ArrayList;
/**
* Implementation of {@link java.util.ArrayList} that allows
* an element to be added only if it is not already in the list.
* <p>
* This class is designed to demonstrate the testing of race conditions,
* and is not intended to be complete or correct.
*
* @param <E> the list element
*
* @author alasdair.mackintosh@gmail.com (Alasdair Mackintosh)
*/
public class UniqueList<E> extends ArrayList<E> {
/**
* Adds an element iff it is not already present in this list. Returns true
* if the element was added, and false if it was already found.
*/
public boolean putIfAbsent(E elem) {
return putIfAbsentInternal(elem);
}
// The actual method that we want to test is a private one. To make this work, we
// specify the name of this method in the test setup. See
// UniqueListTest.testPutIfAbsent()
private boolean putIfAbsentInternal(E elem) {
boolean absent = !super.contains(elem);
if (absent) {
super.add(elem);
}
return absent;
}
}