OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright 2010 Google Inc | |
3 | |
4 Licensed under the Apache License, Version 2.0 (the "License"); | |
5 you may not use this file except in compliance with the License. | |
6 You may obtain a copy of the License at | |
7 | |
8 http://www.apache.org/licenses/LICENSE-2.0 | |
9 | |
10 Unless required by applicable law or agreed to in writing, software | |
11 distributed under the License is distributed on an "AS IS" BASIS, | |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 See the License for the specific language governing permissions and | |
14 limitations under the License. | |
15 */ | |
16 | |
17 using System; | |
18 | |
19 namespace Google.Apis.Util | |
20 { | |
21 /// <summary> | |
22 /// Lazily returns the result of an operation. Can be used to pass Exception
s along asynchronous methods. | |
23 /// </summary> | |
24 /// <typeparam name="T">Type of the result.</typeparam> | |
25 public class LazyResult<T> | |
26 { | |
27 private T result; | |
28 private bool resultFetched; | |
29 private readonly Func<T> fetchFunction; | |
30 | |
31 internal LazyResult(Func<T> resultFetcher) | |
32 { | |
33 resultFetcher.ThrowIfNull("resultFetcher"); | |
34 fetchFunction = resultFetcher; | |
35 } | |
36 | |
37 /// <summary> | |
38 /// Returns the result associated with this object. Might throw an excep
tion. | |
39 /// </summary> | |
40 public T GetResult() | |
41 { | |
42 if (!resultFetched) | |
43 { | |
44 resultFetched = true; | |
45 result = fetchFunction(); | |
46 } | |
47 | |
48 return result; | |
49 } | |
50 } | |
51 } | |
OLD | NEW |