import '../Core/IDestructible.dart'; import 'Codec.dart'; import 'dart:collection'; class AutoList extends IDestructible with IterableMixin { List _list = new List(); ST _state; bool _removableList; sort(Function comparer) { _list.sort(comparer); } Iterator get iterator => _list.iterator; /// /// Convert AutoList to array /// /// Array //List toList() //{ // list.OrderBy() // return _list; //} /// Create a new instance of AutoList /// State object to be included when an event is raised. AutoList([ST state, List values]) { this._state = state; this._removableList = Codec.implementsInterface(); if (values != null) addRange(values); register("modified"); register("added"); register("removed"); register("cleared"); } /// /// Synchronization lock of the list /// //public object SyncRoot //{ // get // { // return syncRoot; // } //} /// /// First item in the list /// //T first() //{ // return _list.first; //} operator [](index) { return _list[index]; } operator []=(index, value) { var oldValue = _list[index]; if (_removableList) { if (oldValue != null) (oldValue as IDestructible).off("destroy", _itemDestroyed); if (value != null) value.on("destroy", _itemDestroyed); } //lock (syncRoot) _list[index] = value; emitArgs("modified", [_state, index, oldValue, value]); } /// /// Add item to the list /// add(T value) { if (_removableList) if (value != null) (value as IDestructible).on("destroy", _itemDestroyed); // lock (syncRoot) _list.add(value); emitArgs("add",[_state, value]); } /// /// Add an array of items to the list /// addRange(List values) { values.forEach((x)=>add(x)); } _itemDestroyed(T sender) { remove(sender); } /// /// Clear the list /// clear() { if (_removableList) _list.forEach((x)=>(x as IDestructible)?.off("destroy", _itemDestroyed)); // lock (syncRoot) _list.clear(); emitArgs("cleared", [_state]); } /// /// Remove an item from the list /// Item to remove /// remove(T value) { if (!_list.contains(value)) return; if (_removableList) if (value != null) (value as IDestructible).off("destroy", _itemDestroyed); //lock (syncRoot) _list.remove(value); emitArgs("removed", [_state, value]); } /// /// Number of items in the list /// get count => _list.length; get length => _list.length; /// /// Check if an item exists in the list /// /// Item to check if exists //contains(T value) => _list.contains(value); /// /// Check if any item of the given array is in the list /// /// Array of items containsAny(values) { if (values is List) { for(var v in values) { if (_list.contains(v)) return true; } } else if (values is AutoList) { for(var v in values._list) { if (_list.contains(v)) return true; } } return false; } @override void destroy() { clear(); } }