Fixing Brail Templates for VS Net wizards.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / DictionaryAdapter.cs
blob1e457c15d2e82922e5fcf31b2905d20e5297534d
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ActiveRecord.Framework
17 using System;
18 using System.Collections;
20 /// <summary>
21 /// Maps keys to position in the values array.
22 /// Basically key -> index
23 /// </summary>
24 public class DictionaryAdapter : IDictionary
26 private int _tablesize;
27 private Entry[] _table;
28 private object[] _values;
29 private string[] _keys;
31 /// <summary>
32 /// Initializes a new instance of the <see cref="DictionaryAdapter"/> class.
33 /// </summary>
34 /// <param name="names">The names.</param>
35 /// <param name="values">The values.</param>
36 public DictionaryAdapter(String[] names, object[] values)
38 _tablesize = names.Length;
39 _table = new Entry[_tablesize];
40 _values = values;
41 _keys = names;
43 for(int i=0; i<_tablesize; i++)
45 InternalAdd(names[i], i);
49 private void InternalAdd(String key, int index)
51 int position = CalculateHash(key);
52 System.Diagnostics.Debug.Assert(position >= 0);
54 if (_table[position] == null)
56 _table[position] = new Entry(key, index);
58 else
60 _table[position].AddEntry(new Entry(key, index));
64 private int CalculateHash(String key)
66 uint result = (uint)key.GetHashCode();
67 return (int) Math.Abs(result % _tablesize);
70 /// <summary>
71 /// Simple link list entry
72 /// </summary>
73 public class Entry
75 int index;
76 String key;
77 Entry nextEntry;
79 /// <summary>
80 /// Initializes a new instance of the <see cref="Entry"/> class.
81 /// </summary>
82 /// <param name="key">The key.</param>
83 /// <param name="index">The index.</param>
84 public Entry(String key, int index)
86 this.key = key;
87 this.index = index;
90 internal void AddEntry(Entry entry)
92 Entry walker = this;
94 while(walker.nextEntry != null)
96 walker = walker.nextEntry;
99 walker.nextEntry = entry;
102 /// <summary>
103 /// Finds the specified key.
104 /// </summary>
105 /// <param name="key">The key.</param>
106 /// <returns></returns>
107 public int Find(string key)
109 Entry walker = this;
111 while(walker != null && !walker.key.Equals(key))
113 walker = walker.nextEntry;
116 return walker != null ? walker.index : -1;
120 /// <summary>
121 /// Determines whether the <see cref="T:System.Collections.IDictionary"></see> object contains an element with the specified key.
122 /// </summary>
123 /// <param name="key">The key to locate in the <see cref="T:System.Collections.IDictionary"></see> object.</param>
124 /// <returns>
125 /// true if the <see cref="T:System.Collections.IDictionary"></see> contains an element with the key; otherwise, false.
126 /// </returns>
127 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
128 public bool Contains(object key)
130 return GetValuesIndexByKey(key) != -1;
133 /// <summary>
134 /// Adds an element with the provided key and value to the <see cref="T:System.Collections.IDictionary"></see> object.
135 /// </summary>
136 /// <param name="key">The <see cref="T:System.Object"></see> to use as the key of the element to add.</param>
137 /// <param name="value">The <see cref="T:System.Object"></see> to use as the value of the element to add.</param>
138 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.IDictionary"></see> object. </exception>
139 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
140 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> is read-only.-or- The <see cref="T:System.Collections.IDictionary"></see> has a fixed size. </exception>
141 public void Add(object key, object value)
143 throw new NotSupportedException();
146 /// <summary>
147 /// Removes all elements from the <see cref="T:System.Collections.IDictionary"></see> object.
148 /// </summary>
149 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> object is read-only. </exception>
150 public void Clear()
152 throw new NotSupportedException();
155 /// <summary>
156 /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator"></see> object for the <see cref="T:System.Collections.IDictionary"></see> object.
157 /// </summary>
158 /// <returns>
159 /// An <see cref="T:System.Collections.IDictionaryEnumerator"></see> object for the <see cref="T:System.Collections.IDictionary"></see> object.
160 /// </returns>
161 IDictionaryEnumerator IDictionary.GetEnumerator()
163 throw new NotSupportedException();
166 /// <summary>
167 /// Removes the element with the specified key from the <see cref="T:System.Collections.IDictionary"></see> object.
168 /// </summary>
169 /// <param name="key">The key of the element to remove.</param>
170 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> object is read-only.-or- The <see cref="T:System.Collections.IDictionary"></see> has a fixed size. </exception>
171 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
172 public void Remove(object key)
174 throw new NotSupportedException();
177 /// <summary>
178 /// Gets an <see cref="T:System.Collections.ICollection"></see> object containing the keys of the <see cref="T:System.Collections.IDictionary"></see> object.
179 /// </summary>
180 /// <value></value>
181 /// <returns>An <see cref="T:System.Collections.ICollection"></see> object containing the keys of the <see cref="T:System.Collections.IDictionary"></see> object.</returns>
182 public ICollection Keys
184 get
186 return _keys;
190 /// <summary>
191 /// Gets an <see cref="T:System.Collections.ICollection"></see> object containing the values in the <see cref="T:System.Collections.IDictionary"></see> object.
192 /// </summary>
193 /// <value></value>
194 /// <returns>An <see cref="T:System.Collections.ICollection"></see> object containing the values in the <see cref="T:System.Collections.IDictionary"></see> object.</returns>
195 public ICollection Values
197 get { throw new NotSupportedException(); }
200 /// <summary>
201 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"></see> object is read-only.
202 /// </summary>
203 /// <value></value>
204 /// <returns>true if the <see cref="T:System.Collections.IDictionary"></see> object is read-only; otherwise, false.</returns>
205 public bool IsReadOnly
207 get { return true; }
210 /// <summary>
211 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"></see> object has a fixed size.
212 /// </summary>
213 /// <value></value>
214 /// <returns>true if the <see cref="T:System.Collections.IDictionary"></see> object has a fixed size; otherwise, false.</returns>
215 public bool IsFixedSize
217 get { return true; }
220 /// <summary>
221 /// Gets or sets the <see cref="System.Object"/> with the specified key.
222 /// </summary>
223 /// <value></value>
224 public object this[object key]
226 get
228 if (_values == null || _values.Length.Equals(0))
229 return null;
231 int index = GetValuesIndexByKey(key);
233 if (index >= 0)
235 return _values[index];
238 return null;
242 int index = GetValuesIndexByKey(key);
244 if (index >= 0)
246 _values[index] = value;
248 else
250 throw new ArgumentException(
251 "DictionaryAdapter: Could not find value related to key {" + key + "}. " +
252 "This column is probably not mapped on ActiveRecord");
257 private int GetValuesIndexByKey(object key)
259 int pos = CalculateHash(key.ToString());
261 if (_table[pos] == null) return -1;
263 return _table[pos].Find(key.ToString());
266 /// <summary>
267 /// Copies the elements of the <see cref="T:System.Collections.ICollection"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
268 /// </summary>
269 /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
270 /// <param name="index">The zero-based index in array at which copying begins.</param>
271 /// <exception cref="T:System.ArgumentNullException">array is null. </exception>
272 /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than zero. </exception>
273 /// <exception cref="T:System.ArgumentException">array is multidimensional.-or- index is equal to or greater than the length of array.-or- The number of elements in the source <see cref="T:System.Collections.ICollection"></see> is greater than the available space from index to the end of the destination array. </exception>
274 /// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.ICollection"></see> cannot be cast automatically to the type of the destination array. </exception>
275 public void CopyTo(Array array, int index)
277 throw new NotSupportedException();
280 /// <summary>
281 /// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"></see>.
282 /// </summary>
283 /// <value></value>
284 /// <returns>The number of elements contained in the <see cref="T:System.Collections.ICollection"></see>.</returns>
285 public int Count
287 get { return _tablesize; }
290 /// <summary>
291 /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.
292 /// </summary>
293 /// <value></value>
294 /// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.</returns>
295 public object SyncRoot
297 get { return this; }
300 /// <summary>
301 /// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe).
302 /// </summary>
303 /// <value></value>
304 /// <returns>true if access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe); otherwise, false.</returns>
305 public bool IsSynchronized
307 get { return false; }
310 /// <summary>
311 /// Returns an enumerator that iterates through a collection.
312 /// </summary>
313 /// <returns>
314 /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
315 /// </returns>
316 public IEnumerator GetEnumerator()
318 throw new NotSupportedException();