All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.talk2object.common.KeyIndexedArrayList Maven / Gradle / Ivy

package com.talk2object.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create an index for the key.
 * 
 * @author jack
 * 
 * @param 
 * @param 
 */
public class KeyIndexedArrayList extends ArrayList>
		implements List> {
	private Map> map = new HashMap>();

	@Override
	public boolean add(MapItem e) {
		map.put(e.getKey(), e);

		return super.add(e);
	}

	@Override
	public void add(int index, MapItem element) {
		map.put(element.getKey(), element);

		super.add(index, element);
	}

	@Override
	public boolean addAll(Collection> c) {
		for (MapItem mi : c)
			map.put(mi.getKey(), mi);

		return super.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection> c) {
		for (MapItem mi : c)
			map.put(mi.getKey(), mi);

		return addAll(index, c);
	}

	@Override
	public void clear() {
		map.clear();
	}

	@Override
	public boolean remove(Object o) {
		MapItem mi = (MapItem) o;
		map.remove(mi.getKey());

		return super.remove(o);
	}

	@Override
	public MapItem remove(int index) {
		MapItem mi = get(index);

		map.remove(mi.getKey());

		return super.remove(index);
	}

	@Override
	public boolean removeAll(Collection c) {
		for (Object item : c) {
			MapItem mi = (MapItem) item;
			map.put(mi.getKey(), mi);
		}
		return super.removeAll(c);
	}

	@Override
	public MapItem set(int index, MapItem element) {
		map.put(element.getKey(), element);

		return super.set(index, element);
	}

	public int indexOfKey(K key) {
		MapItem mi = map.get(key);
		int index = indexOf(mi);
		return index;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy