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

org.zkoss.bind.proxy.ListProxy Maven / Gradle / Ivy

There is a newer version: 10.0.0-jakarta
Show newest version
/** ListProxy.java.

	Purpose:
		
	Description:
		
	History:
		2:10:51 PM Dec 25, 2014, Created by jumperchen

Copyright (C) 2014 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.bind.proxy;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * A list proxy
 * @author jumperchen
 * @since 8.0.0
 */
public class ListProxy extends AbstractCollectionProxy implements List {
	private static final long serialVersionUID = 20141225150833L;
	private Annotation[] _callerAnnots;

	public ListProxy(Collection origin, Annotation[] callerAnnots) {
		super(origin, callerAnnots);
		_callerAnnots = callerAnnots;
		resetFromOrigin();
	}

	@SuppressWarnings("unchecked")
	protected Collection initCache() {
		return new ArrayList(((Collection) getOriginObject()).size());
	}

	public boolean addAll(int index, Collection c) {
		boolean result = false;
		if (c.size() > 0) {
			List proxyList = new LinkedList<>();
			for (E e : c)
				proxyList.add(createProxyObject(e));
			result = ((List) getCache()).addAll(index, proxyList);
			setDirty(true);
		}
		return result;
	}

	public E get(int index) {
		return ((List) getCache()).get(index);
	}

	public E set(int index, E element) {
		E prevElement = ((List) getCache()).set(index, createProxyObject(element));
		setDirty(true);
		return prevElement;
	}

	public void add(int index, E element) {
		((List) getCache()).add(index, createProxyObject(element));
		setDirty(true);
	}

	public E remove(int index) {
		E removed = ((List) getCache()).remove(index);
		setDirty(true);
		return removed;
	}

	public int indexOf(Object o) {
		final int size = size();
		if (o == null) {
			for (int i = 0; i < size; i++)
				if (testEquals(get(i), null))
					return i;
		} else {
			for (int i = 0; i < size; i++)
				if (testEquals(o, get(i)))
					return i;
		}
		return -1;
	}

	public int lastIndexOf(Object o) {
		final int size = size();
		if (o == null) {
			for (int i = size - 1; i >= 0; i--)
				if (testEquals(get(i), null))
					return i;
		} else {
			for (int i = size - 1; i >= 0; i--)
				if (testEquals(o, get(i)))
					return i;
		}
		return -1;
	}

	public ListIterator listIterator() {
		return ((List) getCache()).listIterator();
	}

	public ListIterator listIterator(int index) {
		return ((List) getCache()).listIterator(index);
	}

	public List subList(int fromIndex, int toIndex) {
		return ProxyHelper.createProxyIfAny(((List) getCache()).subList(fromIndex, toIndex), _callerAnnots);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy