/** * Wraps a collection in a listenable collection.<br> * The type of the listenable collection returned will depend on that of the provided collection : * <ul> * <li>wrapping a Set<T> will return a ListenableSet<T>, using ListenableCollections.listenableSet(Set<T>)</li> * <li>wrapping a List<T> will return a ListenableList<T>, using ListenableCollections.listenableList(List<T>)</li> * <li>wrapping a ListenableCollection<T> will return the same object</li> * <li>otherwise a instance of a ListenableCollection<T> will be returned.</li> * </ul> * @param <T> component type * @param collectionToWrap * @return listenable collection that uses the provided collection as storage */ public static final <T> ListenableCollection<T> listenableCollection(Collection<T> collectionToWrap) { if (collectionToWrap instanceof ListenableCollection<?>) return (ListenableCollection<T>)collectionToWrap; if (collectionToWrap instanceof Set<?>) { return listenableSet((Set<T>)collectionToWrap); } else if (collectionToWrap instanceof List<?>) { return listenableList((List<T>)collectionToWrap); } return new DefaultListenableCollection<T>(collectionToWrap); }