/* * Copyright (C) 2007-2012 Xagasoft, All rights reserved. * * This file is part of the libgats library and is released under the * terms of the license contained in the file LICENSE. */ using System.Text; using System.IO; using System.Linq; using System.Collections; using System.Collections.Generic; namespace Com.Xagasoft.Gats { public class GatsDictionary : GatsObject, IDictionary // ICollection>, // IEnumerable> { private Dictionary Value = new Dictionary(); public GatsDictionary() { } public override string ToString() { StringBuilder bld = new StringBuilder(); bld.Append("{"); bool begin = true; foreach( KeyValuePair j in Value ) { if( begin ) begin = false; else bld.Append(", "); bld.Append( j.Key ); bld.Append(" = "); bld.Append( j.Value ); } bld.Append("}"); return bld.ToString(); } public override void Read( Stream s, char type ) { for(;;) { GatsObject key = GatsObject.Read( s ); if( key == null ) break; if( key.GetType() != typeof(GatsString) ) throw new GatsException( GatsException.Type.InvalidFormat ); Value.Add( key.ToString(), GatsObject.Read( s ) ); } } public override void Write( Stream s ) { s.WriteByte( (int)'d' ); foreach( KeyValuePair j in Value ) { new GatsString( j.Key ).Write( s ); j.Value.Write( s ); } s.WriteByte( (int)'e' ); } // // List interface overrides under here. // public void Add( string key, GatsObject obj ) { Value.Add( key, obj ); } public bool Remove( string key ) { return Value.Remove( key ); } public bool ContainsKey( string key ) { return Value.ContainsKey( key ); } public bool TryGetValue( string key, out GatsObject obj ) { return Value.TryGetValue( key, out obj ); } public GatsObject this[string key] { get { return this.Value[key]; } set { this.Value[key] = value; } } public ICollection Keys { get { return this.Value.Keys; } } public ICollection Values { get { return this.Value.Values; } } public int Count { get { return this.Value.Count; } } public bool IsReadOnly { get { return false; } // return this.Value.IsReadOnly; } } public void Add( KeyValuePair pair ) { ((IDictionary)Value).Add( pair ); } public void Clear() { Value.Clear(); } public bool Contains( KeyValuePair pair ) { return ((IDictionary)Value).Contains( pair ); } public void CopyTo( KeyValuePair[] result, int count ) { ((IDictionary)Value).CopyTo( result, count ); } public bool Remove( KeyValuePair pair ) { return ((IDictionary)Value).Remove( pair ); } IEnumerator > IEnumerable >.GetEnumerator() { return Value.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return Value.GetEnumerator(); } } }