/** * There are no more guarantees made on the behaviour of the sublists returned by this method upon list change than there are on java.util.List.subList. */ public List<T> subList(int fromIndex, int toIndex) { return new DefaultListenableList<T>(list.subList(fromIndex, toIndex),collectionSupport); }
/** * Wraps a list in a listenable list.<br> * If is provided with a list that implements the RandomAccess interface, this method returns a listenable list that also implements the RandomAccess interface. * @param <T> component type * @param listToWrap * @return listenable list that uses the provided list as storage */ public static final <T> ListenableList<T> listenableList(List<T> listToWrap) { if (listToWrap instanceof ListenableList<?>) return (ListenableList<T>)listToWrap; if (listToWrap instanceof RandomAccess) { class RandomAccessListenableList extends DefaultListenableList<T> implements RandomAccess { public RandomAccessListenableList(List<T> l) { super(l); } }; return new RandomAccessListenableList(listToWrap); } return new DefaultListenableList<T>(listToWrap); }
/** * Create a listenable list that will dynamically reflect the contents of the source listenable collection.<br> * This is useful for instance when you want to put the contents of a set in a swing JList (then use new JList(new ListenableListModel(ListenableCollections.asList(yourSet)))). * @param <T> type of the elements of the collection * @param source collection that is to be adapted to a listenable list * @return source if it is already a listenable list, otherwise a new listenable list that is two-ways bound to the source collection. */ public static <T> ListenableList<T> asList(ListenableCollection<T> source) { if (source instanceof ListenableList<?>) return (ListenableList<T>)source; ListenableList<T> out = new DefaultListenableList<T>(new ArrayList<T>(source)); bind(source, out); return out; }