EOS 2  1.1.0
Einfache Objektbasierte Sprache
ConcurrentLinkedList.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.base.util;
2 
3 import java.util.Iterator;
4 
26 public class ConcurrentLinkedList<T> implements Iterable<T> {
27 
28  private Node<T> root;
29  private Node<T> last;
30  private int count;
32  this.root = null;
33  this.last = null;
34  count = 0;
35  }
36 
37  public synchronized void add(T t) {
38  if (last == null) {
39  root = new Node<T>(t);
40  last = root;
41  } else {
42  last.next = new Node<T>(t);
43  last = last.next;
44  }
45  count++;
46  }
47  public synchronized void addFront(T t) {
48  if (root == null) {
49  root = new Node<T>(t);
50  last = root;
51  } else {
52  root = new Node<T>(t, root.next);
53  }
54  }
55  public synchronized void remove(T t) {
56  if (root == null) {
57  return;
58  } else if (root.t == t) {
59  if (root == last) {
60  last = null;
61  root = null;
62  } else {
63  root = root.next;
64  }
65  } else {
66  Node<T> prev = root;
67  Node<T> act = root.next;
68  while (act != null) {
69  if (act.t == t) {
70  prev.next = act.next;
71  if (act == last) {
72  last = prev;
73  }
74  count--;
75  return;
76  }
77  prev = act;
78  act = act.next;
79  }
80  }
81  count--;
82  }
83  public int getLength() {
84  return count;
85  }
86  @Override
87  public Iterator<T> iterator() {
88  return new TIterator<T>(root);
89  }
90 
91  public synchronized void clear() {
92  root = null;
93  last = null;
94  }
95 
96  private static class Node<T> {
97  private volatile Node<T> next;
98  private T t;
99 
100  public Node(T t) {
101  this.t = t;
102  }
103  public Node(T t, Node<T> next) {
104  this.t= t;
105  this.next = next;
106  }
107  }
108 
109  private static class TIterator<T> implements Iterator<T> {
110  private Node<T> index;
111 
112  public TIterator(Node<T> index) {
113  this.index = index;
114  }
115 
116  @Override
117  public boolean hasNext() {
118  return index != null;
119  }
120 
121  @Override
122  public T next() {
123  T next = index.t;
124  index = index.next;
125  return next;
126  }
127  }
128 }
Impressum