SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IndexedRDList.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.Generic;
18 using System.Linq;
19 using erminas.SmartAPI.CMS;
20 
21 namespace erminas.SmartAPI.Utils.CachedCollections
22 {
23  public interface IIndexedRDList<in TK, T> : IIndexedCachedList<TK, T>, IRDList<T> where T : class, IRedDotObject
24  {
25  new IIndexedRDList<TK, T> Refreshed();
26  void WaitFor(Func<IIndexedRDList<TK, T>, bool> predicate, TimeSpan maxWait, TimeSpan retryEverySecond);
27  }
28 
29  public class IndexedRDList<TK, T> : IndexedCachedList<TK, T>, IIndexedRDList<TK, T> where T : class, IRedDotObject
30  {
31  public IndexedRDList(Func<List<T>> retrieveFunc, Func<T, TK> indexFunc, Caching caching)
32  : base(retrieveFunc, indexFunc, caching)
33  {
34  }
35 
36  protected IndexedRDList(Func<T, TK> indexFunc, Caching caching) : base(indexFunc, caching)
37  {
38  }
39 
40  public bool Contains(T element)
41  {
42  return ContainsGuid(element.Guid);
43  }
44 
45  public bool ContainsGuid(Guid guid)
46  {
47  T tmp;
48  return TryGetByGuid(guid, out tmp);
49  }
50 
51  public bool ContainsName(string name)
52  {
53  T tmp;
54  return TryGetByName(name, out tmp);
55  }
56 
57  public virtual T GetByGuid(Guid guid)
58  {
59  EnsureListIsLoaded();
60  return List.First(x => x.Guid == guid);
61  }
62 
63  public virtual T GetByName(string name)
64  {
65  EnsureListIsLoaded();
66  return List.First(x => x.Name == name);
67  }
68 
69  public new IIndexedRDList<TK, T> Refreshed()
70  {
71  Refresh();
72  return this;
73  }
74 
75  public virtual bool TryGetByGuid(Guid guid, out T output)
76  {
77  EnsureListIsLoaded();
78  output = List.FirstOrDefault(x => x.Guid == guid);
79  return output != null;
80  }
81 
82  public virtual bool TryGetByName(string name, out T output)
83  {
84  EnsureListIsLoaded();
85  output = List.FirstOrDefault(x => x.Name == name);
86  return output != null;
87  }
88 
89  public void WaitFor(Func<IIndexedRDList<TK, T>, bool> predicate, TimeSpan maxWait, TimeSpan retryPeriod)
90  {
91  Wait.For(() => predicate(Refreshed()), maxWait, retryPeriod);
92  }
93 
94  public void WaitFor(Predicate<IRDList<T>> predicate, TimeSpan maxWait, TimeSpan retryPeriod)
95  {
96  Wait.For(() => predicate(Refreshed()), maxWait, retryPeriod);
97  }
98 
100  {
101  Refresh();
102  return this;
103  }
104  }
105 
110  public class NameIndexedRDList<T> : IndexedRDList<String, T> where T : class, IRedDotObject
111  {
112  public NameIndexedRDList(Func<List<T>> retrieveFunc, Caching caching) : base(retrieveFunc, x => x.Name, caching)
113  {
114  }
115 
116  protected NameIndexedRDList(Caching caching) : base(x => x.Name, caching)
117  {
118  }
119 
120  public override T GetByName(string name)
121  {
122  return this[name];
123  }
124 
125  public new NameIndexedRDList<T> Refreshed()
126  {
127  Refresh();
128  return this;
129  }
130 
131  public override bool TryGetByName(string name, out T output)
132  {
133  return TryGet(name, out output);
134  }
135  }
136 
141  public class GuidIndexedRDList<T> : IndexedRDList<Guid, T> where T : class, IRedDotObject
142  {
143  public GuidIndexedRDList(Func<List<T>> retrieveFunc, Caching caching) : base(retrieveFunc, x => x.Guid, caching)
144  {
145  }
146 
147  protected GuidIndexedRDList(Caching caching) : base(x => x.Guid, caching)
148  {
149  }
150 
151  public override T GetByGuid(Guid guid)
152  {
153  return this[guid];
154  }
155 
156  public new GuidIndexedRDList<T> Refreshed()
157  {
158  Refresh();
159  return this;
160  }
161 
162  public override bool TryGetByGuid(Guid guid, out T output)
163  {
164  return TryGet(guid, out output);
165  }
166  }
167 }