feb3cf12ec88e81053e9c14f6a341224f7b12907
[mono.git] / mcs / class / Mono.C5 / current / UserGuideExamples / ReadOnlyPatterns.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: ReadOnlyPatterns.cs for pattern chapter\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll ReadOnlyPatterns.cs \r
26 \r
27 using System;\r
28 using C5;\r
29 using SCG = System.Collections.Generic;\r
30 \r
31 namespace ReadOnlyPatterns {\r
32   class ReadOnlyPatterns {\r
33     public static void Main(String[] args) {\r
34       GuardHashSet<int>();\r
35       GuardTreeSet<int>();\r
36       GuardList<int>();\r
37       GuardHashDictionary<int,int>();\r
38       GuardSortedDictionary<int,int>();\r
39     }\r
40 \r
41     // Read-only access to a hash-based collection\r
42     \r
43     static void GuardHashSet<T>() {\r
44       ICollection<T> coll = new HashSet<T>();\r
45       DoWork(new GuardedCollection<T>(coll));\r
46     }\r
47 \r
48     static void DoWork<T>(ICollection<T> gcoll) { \r
49       // Use gcoll ... \r
50     }\r
51 \r
52     // Read-only access to an indexed sorted collection\r
53 \r
54     static void GuardTreeSet<T>() {\r
55       IIndexedSorted<T> coll = new TreeSet<T>(); \r
56       DoWork(new GuardedIndexedSorted<T>(coll));\r
57     }\r
58     \r
59     static void DoWork<T>(IIndexedSorted<T> gcoll) { \r
60       // Use gcoll ...\r
61     }\r
62 \r
63     // Read-only access to a list\r
64 \r
65     static void GuardList<T>() {\r
66       IList<T> coll = new ArrayList<T>(); \r
67       DoWork(new GuardedList<T>(coll));\r
68     }\r
69     \r
70     static void DoWork<T>(IList<T> gcoll) { \r
71       // Use gcoll ...\r
72     }\r
73 \r
74     // Read-only access to a dictionary\r
75 \r
76     static void GuardHashDictionary<K,V>() {\r
77       IDictionary<K,V> dict = new HashDictionary<K,V>(); \r
78       DoWork(new GuardedDictionary<K,V>(dict));\r
79     }\r
80     \r
81     static void DoWork<K,V>(IDictionary<K,V> gdict) { \r
82       // Use gdict ...\r
83     }\r
84 \r
85     // Read-only access to a sorted dictionary\r
86 \r
87     static void GuardSortedDictionary<K,V>() {\r
88       ISortedDictionary<K,V> dict = new TreeDictionary<K,V>(); \r
89       DoWork(new GuardedSortedDictionary<K,V>(dict));\r
90     }\r
91     \r
92     static void DoWork<K,V>(ISortedDictionary<K,V> gdict) { \r
93       // Use gdict ...\r
94     }\r
95   }\r
96 }\r