Created
December 3, 2010 04:32
-
-
Save kana/726583 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class CacheTable<TKey, TValue> | |
{ | |
public void Expire() | |
{ | |
m_table = null; | |
} | |
abstract public IDictionary<TKey, TValue> CreateTable(); | |
public TValue Lookup(TKey key) | |
{ | |
return table[key]; | |
} | |
public TValue Lookup(TKey key, TValue defaultValue) | |
{ | |
TValue value; | |
if (!(table.TryGetValue(key, out value))) | |
value = defaultValue; | |
return value; | |
} | |
#region Internal | |
private IDictionary<TKey, TValue> table | |
{ | |
get | |
{ | |
if (m_table == null) | |
m_table = CreateTable(); | |
return m_table; | |
} | |
} | |
private IDictionary<TKey, TValue> m_table = null; | |
#endregion | |
} | |
class FooBarCacheTable : CacheTable<char, string> | |
{ | |
override public IDictionary<char, string> CreateTable() | |
{ | |
return ( | |
new [] { | |
"foo", | |
"bar", | |
"baz", | |
} | |
.ToLookup(x => x[0]) | |
.Select(xs => xs.OrderBy(x => x).First()) | |
.ToDictionary(x => x[0]) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment