/*
* Copyright (c) 2009, Hamish Morgan. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the University of Sussex nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package locusts.common.entities;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
/**
*
* @author Hamish Morgan
*/
public class EntityDataList implements Map<EntityType, EntityData>, Cloneable,
Serializable, Iterable<EntityData> {
private static final int MAX_TYPE_ID = 100;
private CopyOnWriteArrayList<EntityData> data;
protected EntityDataList(CopyOnWriteArrayList<EntityData> data) {
this.data = data;
}
public EntityDataList() {
this(new CopyOnWriteArrayList<EntityData>());
}
public EntityDataList(EntityDataList that) {
this();
data.addAll(that.data);
}
private final void growTypesList(int id) {
if (id < 0 || id > MAX_TYPE_ID)
throw new IllegalArgumentException(
"Argument id must be between 0 and " + MAX_TYPE_ID);
while (data.size() <= id)
data.add(null);
}
public int size() {
return data.size();
}
public boolean isEmpty() {
return data.isEmpty();
}
public boolean containsKey(Object key) {
if (!(key instanceof EntityType))
return false;
return containsKey(((EntityType) key));
}
public boolean containsKey(EntityType key) {
return containsKey(key.getId());
}
public boolean containsKey(int id) {
return data.size() > id && data.get(id) != null;
}
public boolean containsValue(Object value) {
return containsValue((EntityData) value);
}
public boolean containsValue(EntityData value) {
return data.contains(value);
}
public EntityData get(Object key) {
return (key instanceof EntityType) ? get((EntityType) key) : null;
}
public EntityData get(EntityType key) {
return get(key.getId());
}
public EntityData get(Entity e) {
return get(e.getTypeId());
}
public EntityData get(int id) {
return id < data.size() ? data.get(id) : null;
}
public EntityData put(EntityType key, EntityData value) {
return put(key.getId(), value);
}
public EntityData put(int id, EntityData value) {
growTypesList(id);
return data.set(id, value);
}
public EntityData remove(Object key) {
return (key instanceof EntityType) ? remove((EntityType) key) : null;
}
public EntityData remove(EntityType key) {
return remove(key.getId());
}
public EntityData remove(int id) {
return id < data.size() ? data.remove(id) : null;
}
public void putAll(Map<? extends EntityType, ? extends EntityData> t) {
for(EntityType et : t.keySet())
put(et, t.get(et));
}
public void clear() {
data.clear();
}
public Set<EntityType> keySet() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Collection<EntityData> values() {
return data;
}
public Set<Entry<EntityType, EntityData>> entrySet() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Iterator<EntityData> iterator() {
return data.iterator();
}
@Override
public EntityDataList clone() {
return new EntityDataList(this);
}
}
|