Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive/3.3/ServiceClients/HiveItemCollection.cs @ 14615

Last change on this file since 14615 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Clients.Hive {
29  [Item("HiveItem Collection", "Represents a collection of Hive items.")]
30  public class HiveItemCollection<T> : ItemCollection<T> where T : class, IHiveItem {
31    protected HiveItemCollection(HiveItemCollection<T> original, Cloner cloner) : base(original, cloner) { }
32    public HiveItemCollection() : base() { }
33    public HiveItemCollection(IEnumerable<T> collection) : base(collection) { }
34
35    public override IDeepCloneable Clone(Cloner cloner) { return new HiveItemCollection<T>(this, cloner); }
36
37    protected override void OnItemsRemoved(IEnumerable<T> items) {
38      IEnumerable<T> successful, unsuccessful;
39      Exception ex;
40      RemoveItems(items, out successful, out unsuccessful, out ex);
41      list.AddRange(unsuccessful);
42      base.OnItemsRemoved(successful);
43      if (ex != null) throw ex;
44    }
45    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
46      IEnumerable<T> successful, unsuccessful;
47      Exception ex;
48      RemoveItems(oldItems, out successful, out unsuccessful, out ex);
49      list.AddRange(unsuccessful);
50      base.OnCollectionReset(items.Concat(unsuccessful), oldItems);
51      if (ex != null) throw ex;
52    }
53
54    public void ClearWithoutHiveDeletion() {
55      if (list.Count > 0) {
56        T[] items = list.ToArray();
57        list.Clear();
58        OnPropertyChanged("Count");
59        //don't call OnCollectionReset directly as it would delete the job
60        base.OnCollectionReset(list, items);
61      }
62    }
63
64    private void RemoveItems(IEnumerable<T> items, out IEnumerable<T> successful, out IEnumerable<T> unsuccessful, out Exception exception) {
65      List<T> removed = new List<T>();
66      List<T> notremoved = new List<T>();
67      exception = null;
68      foreach (T item in items) {
69        try {
70          HiveClient.Delete(item);
71          removed.Add(item);
72        }
73        catch (Exception ex) {
74          exception = ex;
75          notremoved.Add(item);
76        }
77      }
78      successful = removed;
79      unsuccessful = notremoved;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.