aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/main/java/io/opencensus/implcore/trace/internal/ConcurrentIntrusiveList.java
blob: 22d8e41a64d0e206acac0346e6109aaeefabc7f6 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.implcore.trace.internal;

import static com.google.common.base.Preconditions.checkArgument;

import io.opencensus.implcore.internal.CheckerFrameworkUtils;
import io.opencensus.implcore.trace.internal.ConcurrentIntrusiveList.Element;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

/**
 * An {@code ConcurrentIntrusiveList<T>} is a doubly-linked list where the link pointers are
 * embedded in the elements. This makes insertion and removal into a known position constant time.
 *
 * <p>Elements must derive from the {@code Element<T extends Element<T>>} interface:
 *
 * <pre><code>
 * class MyClass implements {@code Element<MyClass>} {
 *   private MyClass next = null;
 *   private MyClass prev = null;
 *
 *  {@literal @}Override
 *   MyClass getNext() {
 *     return next;
 *   }
 *
 *  {@literal @}Override
 *   void setNext(MyClass element) {
 *     next = element;
 *   }
 *
 *  {@literal @}Override
 *   MyClass getPrev() {
 *     return prev;
 *   }
 *
 *  {@literal @}Override
 *   void setPrev(MyClass element) {
 *     prev = element;
 *   }
 * }
 * </code></pre>
 */
@ThreadSafe
public final class ConcurrentIntrusiveList<T extends Element<T>> {
  private int size = 0;
  @Nullable private T head = null;

  public ConcurrentIntrusiveList() {}

  /**
   * Adds the given {@code element} to the list.
   *
   * @param element the element to add.
   * @throws IllegalArgumentException if the element is already in a list.
   */
  public synchronized void addElement(T element) {
    checkArgument(
        element.getNext() == null && element.getPrev() == null && element != head,
        "Element already in a list.");
    size++;
    if (head == null) {
      head = element;
    } else {
      head.setPrev(element);
      element.setNext(head);
      head = element;
    }
  }

  /**
   * Removes the given {@code element} from the list.
   *
   * @param element the element to remove.
   * @throws IllegalArgumentException if the element is not in the list.
   */
  public synchronized void removeElement(T element) {
    checkArgument(
        element.getNext() != null || element.getPrev() != null || element == head,
        "Element not in the list.");
    size--;
    if (element.getPrev() == null) {
      // This is the first element
      head = element.getNext();
      if (head != null) {
        // If more than one element in the list.
        head.setPrev(null);
        element.setNext(null);
      }
    } else if (element.getNext() == null) {
      // This is the last element, and there is at least another element because
      // element.getPrev() != null.
      CheckerFrameworkUtils.castNonNull(element.getPrev()).setNext(null);
      element.setPrev(null);
    } else {
      CheckerFrameworkUtils.castNonNull(element.getPrev()).setNext(element.getNext());
      CheckerFrameworkUtils.castNonNull(element.getNext()).setPrev(element.getPrev());
      element.setNext(null);
      element.setPrev(null);
    }
  }

  /**
   * Returns the number of elements in this list.
   *
   * @return the number of elements in this list.
   */
  public synchronized int size() {
    return size;
  }

  /**
   * Returns all the elements from this list.
   *
   * @return all the elements from this list.
   */
  public synchronized Collection<T> getAll() {
    List<T> all = new ArrayList<T>(size);
    for (T e = head; e != null; e = e.getNext()) {
      all.add(e);
    }
    return all;
  }

  /**
   * This is an interface that must be implemented by any element that uses {@link
   * ConcurrentIntrusiveList}.
   *
   * @param <T> the element that will be used for the list.
   */
  public interface Element<T extends Element<T>> {

    /**
     * Returns a reference to the next element in the list.
     *
     * @return a reference to the next element in the list.
     */
    @Nullable
    T getNext();

    /**
     * Sets the reference to the next element in the list.
     *
     * @param element the reference to the next element in the list.
     */
    void setNext(@Nullable T element);

    /**
     * Returns a reference to the previous element in the list.
     *
     * @return a reference to the previous element in the list.
     */
    @Nullable
    T getPrev();

    /**
     * Sets the reference to the previous element in the list.
     *
     * @param element the reference to the previous element in the list.
     */
    void setPrev(@Nullable T element);
  }
}