| 1 |
import org.checkerframework.checker.initialization.qual.NotOnlyInitialized; |
| 2 |
import org.checkerframework.checker.initialization.qual.UnderInitialization; |
| 3 |
import org.checkerframework.checker.nullness.qual.Nullable; |
| 4 |
|
| 5 |
// This example is taken from the FBC paper, figure 1 (and has some additional code in main below). |
| 6 |
// We made the list generic. |
| 7 |
public class List<T> { |
| 8 |
@NotOnlyInitialized Node<T> sentinel; |
| 9 |
|
| 10 |
public List() { |
| 11 |
this.sentinel = new Node<>(this); |
| 12 |
} |
| 13 |
|
| 14 |
void insert(@Nullable T data) { |
| 15 |
this.sentinel.insertAfter(data); |
| 16 |
} |
| 17 |
|
| 18 |
public static void main() { |
| 19 |
List<Integer> l = new List<>(); |
| 20 |
l.insert(1); |
| 21 |
l.insert(2); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
class Node<T> { |
| 26 |
@NotOnlyInitialized Node<T> prev; |
| 27 |
|
| 28 |
@NotOnlyInitialized Node<T> next; |
| 29 |
|
| 30 |
@NotOnlyInitialized List parent; |
| 31 |
|
| 32 |
@Nullable T data; |
| 33 |
|
| 34 |
// for sentinel construction |
| 35 |
Node(@UnderInitialization List parent) { |
| 36 |
this.parent = parent; |
| 37 |
this.prev = this; |
| 38 |
this.next = this; |
| 39 |
} |
| 40 |
|
| 41 |
// for data node construction |
| 42 |
Node(Node<T> prev, Node<T> next, @Nullable T data) { |
| 43 |
this.parent = prev.parent; |
| 44 |
this.prev = prev; |
| 45 |
this.next = next; |
| 46 |
this.data = data; |
| 47 |
} |
| 48 |
|
| 49 |
void insertAfter(@Nullable T data) { |
| 50 |
Node<T> n = new Node<>(this, this.next, data); |
| 51 |
this.next.prev = n; |
| 52 |
this.next = n; |
| 53 |
} |
| 54 |
} |