class ConcurrentGroupList<T> extends CopyOnWriteArrayList<T> implements GroupAwareList<T> {

    private static final long serialVersionUID = 7735849143650412115L;

    // This set is used to optimize group operations within this list.
    // We only populate this set when it's needed to dereference the
    // groups in the base list, but once it exists we keep it in sync
    // via the various add/remove operations.
    // NOTE: added volatile keyword for double-check idiom (lazy instantiation)
    private volatile transient Set<String> knownGroupNamesInList;
    
    public ConcurrentGroupList() {
        super();
    }

    public ConcurrentGroupList(Collection<? extends T> c) {
        super(c);
    }

    /**
     * Returns true if the list contains the given JID. If the JID
     * is not found in the list, search the list for groups and look
     * for the JID in each of the corresponding groups.
     * 
     * @param value The target, presumably a JID
     * @return True if the target is in the list, or in any groups in the list
     */
    @Override
    public boolean includes(Object value) {
        boolean found = false;
        if (contains(value)) {
            found = true;
        } else if (value instanceof JID) {
            JID target = (JID) value;
            Iterator<Group> iterator = getGroups().iterator();
            while (!found && iterator.hasNext()) {
                found = iterator.next().isUser(target);
            }
        }
        return found;
    }

    /**
     * Returns the groups that are implied (resolvable) from the items in the list.
     * 
     * @return A Set containing the groups in the list
     */
    @Override
    public Set<Group> getGroups() {
        Set<Group> result = new HashSet<>();
        for (String groupName : getKnownGroupNamesInList()) {
            result.add(Group.resolveFrom(groupName));
        }
        return result;
    }
    
    /**
     * Accessor uses the "double-check idiom" (j2se 5.0+) for proper lazy instantiation.
     * Additionally, the set is not cached until there is at least one group in the list.
     * 
     * @return the known group names among the items in the list
     */
    private Set<String> getKnownGroupNamesInList() {
        Set<String> result = knownGroupNamesInList;
        if (result == null) {
            synchronized(this) {
                result = knownGroupNamesInList;
                if (result == null) {
                    result = new HashSet<>();
                    // add all the groups into the group set
                    for (T listItem : this) {
                        Group group = Group.resolveFrom(listItem);
                        if (group != null) {
                            result.add(group.getName());
                        }
                    }
                    knownGroupNamesInList = result.isEmpty() ? null : result;
                }
            }
        }
        return result;
    }

    /**
     * This method is called from several of the mutators to keep
     * the group set in sync with the full list. 
     * 
     * @param item The item to be added or removed if it is in the group set
     * @param addOrRemove True to add, false to remove
     * @return true if the given item is a group
     */
    private synchronized boolean syncGroups(Object item, boolean addOrRemove) {<FILL_FUNCTION_BODY>}
    
    // below are overrides for the various mutators
    
    @Override
    public T set(int index, T element) {
        T result = super.set(index, element);
        syncGroups(element, ADD);
        return result;
    }

    @Override
    public boolean add(T e) {
        boolean result = super.add(e);
        syncGroups(e, ADD);
        return result;
    }

    @Override
    public void add(int index, T element) {
        super.add(index, element);
        syncGroups(element, ADD);
    }

    @Override
    public T remove(int index) {
        T result = super.remove(index);
        syncGroups(result, REMOVE);
        return result;
    }

    @Override
    public boolean remove(Object o) {
        boolean removed = super.remove(o);
        if (removed) {
            syncGroups(o, REMOVE);
        }
        return removed;
    }

    @Override
    public boolean addIfAbsent(T e) {
        boolean added = super.addIfAbsent(e);
        if (added) {
            syncGroups(e, ADD);
        }
        return added;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        boolean changed = super.removeAll(c);
        if (changed) {
            // drop the transient set, will be rebuilt when/if needed
            synchronized(this) {
                knownGroupNamesInList = null;
            }
        }
        return changed;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        boolean changed = super.retainAll(c);
        if (changed) {
            // drop the transient set, will be rebuilt when/if needed
            synchronized(this) {
                knownGroupNamesInList = null;
            }
        }
        return changed;
    }

    @Override
    public int addAllAbsent(Collection<? extends T> c) {
        int added = super.addAllAbsent(c);
        if (added > 0) {
            // drop the transient set, will be rebuilt when/if needed
            synchronized(this) {
                knownGroupNamesInList = null;
            }
        }
        return added;
    }

    @Override
    public void clear() {
        super.clear();
        synchronized(this) {
            knownGroupNamesInList = null;
        }
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        boolean changed = super.addAll(c);
        if (changed) {
            // drop the transient set, will be rebuilt when/if needed
            synchronized(this) {
                knownGroupNamesInList = null;
            }
        }
        return changed;
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        boolean changed = super.addAll(index, c);
        if (changed) {
            // drop the transient set, will be rebuilt when/if needed
            synchronized(this) {
                knownGroupNamesInList = null;
            }
        }
        return changed;
    }

    private static final boolean ADD = true;
    private static final boolean REMOVE = false;
}

        boolean result = false;
        // only sync if the group list has been instantiated
        if (knownGroupNamesInList != null) {
            Group group = Group.resolveFrom(item);
            if (group != null) {
                result = true;
                if (addOrRemove == ADD) {
                    knownGroupNamesInList.add(group.getName());
                } else if (addOrRemove == REMOVE) {
                    knownGroupNamesInList.remove(group.getName());
                    if (knownGroupNamesInList.isEmpty()) {
                        knownGroupNamesInList = null;
                    }
                }
            }
        }
        return result;
    
<methods>public void <init>() ,public void <init>(Collection<? extends T>) ,public void <init>(T[]) ,public boolean add(T) ,public void add(int, T) ,public boolean addAll(Collection<? extends T>) ,public boolean addAll(int, Collection<? extends T>) ,public int addAllAbsent(Collection<? extends T>) ,public boolean addIfAbsent(T) ,public void clear() ,public java.lang.Object clone() ,public boolean contains(java.lang.Object) ,public boolean containsAll(Collection<?>) ,public boolean equals(java.lang.Object) ,public void forEach(Consumer<? super T>) ,public T get(int) ,public int hashCode() ,public int indexOf(java.lang.Object) ,public int indexOf(T, int) ,public boolean isEmpty() ,public Iterator<T> iterator() ,public int lastIndexOf(java.lang.Object) ,public int lastIndexOf(T, int) ,public ListIterator<T> listIterator() ,public ListIterator<T> listIterator(int) ,public T remove(int) ,public boolean remove(java.lang.Object) ,public boolean removeAll(Collection<?>) ,public boolean removeIf(Predicate<? super T>) ,public void replaceAll(UnaryOperator<T>) ,public boolean retainAll(Collection<?>) ,public T set(int, T) ,public int size() ,public void sort(Comparator<? super T>) ,public Spliterator<T> spliterator() ,public List<T> subList(int, int) ,public java.lang.Object[] toArray() ,public T[] toArray(T[]) ,public java.lang.String toString() <variables>private volatile transient java.lang.Object[] array,final transient java.lang.Object lock,private static final long serialVersionUID