Beispiel #1
0
        public static void Add(DataType data, bool force = false)
        {
            if (null == data)
            {
                return;
            }

            try
            {
                var key = Model <KeyType, DataType> .GetKey(data);

                if (force)
                {
                    Model <KeyType, DataType> .map[key] = data;
                }
                else
                {
                    Model <KeyType, DataType> .map.Add(key, data);
                }
            }
            catch (Exception e)
            {
                string         args = string.Format("{0}", ModelException.ToStringOrNull(data));
                ModelException me   = new ModelException(CLASSNAME, "Add", args, e);

                if (null != ModelException.ExceptHandler)
                {
                    ModelException.ExceptHandler(me);
                }
                else
                {
                    throw me;
                }
            }
        }
Beispiel #2
0
        public static void RegistUpdateCallback(int id, Action <int, DataType> callback)
        {
            if (null == callback)
            {
                return;
            }

            try
            {
                List <Action <int, DataType> > callbacks;
                if (!Model <KeyType, DataType> .updateCallbacks.TryGetValue(id, out callbacks))
                {
                    callbacks = new List <Action <int, DataType> >();
                    Model <KeyType, DataType> .updateCallbacks.Add(id, callbacks);
                }

                callbacks.Add(callback);
            }
            catch (Exception e)
            {
                string         args = string.Format("{0}, {1}", ModelException.ToStringOrNull(id), ModelException.ToStringOrNull(callback));
                ModelException me   = new ModelException(CLASSNAME, "RegistUpdateCallback", args, e);
                if (null != ModelException.ExceptHandler)
                {
                    ModelException.ExceptHandler(me);
                }
                else
                {
                    throw me;
                }
            }
        }
Beispiel #3
0
        public static void Update(int id, DataType data)
        {
            List <Action <int, DataType> > callbacks;

            if (Model <KeyType, DataType> .updateCallbacks.TryGetValue(id, out callbacks))
            {
                try
                {
                    foreach (Action <int, DataType> callback in callbacks)
                    {
                        if (null != callback)
                        {
                            callback(id, data);
                        }
                    }
                }
                catch (Exception e)
                {
                    string         args = string.Format("{0}, {1}", ModelException.ToStringOrNull(id), ModelException.ToStringOrNull(data));
                    ModelException me   = new ModelException(CLASSNAME, "Update", args, e);
                    if (null != ModelException.ExceptHandler)
                    {
                        ModelException.ExceptHandler(me);
                    }
                    else
                    {
                        throw me;
                    }
                }
            }
        }
Beispiel #4
0
        public static bool Remove(KeyType key)
        {
            if (Model <KeyType, DataType> .IsOpened)
            {
                try
                {
                    return(Model <KeyType, DataType> .map.Remove(key));
                }
                catch (Exception e)
                {
                    string         args = string.Format("{0}", ModelException.ToStringOrNull(key));
                    ModelException me   = new ModelException(CLASSNAME, "Remove", args, e);
                    if (null != ModelException.ExceptHandler)
                    {
                        ModelException.ExceptHandler(me);
                    }
                    else
                    {
                        throw me;
                    }
                }
            }

            return(false);
        }
Beispiel #5
0
        public static void Open(IEnumerable <DataType> datas, int count)
        {
            IndexedModel <DataType> .Close();

            IndexedModel <DataType> .IsOpened = true;
#if LOG_DEBUG
            ClosableDebugger.Open(CLASSNAME);
#endif// LOG_DEBUG
            ModelDisposer.Regist(typeof(IndexedModel <DataType>), IndexedModel <DataType> .Close);

            var array = IndexedModel <DataType> .array = new DataType[count];
            try
            {
                if (null == datas)
                {
                    throw new NullReferenceException("datas is null");
                }

                var enumerator = datas.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var data = enumerator.Current;

                    if (null == data)
                    {
                        throw new NullReferenceException("data(element) is null");
                    }

                    var index = data.Index;

                    if (index < 0 || array.Length <= index)
                    {
                        throw new ArgumentException(string.Format("invalid data(element).index:{0}, count:{1}, data(element):{2}", index, count, data));
                    }

                    if (null != array[index])
                    {
                        throw new ArgumentException(string.Format("duplicated data(element).index:{0}, count:{1}, data(element):{2}, prev_data(element):{3}", index, count, data, array[index]));
                    }

                    array[index] = data;
                }
            }
            catch (Exception e)
            {
                string         args = string.Format("{0}, {1}", ModelException.ToStringOrNull(datas), count);
                ModelException me   = new ModelException(CLASSNAME, "Open", args, e);

                if (null != ModelException.ExceptHandler)
                {
                    ModelException.ExceptHandler(me);
                }
                else
                {
                    throw me;
                }
            }
        }
Beispiel #6
0
        public static bool UnregistUpdateCallback(int id, Action <int, DataType> callback)
        {
            if (null == callback)
            {
                return(false);
            }

            List <Action <int, DataType> > callbacks;

            if (Model <KeyType, DataType> .updateCallbacks.TryGetValue(id, out callbacks))
            {
                try
                {
                    for (int n = 0, count = callbacks.Count; n < count; ++n)
                    {
                        if (callback == callbacks[n])
                        {
                            callbacks.RemoveAt(n);
                            if (0 == callbacks.Count)
                            {
                                Model <KeyType, DataType> .updateCallbacks.Remove(id);
                            }

                            return(true);
                        }
                    }
                }
                catch (Exception e)
                {
                    string         args = string.Format("{0}, {1}", ModelException.ToStringOrNull(id), ModelException.ToStringOrNull(callback));
                    ModelException me   = new ModelException(CLASSNAME, "UnregistUpdateCallback", args, e);
                    if (null != ModelException.ExceptHandler)
                    {
                        ModelException.ExceptHandler(me);
                    }
                    else
                    {
                        throw me;
                    }
                }
            }

            return(false);
        }
        public static void Setup(IEnumerable <DataType> datas,
                                 int collectionIndex = 0,
                                 Condition cond      = null,
                                 Comparison <DataType> sortComparer = null)
        {
            try
            {
                if (!SequentialModel <DataType> .IsOpened)
                {
                    throw new Exception("not opened");
                }

                if (null == datas)
                {
                    throw new NullReferenceException("datas is null");
                }

                var coll = SequentialModel <DataType> .GetCollection(collectionIndex);

                if (null == coll)
                {
                    throw new ArgumentOutOfRangeException("invalid collectionIndex");
                }

                var list = coll.__InternalAccessList();

                var enumerator = datas.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var data = enumerator.Current;

                    if (null == data)
                    {
                        throw new NullReferenceException("data(element) is null");
                    }

                    if (null == cond || cond(data))
                    {
                        list.Add(data);
                    }
                }

                if (null != sortComparer)
                {
                    list.Sort(sortComparer);
                }
            }
            catch (Exception e)
            {
                string args = string.Format("{0}, {1}, {2}, {3}",
                                            ModelException.ToStringOrNull(datas),
                                            collectionIndex,
                                            ModelException.ToStringOrNull(cond),
                                            ModelException.ToStringOrNull(sortComparer));
                ModelException me = new ModelException(CLASSNAME, "Setup", args, e);

                if (null != ModelException.ExceptHandler)
                {
                    ModelException.ExceptHandler(me);
                }
                else
                {
                    throw me;
                }
            }
        }