Forráskód Böngészése

Updating L2J custom collections and added L2SyncList as well

Julian 17 éve
szülő
commit
244fe95bea

+ 11 - 12
L2_GameServer/java/net/sf/l2j/util/L2FastList.java

@@ -14,7 +14,8 @@
  */
 package net.sf.l2j.util;
 
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
 *
@@ -26,7 +27,7 @@ import java.util.LinkedList;
 *      1.0.0 - Initial version.<br>
 *      1.0.1 - Made forEachP() final.<br>
 */
-public class L2FastList<T extends Object> extends LinkedList<T>
+public class L2FastList<T extends Object> extends ArrayList<T>
 {
 	static final long serialVersionUID = 1L;
 	
@@ -39,23 +40,21 @@ public class L2FastList<T extends Object> extends LinkedList<T>
 		public boolean ForEach(T obj);
 	}
 	
+	public L2FastList() {
+		super();
+	}
+	
+	public L2FastList(List<? extends T> list) {
+		super(list);
+	}
      /**
       * Public method that iterate entire collection.<br>
       * <br>
       * @param func - a class method that must be executed on every element of collection.<br>
-      * @param sync - if set to true, will lock entire collection.<br>
       * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
       *             check method (I2ForEach.forEach())<br>
       */
-	public final boolean forEach(I2ForEach<T> func, boolean sync) {
-		if (sync)
-			synchronized(this) { return forEachP(func); }
-		else
-			return forEachP(func);
-	}
-	
-    // private method that implements forEach iteration
-	private final boolean forEachP(I2ForEach<T> func) {
+	public boolean forEach(I2ForEach<T> func) {
 		for (T e: this)
 			if (!func.ForEach(e)) return false;
 		return true;

+ 21 - 11
L2_GameServer/java/net/sf/l2j/util/L2FastMap.java

@@ -20,7 +20,6 @@ import java.util.Map;
 /**
  *
  * A custom version of HashMap with extension for iterating without using temporary collection<br>
- * It`s provide synchronization lock when iterating if needed<br>
  * <br>
  * @author  Julian Version 1.0.1 (2008-02-07)<br>
  * Changes:<br>
@@ -41,25 +40,36 @@ public class L2FastMap<K extends Object, V extends Object> extends HashMap<K,V>
 		public boolean forEach(K key, V val);
 	}
 
+	public interface I2ForEachKey<K> {
+		public boolean forEach(K key);
+	}
+
+	public interface I2ForEachValue<V> {
+		public boolean forEach(V val);
+	}
+
 	/**
 	 * Public method that iterate entire collection.<br>
 	 * <br>
 	 * @param func - a class method that must be executed on every element of collection.<br>
-	 * @param sync - if set to true, will lock entire collection.<br>
 	 * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
 	 *             check method (I2ForEach.forEach())<br>
 	 */
-	public final boolean ForEach(I2ForEach<K,V> func, boolean sync) {
-		if (sync)
-			synchronized (this) { return forEachP(func); }
-		else
-			return forEachP(func);
-	}
-	
-	// private method that implements forEach iteration
-	private final boolean forEachP(I2ForEach<K,V> func) {
+	public boolean ForEach(I2ForEach<K,V> func) {
 		for (Map.Entry<K,V> e: this.entrySet())
 			if (!func.forEach(e.getKey(),e.getValue())) return false;
 		return true;
 	}
+
+	public boolean ForEachKey(I2ForEachKey<K> func) {
+		for (K k: this.keySet())
+			if (!func.forEach(k)) return false;
+		return true;
+	}
+
+	public boolean ForEachValue(I2ForEachValue<V> func) {
+		for (V v: this.values())
+			if (!func.forEach(v)) return false;
+		return true;
+	}
 }

+ 162 - 0
L2_GameServer/java/net/sf/l2j/util/L2SyncList.java

@@ -0,0 +1,162 @@
+/*
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ * 
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package net.sf.l2j.util;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import net.sf.l2j.util.L2FastList.I2ForEach;
+
+/**
+ *
+ * @author  Julian
+ */
+public class L2SyncList<T extends Object> implements List<T> 
+{
+	private final L2FastList<T> _list;
+	
+	public L2SyncList() {
+		_list = new L2FastList<T>();
+	}
+
+	public L2SyncList(List<? extends T> list) {
+		_list = new L2FastList<T>(list);
+	}
+	
+	public synchronized T get(int index) {
+		return _list.get(index);
+	}
+	
+    public synchronized boolean equals(Object o) {
+        return _list.equals(o);
+    }
+    public synchronized int hashCode() {
+        return _list.hashCode();
+    }
+
+    public synchronized T set(int index, T element) {
+        return _list.set(index, element);
+    }
+    
+    public synchronized void add(int index, T element) {
+        _list.add(index, element);
+    }
+
+    public synchronized boolean add(T element) {
+    	return _list.add(element);
+    }
+    
+    public synchronized T remove(int index) {
+        return _list.remove(index);
+    }
+
+    public synchronized boolean remove(Object value) {
+    	return _list.remove(value);
+    }
+    
+    public synchronized boolean removeAll(Collection<?> list) {
+    	return _list.removeAll(list);
+    }
+
+    public synchronized boolean retainAll(Collection<?> list) {
+    	return _list.retainAll(list);
+    }
+    public synchronized int indexOf(Object o) {
+        return _list.indexOf(o);
+    }
+
+    public synchronized boolean contains(Object o) {
+    	return _list.contains(o);
+    }
+    
+    public synchronized boolean containsAll(Collection<?> list) {
+    	return _list.containsAll(list);
+    }
+    
+    public synchronized int lastIndexOf(Object o) {
+        return _list.lastIndexOf(o);
+    }
+
+    public synchronized boolean addAll(Collection<? extends T> list) {
+    	return _list.addAll(list);
+    }
+    
+    public synchronized boolean addAll(int index, Collection<? extends T> c) {
+        return _list.addAll(index, c);
+    }
+    
+    public synchronized List<T> subList(int fromIndex, int toIndex) {
+        return new L2SyncList<T>(_list.subList(fromIndex, toIndex));
+    }
+
+    public synchronized void clear() {
+    	_list.clear();
+    }
+
+    public synchronized int size() {
+    	return _list.size();
+    }
+    
+    public synchronized boolean isEmpty() {
+    	return _list.isEmpty();
+    }
+
+    public synchronized boolean forEach(I2ForEach<T> func) {
+    	return _list.forEach(func);
+    }
+    
+    /**
+     * @deprecated
+     * @see java.util.List#listIterator()
+     */
+    public ListIterator<T> listIterator() {
+    	throw new UnsupportedOperationException();
+    }
+
+    /**
+     * @deprecated
+     * @see java.util.List#listIterator(int)
+     */
+    public ListIterator<T> listIterator(int index) {
+    	throw new UnsupportedOperationException();
+    }
+
+    /**
+     * @deprecated
+     * @see java.util.List#iterator()
+     */
+    public Iterator<T> iterator() {
+    	throw new UnsupportedOperationException();
+    }
+    
+    /**
+     * @deprecated
+     * @see java.util.List#toArray()
+     */
+	public synchronized Object[] toArray() {
+		throw new UnsupportedOperationException();
+	}
+	
+	/**
+	 * @deprecated
+	 * @see java.util.List#toArray(T[])
+	 */
+	@SuppressWarnings("hiding")
+	public synchronized <T> T[] toArray(T[] a) {
+		throw new UnsupportedOperationException();
+	}
+}

+ 113 - 24
L2_GameServer/java/net/sf/l2j/util/L2SyncMap.java

@@ -17,50 +17,139 @@
  */
 package net.sf.l2j.util;
 
+import java.util.Collection;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.Set;
+
+import net.sf.l2j.util.L2FastMap.I2ForEach;
+import net.sf.l2j.util.L2FastMap.I2ForEachKey;
+import net.sf.l2j.util.L2FastMap.I2ForEachValue;
 
 /**
  *
- * Fully synchronized version of HashMap class.<br>
- * In addition it`s provide ForEach method and interface that can be used for iterating collection<br>
- *  without using temporary collections. As addition its provide full lock on entire class if needed<br>
- *  (by default {@link ConcurrentHashMap} does not provide this functionality)<br>
+ * Fully synchronized version of L2FastMap class.<br>
+ * In addition it`s provide ForEach methods and interfaces that can be used for iterating collection<br>
+ * without using iterators. As addition its provide full lock on entire class if needed<br>
+ * <font color="red">WARNING!!! methods: keySet(), values() and entrySet() are removed!</font>
  * <br>
- * @author  Julian Version: 1.0.0 <2008-02-07>
+ * @author  Julian Version: 1.0.0 <2008-02-07> - Original release
+ * @author  Julian Varsion: 1.0.1 <2008-06-17> - Changed underlayng map to L2FastMap
  */
-public class L2SyncMap<K extends Object, V extends Object> extends ConcurrentHashMap<K, V>
+public class L2SyncMap<K extends Object, V extends Object> implements Map<K, V>
 {
     static final long serialVersionUID = 1L;
+    private final L2FastMap<K, V> _map = new L2FastMap<K, V>();
+    
+    public synchronized V put(K key, V value) {
+    	return _map.put(key, value);
+    }
+    
+    public synchronized V get(Object key) {
+        return _map.get(key);
+    }
+    
+    public synchronized V remove(Object key) {
+        return _map.remove(key);
+    }
+    
+    public synchronized boolean containsKey(Object key) {
+    	return _map.containsKey(key);
+    }
+    
+    public synchronized int size() {
+    	return _map.size();
+    }
+
+    public synchronized boolean isEmpty() {
+        return _map.isEmpty();
+    }
+    
+    public synchronized void clear() {
+        _map.clear();
+    }
     
     /**
-     * Public inner interface used by ForEach iterations<br>
-     *
-     * @author  Julian
+     * This method use specific locking strategy: map which have lowest hashCode() will be locked first
+     * @see java.util.Map#putAll(java.util.Map)
      */
-    public interface I2ForEach<K,V> {
-        public boolean forEach(K key, V val);
+    public void putAll(Map<? extends K, ? extends V> map) {
+    	if (map == null || this == map) return;
+    	if (this.hashCode() <= map.hashCode())
+    		synchronized (this) {
+    			synchronized (map) {
+    				_map.putAll(map);
+    			}
+    		}
+    	else {
+    		synchronized (map) {
+    			synchronized(this) {
+    				_map.putAll(map);
+    			}
+    		}
+    	}
+    }
+    
+    public synchronized boolean containsValue(Object value) {
+        return _map.containsValue(value);
     }
 
+    public synchronized boolean equals(Object o) {
+        return _map.equals(o);
+    }
+    
+    public synchronized int hashCode() {
+        return _map.hashCode();
+    }
+    
+    public synchronized String toString() {
+        return _map.toString();
+    }
+    
     /**
      * Public method that iterate entire collection.<br>
      * <br>
      * @param func - a class method that must be executed on every element of collection.<br>
-     * @param sync - if set to true, will lock entire collection.<br>
-     * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
+     * @return - returns true if entire collection is iterated, false if it`s been interrupted by
      *             check method (I2ForEach.forEach())<br>
      */
-    public final boolean ForEach(I2ForEach<K,V> func, boolean sync) {
-        if (sync)
-            synchronized (this) { return forEachP(func); }
-        else
-            return forEachP(func);
+    public synchronized final boolean ForEach(I2ForEach<K,V> func) {
+    	return _map.ForEach(func);
+    }
+    
+    public synchronized final boolean ForEachValue(I2ForEachValue<V> func) {
+    	return _map.ForEachValue(func);
+    }
+
+    public synchronized final boolean ForEachKey(I2ForEachKey<K> func) {
+    	return _map.ForEachKey(func);
+    }
+
+    /**
+     * <font color="red">Unsupported operation!!!</font>
+     * @deprecated
+     * @throws UnsupportedOperationException
+     * @see java.util.Map#values()
+     */
+    public Collection<V> values() {
+    	throw new UnsupportedOperationException();
     }
     
-    // private method that implements forEach iteration
-    private final boolean forEachP(I2ForEach<K,V> func) {
-        for (Map.Entry<K,V> e: this.entrySet())
-            if (!func.forEach(e.getKey(),e.getValue())) return false;
-        return true;
+    /**
+     * <font color="red">Unsupported operation!!!</font>
+     * @deprecated
+     * @throws UnsupportedOperationException
+     */
+    public Set<K> keySet() {
+    	throw new UnsupportedOperationException();
+    }
+    
+    /**
+     * <font color="red">Unsupported operation!!!</font>
+     * @deprecated
+     * @throws UnsupportedOperationException
+     * @see java.util.Map#entrySet()
+     */
+    public Set<Map.Entry<K,V>> entrySet() {
+    	throw new UnsupportedOperationException();
     }
 }