L2SyncList.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.util;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.ListIterator;
  21. import java.util.NoSuchElementException;
  22. import java.util.concurrent.locks.ReentrantReadWriteLock;
  23. import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
  24. import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
  25. /**
  26. * Highly concurrent List wrapping class, thread-safe<br>
  27. * The default constructor sets a wrapped class to ArrayList<T> to minimize code line
  28. * when wrapped class type is not that importent.<br>
  29. * <br>
  30. * Note: Iterator returned does not support element removal!
  31. * @author Julian
  32. */
  33. public class L2SyncList<T extends Object> implements List<T>
  34. {
  35. private final List<T> _list;
  36. private final ReentrantReadWriteLock _rw = new ReentrantReadWriteLock();
  37. private final ReadLock _rl = _rw.readLock();
  38. private final WriteLock _wl = _rw.writeLock();
  39. /**
  40. * Default constructor use ArrayList as it internal
  41. */
  42. public L2SyncList() {
  43. _list = new ArrayList<T>();
  44. }
  45. public L2SyncList(List<T> list) {
  46. _list = list;
  47. }
  48. public T get(int index) {
  49. _rl.lock();
  50. try {
  51. return _list.get(index);
  52. } finally {
  53. _rl.unlock();
  54. }
  55. }
  56. public boolean equals(Object o) {
  57. _rl.lock();
  58. try {
  59. return _list.equals(o);
  60. } finally {
  61. _rl.unlock();
  62. }
  63. }
  64. public int hashCode() {
  65. _rl.lock();
  66. try {
  67. return _list.hashCode();
  68. } finally {
  69. _rl.unlock();
  70. }
  71. }
  72. public T set(int index, T element) {
  73. _wl.lock();
  74. try {
  75. return _list.set(index, element);
  76. } finally {
  77. _wl.unlock();
  78. }
  79. }
  80. public void add(int index, T element) {
  81. _wl.lock();
  82. try {
  83. _list.add(index, element);
  84. } finally {
  85. _wl.unlock();
  86. }
  87. }
  88. public boolean add(T element) {
  89. _wl.lock();
  90. try {
  91. return _list.add(element);
  92. } finally {
  93. _wl.unlock();
  94. }
  95. }
  96. public T remove(int index) {
  97. _wl.lock();
  98. try {
  99. return _list.remove(index);
  100. } finally {
  101. _wl.unlock();
  102. }
  103. }
  104. public boolean remove(Object value) {
  105. _wl.lock();
  106. try {
  107. return _list.remove(value);
  108. } finally {
  109. _wl.unlock();
  110. }
  111. }
  112. public boolean removeAll(Collection<?> list) {
  113. _wl.lock();
  114. try {
  115. return _list.removeAll(list);
  116. } finally {
  117. _wl.unlock();
  118. }
  119. }
  120. public boolean retainAll(Collection<?> list) {
  121. _wl.lock();
  122. try {
  123. return _list.retainAll(list);
  124. } finally {
  125. _wl.unlock();
  126. }
  127. }
  128. public int indexOf(Object o) {
  129. _rl.lock();
  130. try {
  131. return _list.indexOf(o);
  132. } finally {
  133. _rl.unlock();
  134. }
  135. }
  136. public boolean contains(Object o) {
  137. _rl.lock();
  138. try {
  139. return _list.contains(o);
  140. } finally {
  141. _rl.unlock();
  142. }
  143. }
  144. public boolean containsAll(Collection<?> list) {
  145. _rl.lock();
  146. try {
  147. return _list.containsAll(list);
  148. } finally {
  149. _rl.unlock();
  150. }
  151. }
  152. public int lastIndexOf(Object o) {
  153. _rl.lock();
  154. try {
  155. return _list.lastIndexOf(o);
  156. } finally {
  157. _rl.unlock();
  158. }
  159. }
  160. public boolean addAll(Collection<? extends T> list) {
  161. _wl.lock();
  162. try {
  163. return _list.addAll(list);
  164. } finally {
  165. _wl.unlock();
  166. }
  167. }
  168. public boolean addAll(int index, Collection<? extends T> c) {
  169. _wl.lock();
  170. try {
  171. return _list.addAll(index, c);
  172. } finally {
  173. _wl.unlock();
  174. }
  175. }
  176. public List<T> subList(int fromIndex, int toIndex) {
  177. _rl.lock();
  178. try {
  179. return new L2SyncList<T>(_list.subList(fromIndex, toIndex));
  180. } finally {
  181. _rl.unlock();
  182. }
  183. }
  184. public void clear() {
  185. _wl.lock();
  186. try {
  187. _list.clear();
  188. } finally {
  189. _wl.unlock();
  190. }
  191. }
  192. public int size() {
  193. _rl.lock();
  194. try {
  195. return _list.size();
  196. } finally {
  197. _rl.unlock();
  198. }
  199. }
  200. public boolean isEmpty() {
  201. _rl.lock();
  202. try {
  203. return _list.isEmpty();
  204. } finally {
  205. _rl.unlock();
  206. }
  207. }
  208. /**
  209. * <FONT color="#FF0000">WARNING: Unsupported</FONT>
  210. */
  211. public ListIterator<T> listIterator() {
  212. throw new UnsupportedOperationException();
  213. }
  214. /**
  215. * <FONT color="#FF0000">WARNING: Unsupported</FONT>
  216. * @see java.util.List#listIterator(int)
  217. */
  218. public ListIterator<T> listIterator(int index) {
  219. throw new UnsupportedOperationException();
  220. }
  221. /**
  222. * <FONT color="#FF0000">WARNING: Returned iterator use cloned List</FONT>
  223. * @see java.util.List#iterator()
  224. */
  225. @SuppressWarnings("unchecked")
  226. public Iterator<T> iterator() {
  227. return new Itr((T[])_list.toArray());
  228. }
  229. private class Itr implements Iterator<T> {
  230. int cursor; // index of next element to return
  231. int lastRet = -1; // index of last element returned; -1 if no such
  232. int size;
  233. T[] elementData;
  234. public Itr(T[] data) {
  235. elementData = data;
  236. if (data != null) size = data.length; else size = 0;
  237. }
  238. public boolean hasNext() {
  239. return cursor != size;
  240. }
  241. public T next() {
  242. int i = cursor;
  243. if (i >= size)
  244. throw new NoSuchElementException();
  245. cursor = i + 1;
  246. lastRet = i;
  247. return elementData[lastRet];
  248. }
  249. public void remove() {
  250. throw new UnsupportedOperationException();
  251. }
  252. }
  253. public Object[] toArray() {
  254. _rl.lock();
  255. try {
  256. return _list.toArray();
  257. } finally {
  258. _rl.unlock();
  259. }
  260. }
  261. @SuppressWarnings("hiding")
  262. public <T> T[] toArray(T[] a) {
  263. _rl.lock();
  264. try {
  265. return _list.toArray(a);
  266. } finally {
  267. _rl.unlock();
  268. }
  269. }
  270. }