This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/ui/list/ComponentListModel.java

112 lines
2.9 KiB
Java

package envoy.client.ui.list;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Stores objects that will be displayed in a {@link ComponentList}.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>ComponentListModel.java</strong><br>
* Created: <strong>25.01.2020</strong><br>
*
* @param <E> the type of object displayed in this list
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public final class ComponentListModel<E> implements Iterable<E>, Serializable {
private List<E> elements = new ArrayList<>();
transient private ComponentList<E> componentList;
private static final long serialVersionUID = 4815005915255497331L;
/**
* Adds an element to this model and notifies the associated
* {@link ComponentList} to add the corresponding component.
*
* @param e the element to add
* @return {@code true}
* @see java.util.List#add(java.lang.Object)
* @since Envoy v0.3-alpha
*/
public boolean add(E e) {
if (componentList != null) componentList.add(e);
return elements.add(e);
}
/**
* Removes all elements from this model and clears the associated
* {@link ComponentList}.
*
* @see java.util.List#clear()
* @since Envoy v0.3-alpha
*/
public void clear() {
elements.clear();
if (componentList != null) componentList.removeAll();
}
/**
* @param index the index to retrieve the element from
* @return the element located at the index
* @see java.util.List#get(int)
* @since Envoy v0.3-alpha
*/
public E get(int index) { return elements.get(index); }
/**
* Removes the element at a specific index from this model and the corresponding
* component from the {@link ComponentList}.
*
* @param index the index of the element to remove
* @return the removed element
* @see java.util.List#remove(int)
* @since Envoy v0.3-alpha
*/
public E remove(int index) {
if (componentList != null) componentList.remove(index);
return elements.remove(index);
}
/**
* @return the amount of elements in this list model
* @see java.util.List#size()
* @since Envoy v0.3-alpha
*/
public int size() { return elements.size(); }
/**
* @return an iterator over the elements of this list model
* @see java.util.List#iterator()
* @since Envoy v0.3-alpha
*/
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
Iterator<E> iter = elements.iterator();
@Override
public boolean hasNext() { return iter.hasNext(); }
@Override
public E next() { return iter.next(); }
};
}
/**
* Sets the component list displaying the elements of this model and triggers a
* synchronization.
*
* @param componentList the component list to set
* @since Envoy v0.3-alpha
*/
void setComponentList(ComponentList<E> componentList) {
this.componentList = componentList;
if (componentList != null) componentList.synchronizeModel();
}
}