SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IRDEnumerable.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Collections;
18 using System.Collections.Generic;
19 using System.Linq;
20 using erminas.SmartAPI.CMS;
21 
22 namespace erminas.SmartAPI.Utils.CachedCollections
23 {
24  public interface IRDEnumerable<T> : IEnumerable<T> where T : class, IRedDotObject
25  {
26  bool Contains(T element);
27  bool ContainsGuid(Guid guid);
28  bool ContainsName(string name);
29  int Count { get; }
30 
31  T GetByGuid(Guid guid);
32 
36  T GetByName(string name);
37 
38  bool TryGetByGuid(Guid guid, out T output);
39 
44  bool TryGetByName(string name, out T output);
45  }
46 
47  internal static class RDEnumerableWrapper
48  {
49  internal static IRDEnumerable<T> ToRDEnumerable<T>(this IEnumerable<T> enumerable)
50  where T : class, IRedDotObject
51  {
52  return new RDEnumerable<T>(enumerable);
53  }
54  }
55 
56  internal class RDEnumerable<T> : IRDEnumerable<T> where T : class, IRedDotObject
57  {
58  private readonly IEnumerable<T> _wrappedList;
59 
60  internal RDEnumerable(IEnumerable<T> wrappedList)
61  {
62  _wrappedList = wrappedList.ToList();
63  }
64 
65  public bool Contains(T element)
66  {
67  return _wrappedList.Contains(element);
68  }
69 
70  public bool ContainsGuid(Guid guid)
71  {
72  return _wrappedList.Any(arg => arg.Guid == guid);
73  }
74 
75  public bool ContainsName(string name)
76  {
77  return _wrappedList.Any(arg => arg.Name == name);
78  }
79 
80  public int Count
81  {
82  get { return _wrappedList.Count(); }
83  }
84 
85  public T GetByGuid(Guid guid)
86  {
87  return _wrappedList.First(arg => arg.Guid == guid);
88  }
89 
90  public T GetByName(string name)
91  {
92  return _wrappedList.First(arg => arg.Name == name);
93  }
94 
95  public IEnumerator GetEnumerator()
96  {
97  return ((IEnumerable) _wrappedList).GetEnumerator();
98  }
99 
100  public bool TryGetByGuid(Guid guid, out T output)
101  {
102  output = _wrappedList.FirstOrDefault(arg => arg.Guid == guid);
103  return output != null;
104  }
105 
106  public bool TryGetByName(string name, out T output)
107  {
108  output = _wrappedList.FirstOrDefault(arg => arg.Name == name);
109  return output != null;
110  }
111 
112  IEnumerator<T> IEnumerable<T>.GetEnumerator()
113  {
114  return _wrappedList.GetEnumerator();
115  }
116  }
117 }