Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PackageCollection.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Collections.Specialized;
7 using System.Diagnostics;
8 using System.Linq;
9 using SiliconStudio.Core;
10 using SiliconStudio.Core.Diagnostics;
11 using SiliconStudio.Core.Extensions;
12 
13 namespace SiliconStudio.Assets
14 {
15  /// <summary>
16  /// A collection of <see cref="Package"/>.
17  /// </summary>
18  [DebuggerTypeProxy(typeof(CollectionDebugView))]
19  [DebuggerDisplay("Count = {Count}")]
20  [DataContract("PackageCollection")]
21  public sealed class PackageCollection : ICollection<Package>, INotifyCollectionChanged
22  {
23  private readonly List<Package> packages;
24 
25  /// <summary>
26  /// Occurs when the collection changes.
27  /// </summary>
28  public event NotifyCollectionChangedEventHandler CollectionChanged;
29 
30  /// <summary>
31  /// Initializes a new instance of the <see cref="PackageCollection"/> class.
32  /// </summary>
34  {
35  packages = new List<Package>();
36  }
37 
38  public int Count
39  {
40  get
41  {
42  return packages.Count;
43  }
44  }
45 
46  public bool IsReadOnly
47  {
48  get
49  {
50  return false;
51  }
52  }
53 
54  public IEnumerator<Package> GetEnumerator()
55  {
56  return packages.GetEnumerator();
57  }
58 
59  IEnumerator IEnumerable.GetEnumerator()
60  {
61  return GetEnumerator();
62  }
63 
64  /// <summary>
65  /// Finds the specified package by its unique identifier.
66  /// </summary>
67  /// <param name="packageGuid">The package unique identifier.</param>
68  /// <returns>Package.</returns>
69  public Package Find(Guid packageGuid)
70  {
71  return packages.FirstOrDefault(package => package.Id == packageGuid);
72  }
73 
74  /// <summary>
75  /// Finds the a package already in this collection from the specified dependency.
76  /// </summary>
77  /// <param name="packageDependency">The package dependency.</param>
78  /// <returns>Package.</returns>
79  public Package Find(PackageDependency packageDependency)
80  {
81  if (packageDependency == null) throw new ArgumentNullException("packageDependency");
82  return Find(packageDependency.Name, packageDependency.Version);
83  }
84 
85  /// <summary>
86  /// Finds a package with the specified name and <see cref="PackageVersionRange"/>.
87  /// </summary>
88  /// <param name="name">The name.</param>
89  /// <param name="versionRange">The version range.</param>
90  /// <returns>Package.</returns>
91  public Package Find(string name, PackageVersionRange versionRange)
92  {
93  if (name == null) throw new ArgumentNullException("name");
94  if (versionRange == null) throw new ArgumentNullException("versionRange");
95  var filter = versionRange.ToFilter();
96  return packages.FirstOrDefault(package => package.Meta.Name == name && filter(package));
97  }
98 
99  public void Add(Package item)
100  {
101  if (item == null) throw new ArgumentNullException("item");
102  if (Find(item.Id) == null)
103  {
104  packages.Add(item);
105  OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
106  }
107  }
108 
109  public void Clear()
110  {
111  var oldPackages = new List<Package>(packages);
112  packages.Clear();
113  OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, packages, oldPackages));
114  }
115 
116  /// <summary>
117  /// Determines whether this collection contains a package with the specified package unique identifier.
118  /// </summary>
119  /// <param name="packageGuid">The package unique identifier.</param>
120  /// <returns><c>true</c> if this collection contains a package with the specified package unique identifier; otherwise, <c>false</c>.</returns>
121  public bool ContainsById(Guid packageGuid)
122  {
123  return packages.Any(package => package.Id == packageGuid);
124  }
125 
126  public bool Contains(Package item)
127  {
128  if (item == null) throw new ArgumentNullException("item");
129  return ContainsById(item.Id);
130  }
131 
132  public void CopyTo(Package[] array, int arrayIndex)
133  {
134  packages.CopyTo(array, arrayIndex);
135  }
136 
137  public bool RemoveById(Guid packageGuid)
138  {
139  var item = Find(packageGuid);
140  if (item != null)
141  {
142 
143  packages.Remove(item);
144  OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
145  return true;
146  }
147  return false;
148  }
149 
150  public bool Remove(Package item)
151  {
152  if (item == null) throw new ArgumentNullException("item");
153  return RemoveById(item.Id);
154  }
155 
156  private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
157  {
158  NotifyCollectionChangedEventHandler handler = CollectionChanged;
159  if (handler != null) handler(this, e);
160  }
161  }
162 }
IEnumerator< Package > GetEnumerator()
Package Find(string name, PackageVersionRange versionRange)
Finds a package with the specified name and PackageVersionRange.
Package Find(Guid packageGuid)
Finds the specified package by its unique identifier.
Package Find(PackageDependency packageDependency)
Finds the a package already in this collection from the specified dependency.
bool ContainsById(Guid packageGuid)
Determines whether this collection contains a package with the specified package unique identifier...
string Name
Gets or sets the package name Id.
Guid Id
Gets or sets the unique identifier of this asset.
Definition: Asset.cs:40
NotifyCollectionChangedEventHandler CollectionChanged
Occurs when the collection changes.
Use this class to provide a debug output in Visual Studio debugger.
A reference to a package either internal (directly to a Package inside the same solution) or external...
void CopyTo(Package[] array, int arrayIndex)
A package managing assets.
Definition: Package.cs:28
PackageVersionRange Version
Gets or sets the version.
PackageCollection()
Initializes a new instance of the PackageCollection class.
A dependency to a range of version.