L2SyncList.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 com.l2jserver.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. @Override
  57. public boolean equals(Object o) {
  58. _rl.lock();
  59. try {
  60. return _list.equals(o);
  61. } finally {
  62. _rl.unlock();
  63. }
  64. }
  65. @Override
  66. public int hashCode() {
  67. _rl.lock();
  68. try {
  69. return _list.hashCode();
  70. } finally {
  71. _rl.unlock();
  72. }
  73. }
  74. public T set(int index, T element) {
  75. _wl.lock();
  76. try {
  77. return _list.set(index, element);
  78. } finally {
  79. _wl.unlock();
  80. }
  81. }
  82. public void add(int index, T element) {
  83. _wl.lock();
  84. try {
  85. _list.add(index, element);
  86. } finally {
  87. _wl.unlock();
  88. }
  89. }
  90. public boolean add(T element) {
  91. _wl.lock();
  92. try {
  93. return _list.add(element);
  94. } finally {
  95. _wl.unlock();
  96. }
  97. }
  98. public T remove(int index) {
  99. _wl.lock();
  100. try {
  101. return _list.remove(index);
  102. } finally {
  103. _wl.unlock();
  104. }
  105. }
  106. public boolean remove(Object value) {
  107. _wl.lock();
  108. try {
  109. return _list.remove(value);
  110. } finally {
  111. _wl.unlock();
  112. }
  113. }
  114. public boolean removeAll(Collection<?> list) {
  115. _wl.lock();
  116. try {
  117. return _list.removeAll(list);
  118. } finally {
  119. _wl.unlock();
  120. }
  121. }
  122. public boolean retainAll(Collection<?> list) {
  123. _wl.lock();
  124. try {
  125. return _list.retainAll(list);
  126. } finally {
  127. _wl.unlock();
  128. }
  129. }
  130. public int indexOf(Object o) {
  131. _rl.lock();
  132. try {
  133. return _list.indexOf(o);
  134. } finally {
  135. _rl.unlock();
  136. }
  137. }
  138. public boolean contains(Object o) {
  139. _rl.lock();
  140. try {
  141. return _list.contains(o);
  142. } finally {
  143. _rl.unlock();
  144. }
  145. }
  146. public boolean containsAll(Collection<?> list) {
  147. _rl.lock();
  148. try {
  149. return _list.containsAll(list);
  150. } finally {
  151. _rl.unlock();
  152. }
  153. }
  154. public int lastIndexOf(Object o) {
  155. _rl.lock();
  156. try {
  157. return _list.lastIndexOf(o);
  158. } finally {
  159. _rl.unlock();
  160. }
  161. }
  162. public boolean addAll(Collection<? extends T> list) {
  163. _wl.lock();
  164. try {
  165. return _list.addAll(list);
  166. } finally {
  167. _wl.unlock();
  168. }
  169. }
  170. public boolean addAll(int index, Collection<? extends T> c) {
  171. _wl.lock();
  172. try {
  173. return _list.addAll(index, c);
  174. } finally {
  175. _wl.unlock();
  176. }
  177. }
  178. public List<T> subList(int fromIndex, int toIndex) {
  179. _rl.lock();
  180. try {
  181. return new L2SyncList<T>(_list.subList(fromIndex, toIndex));
  182. } finally {
  183. _rl.unlock();
  184. }
  185. }
  186. public void clear() {
  187. _wl.lock();
  188. try {
  189. _list.clear();
  190. } finally {
  191. _wl.unlock();
  192. }
  193. }
  194. public int size() {
  195. _rl.lock();
  196. try {
  197. return _list.size();
  198. } finally {
  199. _rl.unlock();
  200. }
  201. }
  202. public boolean isEmpty() {
  203. _rl.lock();
  204. try {
  205. return _list.isEmpty();
  206. } finally {
  207. _rl.unlock();
  208. }
  209. }
  210. /**
  211. * <FONT color="#FF0000">WARNING: Unsupported</FONT>
  212. */
  213. public ListIterator<T> listIterator() {
  214. throw new UnsupportedOperationException();
  215. }
  216. /**
  217. * <FONT color="#FF0000">WARNING: Unsupported</FONT>
  218. * @see java.util.List#listIterator(int)
  219. */
  220. public ListIterator<T> listIterator(int index) {
  221. throw new UnsupportedOperationException();
  222. }
  223. /**
  224. * <FONT color="#FF0000">WARNING: Returned iterator use cloned List</FONT>
  225. * @see java.util.List#iterator()
  226. */
  227. @SuppressWarnings("unchecked")
  228. public Iterator<T> iterator() {
  229. return new Itr((T[])_list.toArray());
  230. }
  231. private class Itr implements Iterator<T> {
  232. int cursor; // index of next element to return
  233. int lastRet = -1; // index of last element returned; -1 if no such
  234. int size;
  235. T[] elementData;
  236. public Itr(T[] data) {
  237. elementData = data;
  238. if (data != null) size = data.length; else size = 0;
  239. }
  240. public boolean hasNext() {
  241. return cursor != size;
  242. }
  243. public T next() {
  244. int i = cursor;
  245. if (i >= size)
  246. throw new NoSuchElementException();
  247. cursor = i + 1;
  248. lastRet = i;
  249. return elementData[lastRet];
  250. }
  251. public void remove() {
  252. throw new UnsupportedOperationException();
  253. }
  254. }
  255. public Object[] toArray() {
  256. _rl.lock();
  257. try {
  258. return _list.toArray();
  259. } finally {
  260. _rl.unlock();
  261. }
  262. }
  263. @SuppressWarnings("hiding")
  264. public <T> T[] toArray(T[] a) {
  265. _rl.lock();
  266. try {
  267. return _list.toArray(a);
  268. } finally {
  269. _rl.unlock();
  270. }
  271. }
  272. }