package com.framsticks.params; import java.util.Collections; import java.util.Iterator; import java.util.Map; import com.framsticks.core.ListChange; public class SimpleUniqueList implements Iterable { protected final Map children; protected final Class type; protected final char prefix; protected final EventListeners listeners = new EventListeners<>(); protected int counter = 0; /** * @param type */ public SimpleUniqueList(Class type, char prefix) { this.type = type; this.prefix = prefix; children = UniqueListAccess.createMap(type, this); } // public static SimpleUniqueList make(Class type) { // return new SimpleUniqueList(type); // } public String generateId() { return prefix + Integer.toString(counter++); } @Override public Iterator iterator() { return children.values().iterator(); } public void addListener(EventListener listener) { listeners.add(listener); } public void removeListener(EventListener listener) { listeners.remove(listener); } public void fireChildrenChange(T child, ListChange.Action action) { ListChange change = new ListChange(action, UniqueListAccess.getNumberInMap(children, child), child.getUid()); listeners.actionForAll(change); } public void add(T child) { child.setUid(generateId()); children.put(child.getUid(), child); fireChildrenChange(child, ListChange.Action.Add); } public void remove(T child) { fireChildrenChange(child, ListChange.Action.Remove); children.remove(child.getUid()); } public void modify(T child) { fireChildrenChange(child, ListChange.Action.Modify); } public Map getView() { return Collections.unmodifiableMap(children); } }