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

com.liferay.portal.kernel.util.TranslatedList Maven / Gradle / Ivy

/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.util;

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

/**
 * @author Brian Wing Shun Chan
 */
public abstract class TranslatedList extends ListWrapper {

	public TranslatedList(List newList, List oldList) {
		super(newList);

		_oldList = oldList;
	}

	@Override
	public boolean add(E o) {
		_oldList.add(toOldObject(o));

		return super.add(o);
	}

	@Override
	public void add(int index, E element) {
		_oldList.add(index, toOldObject(element));

		super.add(index, element);
	}

	@Override
	public boolean addAll(Collection c) {
		for (E o : c) {
			_oldList.add(toOldObject(o));
		}

		return super.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection c) {
		for (E o : c) {
			_oldList.add(index++, toOldObject(o));
		}

		return super.addAll(c);
	}

	@Override
	public E remove(int index) {
		_oldList.remove(index);

		return super.remove(index);
	}

	@Override
	public boolean remove(Object o) {
		_oldList.remove(toOldObject((E)o));

		return super.remove(o);
	}

	@Override
	public boolean removeAll(Collection c) {
		List tempList = new ArrayList();

		for (Object o : c) {
			tempList.add(toOldObject((E)o));
		}

		_oldList.removeAll(tempList);

		return super.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection c) {
		List tempList = new ArrayList();

		for (Object o : c) {
			tempList.add(toOldObject((E)o));
		}

		_oldList.retainAll(tempList);

		return super.retainAll(c);
	}

	@Override
	public E set(int index, E element) {
		_oldList.set(index, toOldObject(element));

		return super.set(index, element);
	}

	@Override
	public List subList(int fromIndex, int toIndex) {
		List newList = super.subList(fromIndex, toIndex);
		List oldList = _oldList.subList(fromIndex, toIndex);

		return newInstance(newList, oldList);
	}

	protected abstract TranslatedList newInstance(
		List newList, List oldList);

	protected abstract F toOldObject(E o);

	private List _oldList;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy