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