interlace /source/library/Interlace/Erlang/ErlangDictionary.cs

Language C# Lines 230
MD5 Hash 20d2ab8730ad5b823fa00688a42166b3 Estimated Cost $3,763 (why?)
Repository https://bitbucket.org/VahidN/interlace.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#region Using Directives and Copyright Notice

// Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Computer Consultancy Pty Ltd nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE 
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Interlace.Erlang
{
    public class ErlangDictionary : ITermWritable
    {
        Dictionary<string, object> _values;

        public ErlangDictionary()
        {
            _values = new Dictionary<string, object>();
        }

        public ErlangDictionary(object dictionaryTuples)
        {
            if (!(dictionaryTuples is List<object>))
            {
                throw new ErlangProtocolException(
                    "A list of options was expected but some term other than a list was found.");
            }

            List<object> dictionaryTuplesList = dictionaryTuples as List<object>;

            _values = new Dictionary<string, object>(dictionaryTuplesList.Count);

            foreach (object tupleObject in dictionaryTuplesList)
            {
                if (!(tupleObject is Tuple))
                {
                    throw new ErlangProtocolException(
                        "A list of options was expected but an element in the list is not a tuple.");
                }

                Tuple tuple = tupleObject as Tuple;

                if (tuple.Length != 2)
                {
                    throw new ErlangProtocolException(
                        "A tuple in an option list is expected to have two elements, and one was " +
                        "found with fewer or more than two items.");
                }

                Atom key = tuple.AtomAt(0);
                object value = tuple.ObjectAt(1);

                _values[key.Value] = value;
            }
        }

        public object this[string key]
        {
            get { return _values[key]; }
            set { _values[key] = value; }
        }

        T InternalAt<T>(string atom)
        {
            if (!_values.ContainsKey(atom)) throw new ErlangProtocolException(string.Format(
                "A list of options was expected to contain an option \"{0}\", but did not.",
                atom));

            if (typeof(T).Equals(typeof(string)) && _values[atom] is List<object>)
            {
                List<object> list = _values[atom] as List<object>;

                if (list.Count == 0) return (T)(object)"";
            }

            if (!(_values[atom] is T)) throw new ErlangProtocolException(string.Format(
                "The option value for option {0} of an option list is not the correct type."));

            return (T)_values[atom];
        }

        public Tuple TupleAt(string atom)
        {
            return InternalAt<Tuple>(atom);
        }

        public string StringAt(string atom)
        {
            return InternalAt<string>(atom);
        }

        public int IntegerAt(string atom)
        {
            return InternalAt<int>(atom);
        }

        public Atom AtomAt(string atom)
        {
            return InternalAt<Atom>(atom);
        }

        public object ObjectAt(string atom)
        {
            return InternalAt<object>(atom);
        }

        T InternalAt<T>(string atom, T defaultValue)
        {
            if (!_values.ContainsKey(atom)) return defaultValue;

            if (typeof(T).Equals(typeof(string)) && _values[atom] is List<object>)
            {
                List<object> list = _values[atom] as List<object>;

                if (list.Count == 0) return (T)(object)"";
            }

            if (!(_values[atom] is T)) throw new ErlangProtocolException(string.Format(
                "The option value for option {0} of an option list is not the correct type."));

            return (T)_values[atom];
        }

        public Tuple TupleAt(string atom, Tuple defaultValue)
        {
            return InternalAt<Tuple>(atom, defaultValue);
        }

        public string StringAt(string atom, string defaultValue)
        {
            return InternalAt<string>(atom, defaultValue);
        }

        public int IntegerAt(string atom, int defaultValue)
        {
            return InternalAt<int>(atom, defaultValue);
        }

        public Atom AtomAt(string atom, Atom defaultValue)
        {
            return InternalAt<Atom>(atom, defaultValue);
        }

        public object ObjectAt(string atom, object defaultValue)
        {
            return InternalAt<object>(atom, defaultValue);
        }

        #region ITermWritable Members

        public object SerializeToTerms()
        {
            List<object> tuples = new List<object>();

            foreach (KeyValuePair<string, object> pair in _values)
            {
                tuples.Add(new Tuple(Atom.From(pair.Key), pair.Value));
            }

            return tuples;
        }

        #endregion

        public override bool Equals(object obj)
        {
            ErlangDictionary rhs = obj as ErlangDictionary;

            if (rhs == null) return false;

            // All of the names in the lhs should be in the rhs, and the values equal:
            foreach (string lhsName in _values.Keys)
            {
                if (!rhs._values.ContainsKey(lhsName)) return false;

                if (!object.Equals(_values[lhsName], rhs._values[lhsName])) return false;
            }

            // All of the names in the rhs should be in the lhs if they are equal:
            foreach (string rhsName in rhs._values.Keys)
            {
                if (!_values.ContainsKey(rhsName)) return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            int hashCode = 0;

            List<string> keys = new List<string>(_values.Keys);
            keys.Sort();

            foreach (string key in keys)
            {
                hashCode ^= key.GetHashCode();
                if (_values[key] != null) hashCode ^= _values[key].GetHashCode();
            }

            return hashCode;
        }
    }
}
Back to Top